diff --git a/logger/logger.js b/logger/logger.js new file mode 100644 index 0000000..b1300cf --- /dev/null +++ b/logger/logger.js @@ -0,0 +1,26 @@ +import pino from 'pino' +import { logflarePinoVercel } from 'pino-logflare' + +// create pino-logflare console stream for serverless functions and send function for browser logs +// Browser logs are going to: https://logflare.app/sources/13989 +const { stream, send } = logflarePinoVercel({ + apiKey: "eA_3wro12LpZ", + sourceToken: "eb1d841a-e0e4-4d23-af61-84465c808157" +}); + +// create pino loggger +const logger = pino({ + browser: { + transmit: { + level: "info", + send: send, + } + }, + level: "debug", + base: { + env: process.env.ENV || "ENV not set", + revision: process.env.VERCEL_GITHUB_COMMIT_SHA, + }, +}, stream); + +export default logger \ No newline at end of file diff --git a/pages/api/hello.js b/pages/api/hello.js index df63de8..93232d2 100644 --- a/pages/api/hello.js +++ b/pages/api/hello.js @@ -1,5 +1,34 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction +import logger from '../../logger/logger' + export default function handler(req, res) { - res.status(200).json({ name: 'John Doe' }) + res.status(200) + + // Logging to pino-logflare + logger.info({ + requst: { + method: req.method, + path: req.path + }, + response: { + status: res.status + } + }, "Handled response. Logged with pino-logflare.") + + // Logging with pino. Both will end up in Vercel's log drains with slight different payloads. + // Both will end up Logflare if a log drain is setup. + const onlyPino = require('pino')() + + onlyPino.info({ + requst: { + method: req.method, + path: req.path + }, + response: { + status: res.status + } + }, "Handled response. Logged with pino.") + + res.json({ name: 'John Doe' }) } diff --git a/pages/index.js b/pages/index.js index 08145bb..ecc51b6 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,8 +1,19 @@ import Head from 'next/head' import Image from 'next/image' import styles from '../styles/Home.module.css' +import logger from '../../logger/logger' export default function Home() { + // Logging to pino-logflare. + // Will get sent to Logflare via HTTP. + logger.info("Client side logging. Logged with pino-logflare.") + + // Logging with pino. + // Will appear only in the console of the client. + const onlyPino = require('pino')() + + onlyPino.info("Client side logging. Logged with pino.") + return (