Skip to content

Commit

Permalink
Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chasers committed Jun 30, 2021
1 parent 6b1d369 commit eeefc18
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
26 changes: 26 additions & 0 deletions logger/logger.js
Original file line number Diff line number Diff line change
@@ -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
31 changes: 30 additions & 1 deletion pages/api/hello.js
Original file line number Diff line number Diff line change
@@ -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' })
}
11 changes: 11 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className={styles.container}>
<Head>
Expand Down

0 comments on commit eeefc18

Please sign in to comment.