Skip to content

Commit

Permalink
Split up types of logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chasers committed Jun 30, 2021
1 parent f730453 commit daba67f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
13 changes: 13 additions & 0 deletions pages/api/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logger from '../../logger/logger'

export default function handler(req, res) {

// Lets log an error with console here.
try {
throw new Error('Whoops!')
} catch (e) {
console.error(e)
}

res.status(200).json({ error: 'true' })
}
22 changes: 22 additions & 0 deletions pages/api/hello-console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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)

const data = {
request: {
method: req.method,
url: req.url
},
response: {
status: res.statusCode
}
}

// We can also simply parse our object into JSON and log it with console.
console.info(JSON.stringify(data), "Handled response. Logged with `console`.")

res.json({ name: 'John Doe' })
}
22 changes: 22 additions & 0 deletions pages/api/hello-pino-logflare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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)

const data = {
request: {
method: req.method,
url: req.url
},
response: {
status: res.statusCode
}
}

// Logging to pino-logflare
logger.info(data, "Handled response. Logged with pino-logflare.")

res.json({ name: 'John Doe' })
}
8 changes: 1 addition & 7 deletions pages/api/hello.js → pages/api/hello-pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ export default function handler(req, res) {
}
}

// Logging to pino-logflare
logger.info(data, "Handled response. Logged with pino-logflare.")

// Logging with pino.
// Logging with straight pino.
// Both will end up in Vercel's log drains with slight different payloads.
const onlyPino = require('pino')()

onlyPino.info(data, "Handled response. Logged with pino.")

// We can also simply parse our object into JSON and log it with console.
console.info(JSON.stringify(data), "Handled response. Logged with `console`.")

res.json({ name: 'John Doe' })
}

0 comments on commit daba67f

Please sign in to comment.