Replies: 16 comments 9 replies
-
I am also getting this error since Nextjs |
Beta Was this translation helpful? Give feedback.
-
@scmx Maybe this topic could help you |
Beta Was this translation helpful? Give feedback.
-
I worked around this by piping to pino-pretty instead.
For example: "dev": "next dev | pino-pretty -m message -i time -c" |
Beta Was this translation helpful? Give feedback.
-
I just came across this as well - it seems my Pino logger will work from the |
Beta Was this translation helpful? Give feedback.
-
I followed the solution of using pipe like @scmx suggested. Hopefully it will be fixed. |
Beta Was this translation helpful? Give feedback.
-
Interesting. My nextjs app is using app router and I managed to get pino w/pino-pretty to work thru next-logger. I'm installing it from within instrumentation.ts
And in next-logger.config.js (it has to be a CommonJS module)
Hope it can help anyone. |
Beta Was this translation helpful? Give feedback.
-
I made it work by adding https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages |
Beta Was this translation helpful? Give feedback.
-
Adding |
Beta Was this translation helpful? Give feedback.
-
I am using "next": "14.1.0" here is logger
here is next.config.mjs
And it is working fine. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I was able to have it running like this: import pino from "pino";
import { IS_PRODUCTION, LOG_LEVEL } from "./environment";
import pinoPretty from "pino-pretty";
const logger = pino(
{
level: LOG_LEVEL,
},
!IS_PRODUCTION
? pinoPretty({
colorize: true,
})
: undefined,
); |
Beta Was this translation helpful? Give feedback.
-
I am using the recommended setup above (#46987 (comment)), but getting the same
Removing the pino Anyone experiencing the same? Is there a fix? |
Beta Was this translation helpful? Give feedback.
-
Finally, after a lot of trial-and-error I figured out how to make pino work in a Next.js monorepo (Turborepo), with transports, pino-pretty, and functions for EDIT:
The setup: @workspace/apps/appname/next.config.mjs
webpack(
config,
// { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack },
) {
config.resolve.fallback = {
...config.resolve.fallback,
fs: false, // the solution
};
// NOTE: Must use this instead of serverExternalPackages | serverComponentsExternalPackages
// because node:crypto for some reason only works below.
// Also, when the below is defined, it overrides the serverExternalPackages,
// so pino and pino-pretty must be included here.
config.externals.push({
'node:fs': 'commonjs fs',
'node:crypto': 'commonjs crypto',
argon2: 'argon2',
pino: 'pino',
'thread-stream': 'thread-stream',
'pino-worker': 'pino-worker',
'pino-file': 'pino-file',
'pino-pretty': 'pino-pretty',
});
return config;
}, @workspace/logger/pino.ts
import pino from 'pino';
const transport = pino.transport({
targets: [
{
target: 'pino/file',
options: { destination: `./logs/pino.log` },
level: 'warn',
},
{
target: `@<workspace>/logger/transports/pino-pretty-transport`,
options: {
colorize: true,
},
},
{
target: '@logtail/pino',
options: { sourceToken: process.env.LOGTAIL_SOURCE_TOKEN },
},
],
options: {
colorize: true,
},
});
export const logger = pino(
{
level: process.env.NODE_ENV === 'production' ? 'warn' : 'trace',
// redact,
// other options
},
transport,
); @workspace/logger/ transports/pino-pretty-transport.mjs
import pretty from 'pino-pretty';
function getPrettyStream(options) {
return pretty({
...options,
colorize: true,
translateTime: 'SYS:standard',
// ignore: 'pid,hostname',
// levelFirst: true,
// messageKey: 'msg',
// timestampKey: 'time',
// levelKey: 'level',
// messageFormat: (log, messageKey, levelLabel, { colors }) => {
// // `colors` is a Colorette object with colors enabled based on `colorize` option.
// const req = log['req'];
// const reqUrl = req instanceof Request ? req.url : null;
// return `This is a ${colors.red('colorized')}, custom message: ${log[messageKey]}, ${levelLabel} ${log['pid'] ? ` - ${log['pid']}` : ''} - ${reqUrl ? ` - url: ${reqUrl}` : ''}${log[''] ? ` - ${log['pid']}` : ''}`;
// },
customPrettifiers: {
// The argument for this function will be the same
// string that's at the start of the log-line by default:
timestamp: (timestamp) => `🕰 ${timestamp}`,
// The argument for the level-prettifier may vary depending
// on if the levelKey option is used or not.
// By default this will be the same numerics as the Pino default:
// level: (logLevel) => `LEVEL: ${logLevel}`,
// level provides additional data in `extras`:
// * label => derived level label string
// * labelColorized => derived level label string with colorette colors applied based on customColors and whether colors are supported
// level: (logLevel, key, log, { label, labelColorized, colors }) =>
// `LEVEL: ${logLevel} LABEL: ${label} COLORIZED LABEL: ${labelColorized}`,
hostname: (hostname) => `MY HOST: ${hostname}`,
pid: (pid) => `${pid}`,
name: (name, key, log, { colors }) => `${colors.blue(String(name))}`,
caller: (caller, key, log, { colors }) =>
`${colors.greenBright(String(caller))}`,
myCustomLogProp: (value, key, log, { colors }) =>
`My Prop -> ${colors.bold(String(value))} <--`,
},
});
}
export default (options) => {
const prettyStream = getPrettyStream(options);
return prettyStream;
}; @workspace/logger/ transports/package.json
{
"exports": {
"./pino": "./src/pino.ts",
"./transports/*": "./src/transports/*.mjs"
},
} @workspace/apps/appname/anyfile.ts
import { logger } from '@workspace/logger/pino';
logger.error(new Error('foobar')); I think that is it. It fully works on my end with that setup. |
Beta Was this translation helpful? Give feedback.
-
Just this line in the next.config will also work:
|
Beta Was this translation helpful? Give feedback.
-
Any update on this? Still not working for me, does a weird error for a missing "path" variable? |
Beta Was this translation helpful? Give feedback.
-
Summary
Hi! I've been using the pino json logging library with Next.js for a long time. And the pino-pretty extension for development.
When upgrading to Next 13 app folder I noticed that logging using pino with pino-pretty no longer works.
I've opened a pino-pretty issue about this. pinojs/pino-pretty#411 with a
Minimal reproduce repo https://github.com/scmx/next13-app-pino-pretty-worker-has-exited-minimal-reproduce
Error 1
error - node_modules/thread-stream/index.js (195:31) @ Worker.onWorkerExit error - uncaughtException: Error: the worker thread exited at Worker.onWorkerExit (webpack-internal:///(sc_server)/./node_modules/thread-stream/index.js:163:34) at Worker.emit (node:events:513:28) at Worker.[kOnExit] (node:internal/worker:287:10) at Worker..onexit (node:internal/worker:202:20) at Worker.callbackTrampoline (node:internal/async_hooks:130:17) null
Error 2
error - uncaughtException: Error: Cannot find module 'next13-app-pino-pretty/.next/server/app/lib/worker.js' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15) at Function.Module._load (node:internal/modules/cjs/loader:833:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at MessagePort. (node:internal/main/worker_thread:197:24) at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:736:20) at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28) { code: 'MODULE_NOT_FOUND', requireStack: [] }
It seems to be something regarding SWC bundling that causes pino-pretty to no longer work.
mcollina commented Mar 10, 2023
Additional information
No response
Example
https://github.com/scmx/next13-app-pino-pretty-worker-has-exited-minimal-reproduce
Beta Was this translation helpful? Give feedback.
All reactions