Properly log Error object and its properties, fixes #3.
Went with stack as an error message for Error objects as it works nicely with pino-pretty
Example output:
$ npm start
> next-logger-example@0.1.0 start
> NODE_OPTIONS='-r next-logger' next start
{"level":30,"time":1626264458782,"pid":18407,"hostname":"MacBook-Pro","name":"next.js","prefix":"ready","msg":"started server on 0.0.0.0:3000, url: http://localhost:3000"}
{"level":50,"time":1626264459086,"pid":18407,"hostname":"MacBook-Pro","name":"next.js","prefix":"error","stack":"Error: Could not find a production build in the '/path/to/next-logger-example/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\n at Server.readBuildId (/path/to/next-logger-example/node_modules/next/dist/next-server/server/next-server.js:151:355)\n at new Server (/path/to/next-logger-example/node_modules/next/dist/next-server/server/next-server.js:3:120)\n at NextServer.createServer (/path/to/next-logger-example/node_modules/next/dist/server/next.js:1:2935)\n at async /path/to/next-logger-example/node_modules/next/dist/server/next.js:1:3360","message":"Could not find a production build in the '/path/to/next-logger-example/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id","msg":"Error: Could not find a production build in the '/path/to/next-logger-example/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\n at Server.readBuildId (/path/to/next-logger-example/node_modules/next/dist/next-server/server/next-server.js:151:355)\n at new Server (/path/to/next-logger-example/node_modules/next/dist/next-server/server/next-server.js:3:120)\n at NextServer.createServer (/path/to/next-logger-example/node_modules/next/dist/server/next.js:1:2935)\n at async /path/to/next-logger-example/node_modules/next/dist/server/next.js:1:3360"}
Throwing error inside Next.js application also works:
$ npm run dev
> next-logger-example@0.1.0 dev
> NODE_OPTIONS='-r next-logger' next dev | pino-pretty
[1626264485580] INFO (next.js/18432 on MacBook-Pro): started server on 0.0.0.0:3000, url: http://localhost:3000
prefix: "ready"
[1626264485713] INFO (next.js/18432 on MacBook-Pro): Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
prefix: "info"
[1626264487760] INFO (next.js/18432 on MacBook-Pro): compiled successfully
prefix: "event"
[1626264489621] INFO (next.js/18432 on MacBook-Pro): build page: /next/dist/pages/_error
prefix: "event"
[1626264489622] INFO (next.js/18432 on MacBook-Pro): compiling...
prefix: "wait"
[1626264489764] INFO (next.js/18432 on MacBook-Pro): compiled successfully
prefix: "event"
[1626264515043] INFO (next.js/18432 on MacBook-Pro): build page: /
prefix: "event"
[1626264515043] INFO (next.js/18432 on MacBook-Pro): compiling...
prefix: "wait"
[1626264517795] INFO (next.js/18432 on MacBook-Pro): compiled successfully
prefix: "event"
[1626264517902] ERROR (next.js/18432 on MacBook-Pro): Error: Phail
at Home (webpack-internal:///./pages/index.js:19:9)
at processChild (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3353:14)
at resolve (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3270:5)
at ReactDOMServerRenderer.render (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3753:22)
at ReactDOMServerRenderer.read (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3690:29)
at renderToString (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:4298:27)
at Object.renderPage (/path/to/next-logger-example/node_modules/next/dist/next-server/server/render.js:53:854)
at Function.getInitialProps (webpack-internal:///./node_modules/next/dist/pages/_document.js:211:19)
at loadGetInitialProps (/path/to/next-logger-example/node_modules/next/dist/next-server/lib/utils.js:5:101)
at renderToHTML (/path/to/next-logger-example/node_modules/next/dist/next-server/server/render.js:53:1145)
prefix: "error"
stack: "Error: Phail\n at Home (webpack-internal:///./pages/index.js:19:9)\n at processChild (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3353:14)\n at resolve (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3270:5)\n at ReactDOMServerRenderer.render (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3753:22)\n at ReactDOMServerRenderer.read (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3690:29)\n at renderToString (/path/to/next-logger-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:4298:27)\n at Object.renderPage (/path/to/next-logger-example/node_modules/next/dist/next-server/server/render.js:53:854)\n at Function.getInitialProps (webpack-internal:///./node_modules/next/dist/pages/_document.js:211:19)\n at loadGetInitialProps (/path/to/next-logger-example/node_modules/next/dist/next-server/lib/utils.js:5:101)\n at renderToHTML (/path/to/next-logger-example/node_modules/next/dist/next-server/server/render.js:53:1145)"
Properly log
Error
object and its properties, fixes #3.Went with
stack
as an error message forError
objects as it works nicely with pino-prettyExample output:
Throwing error inside Next.js application also works: