A opinionated, powerful and flexible logging solution for Next.js applications using Pino under the hood. Highly inspired by next-logger
- 📊 Built on top of the powerful Pino logger
- 🌈 Pretty logging for development, efficient JSON logging for production
- 🔄 Seamless Console API compatibility
- 🛡️ Graceful fallback to console if initialization fails
- 🚀 Optimized for Next.js applications
- 📝 Full TypeScript support
npm install pino-next
# or
yarn add pino-next
# or
pnpm add pino-nextPatch global on instrumentation.ts file on the project root
- Global patcher:
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
const patcher = await import("pino-next");
patcher.patchAllConsoleLogger({ forceArgs: true });
}
}- Next Only patcher:
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
const patcher = await import("pino-next");
patcher.patchAllConsoleLogger(forceArgs: true);
}
}- Custom logger patcher:
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
const patcher = await import("pino-next");
patcher.patchWithLogger(
"customLoggerName",
customeAdapter,
customLogger,
loggerOptions,
);
}
}import { createLogger } from "pino-next";
// Create a logger with default settings
const logger = createLogger();
// Log messages with different levels
logger.info("Application started");
logger.debug({ userId: 123 }, "User logged in");
logger.warn("Resource is running low");
logger.error(new Error("Something went wrong"), "Operation failed");
// Create a child logger with additional context
const userLogger = logger.child({ component: "UserService" });
userLogger.info({ userId: 456 }, "User profile updated");Creates a new logger instance.
config- Optional configuration object:level- Log level (default: 'info' in development, 'warn' in production)isProduction- Force production modeprettify- Enable pretty printingforceArgs- Add hook args
isInstrumented- Whether to add instrumentation hooks (default: false)
Patches a console object with logger methods.
childName- Name for the child loggerpatched- Console object to patch (default: global console)mapper- Method mapping (default: consoleToPino)config- Same as create logger
A pre-configured default logger instance.
Please let me know if you have any requirement or issues. PRs are also welcome.
MIT