Custom logger for Server Errors #54719
-
|
Hello, With the new app folder Server Side errors are captured and logged in the backend and a digest is sent to the frontend to be able to match the corresponding error in Server Logs: https://nextjs.org/docs/app/building-your-application/routing/error-handling#securing-sensitive-error-information. Currently, the error is logged in the backend as plain text, but we would like to capture the error to be able to send it to our elastic stack. Even logging it to the console but with JSON format would be enough for us, as we automatically capture those logs and save them. Is it possible to achieve this kind of behaviour? Can we change the Server Side log? Can we capture the Server Side error? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Hey @Marcosld, our team had a very similar issue until I discovered the experimental.instrumentationHook. You can use this hook in combination with next-logger to console.log JSON rather than plain text. The JSON object even contains the log level and stack trace. Steps1. Install package:2. (Option 1) - using standard outputSimply add NODE_OPTIONS to start command: 2. (Option 2) - using output: 'standalone'If you are specifying output: 'standalone' in your next.config you will need to add an instrumentationHook to ensure that the next-logger module is available at runtime. Without it you will run into the following issue. Note: you do not need to specify the NODE_OPTIONS argument when using the instrumentationHook. Set experimental.instrumentationHook to true // next.config.js
module.exports = {
...
experimental: {
...
instrumentationHook: true,
},Create instrumentation.js file 3. OutputThis should change the logging output to something like this: If you need to customize this output like rename a field or flatten the child object you can add a next-logger.config.js and create a custom logger using Pino. |
Beta Was this translation helpful? Give feedback.
Hey @Marcosld, our team had a very similar issue until I discovered the experimental.instrumentationHook. You can use this hook in combination with next-logger to console.log JSON rather than plain text. The JSON object even contains the log level and stack trace.
Steps
1. Install package:
2. (Option 1) - using standard output
Simply add NODE_OPTIONS to start command:
2. (Option 2) - using output: 'standalone'
If you are specifying output: 'standalone' in your next.config you will need to add an instrumentationHook to ensure that the next-logger module is available at runtime. Without it you will run into the following issue…