next-logger
JSON logging patcher for Next.js
Description
This is a library to patch the logging functions used by Next.js, to have them output to stdout as newline-delimited JSON. This allows a Next.js application to log service events in a format that's compatible with log aggregators, without needing a custom Next.js server.
This works by importing Next.js' inbuilt logger via require, and replacing the logging methods with custom ones. It uses pino to output JSON formatted logs, preserving Next.js' message and prefix, but adding timestamp, hostname and more. Although the library was mainly developed based on pino, it also supports winston as the logger backend. See the Custom Logger section below for more details.
From v2.0.0 onwards, this library also patches the global console methods, to catch additional logs that Next.js makes directly to console. While pino logging remains intact, this may cause issues with other libraries which patch or use console methods. Use the next-only preset to opt-out of this patching.
Example Logs
Before:
ready - started server on http://localhost:3000
info - Using external babel configuration from .babelrc
event - compiled successfully
After:
{"level":30,"time":1609160882850,"pid":18493,"hostname":"MyHostname","name":"next.js","msg":"started server on http://localhost:3000","prefix":"ready"}
{"level":30,"time":1609160883607,"pid":18493,"hostname":"MyHostname","name":"next.js","msg":"Using external babel configuration from .babelrc","prefix":"info"}
{"level":30,"time":1609160885675,"pid":18493,"hostname":"MyHostname","name":"next.js","msg":"compiled successfully","prefix":"event"}