instrumentationHook
は先んじて next.config.mjs
で有効にしておく
instrumentation.ts
export const register = async () => {
if (process.env.NEXT_RUNTIME === "nodejs") {
/* eslint-disable @typescript-eslint/no-require-imports */
await require("pino");
await require("next-logger");
}
};
本番ではpino-prettyは有効にしない
next-logger.config.js
const pino = require("pino");
module.exports = {
logger: (defaultConfig) => {
const opts =
process.env.NODE_ENV === "production"
? {}
: {
transport: {
target: "pino-pretty",
options: {
colorized: true,
},
},
};
return pino({
...defaultConfig,
...opts,
});
},
};