Pino logger does not output JSON within NextJS middleware function #33898
Replies: 10 comments 13 replies
-
I have a similar issue, but ours is pretty-printing JSON and it's driving me crazy:
comes out as
(which is not even technically JSON, it's just a pretty-printed js object) and it's kind of driving me crazy. Anyone know how to turn this off? |
Beta Was this translation helpful? Give feedback.
-
Having the same issue, in our case Data Dog consumes each line as new log message. |
Beta Was this translation helpful? Give feedback.
-
Having the same issue while using pino logger in middleware, but I'm trying to output the log in server. |
Beta Was this translation helpful? Give feedback.
-
Same issue here. I ended up writing a small wrapper to JSON stringify the error and add a
|
Beta Was this translation helpful? Give feedback.
-
Any updates on this issue? I am having the same problem: none of the logs created in my NextJS middlleware are formatted as JSON. Pino-pretty is not working as well. 😢 |
Beta Was this translation helpful? Give feedback.
-
How is everyone using pino from the middleware at all? I get the |
Beta Was this translation helpful? Give feedback.
-
how about this one? pino({
browser: {
write: (o) => console.log(JSON.stringify(o)),
},
}).info('Pino info log from middleware');
pino({
browser: {
write: (o) => console.log(JSON.stringify(o)),
},
}).error('Pino error log from middleware');
pino({
browser: {
write: (o) => console.log(JSON.stringify(o)),
},
}).fatal('Pino fatal log from middleware');
const logger = pino({
browser: {
write: (o) => console.log(JSON.stringify(o)),
},
});
logger.child({ '1': '2', '3': '4' }).info('Pino info log from middleware'); |
Beta Was this translation helpful? Give feedback.
-
To add some context here, the NextJS middleware runs in the edge runtime, which effects more than just |
Beta Was this translation helpful? Give feedback.
-
Has anyone tested this in Next.js 14? |
Beta Was this translation helpful? Give feedback.
-
Just to summarise and hopefully save someone some time :
const serverNodeRuntimeConfig = {
transport: {
target: `pino-pretty`,
options: {
colorize: true,
messageFormat: `[{group}] {msg}`,
hideObject: true,
ignore: `pid,hostname`,
},
},
}
const defaultConfig = {
browser: {
write: (logObj: unknown) => {
const { level, msg, group, time, icon } = logObj as Record<string, string>
const levelUppercased = level.toUpperCase()
const timeFormatted = format(new Date(time), `HH:mm:ss.sss`)
const levelColor = getLevelColor(levelUppercased)
// Output the formatted message using console.log
console.log(
`[${timeFormatted}] ${levelColor}${levelUppercased} ${COLOR.CYAN}[${icon} ${group}] ${msg} ${COLOR.WHITE}`
)
},
formatters: {
level: (label: string) => {
return {
level: label,
}
},
},
},
}
const baseLogger: Logger = pino({
...baseConfig,
...(ENVIRONMENT.IS_RUNTIME_NODE ? serverNodeRuntimeConfig : defaultConfig),
// Disable logging on the client unless in development
enabled: ENVIRONMENT.IS_SERVER || ENVIRONMENT.IS_DEVELOPMENT,
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I was planning to use Pino logger within NextJS middleware function:
pages/_middleware.ts
Then when I run
next dev
and request the page, log in no JSON format is printed out:Weirdly when I use the same logging within any page component:
Pino logs in JSON format:
Not sure what's going. Does NextJS somehow reformat output coming out from middleware functions? 🤔 The same happens for production build with
NODE_ENV=production
.Beta Was this translation helpful? Give feedback.
All reactions