Skip to content

Frans06/pino-next

Repository files navigation

pino-next

A opinionated, powerful and flexible logging solution for Next.js applications using Pino under the hood. Highly inspired by next-logger

Features

  • 📊 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

Installation

npm install pino-next
# or
yarn add pino-next
# or
pnpm add pino-next

Patch 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,
    );
  }
}

Basic Usage

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");

API Reference

createLogger(config?)

Creates a new logger instance.

  • config - Optional configuration object:
    • level - Log level (default: 'info' in development, 'warn' in production)
    • isProduction - Force production mode
    • prettify - Enable pretty printing
    • forceArgs - Add hook args
  • isInstrumented - Whether to add instrumentation hooks (default: false)

patchWithLogger(childName, mapper, patched?, config?)

Patches a console object with logger methods.

  • childName - Name for the child logger
  • patched - Console object to patch (default: global console)
  • mapper - Method mapping (default: consoleToPino)
  • config - Same as create logger

logger

A pre-configured default logger instance.

Development

Please let me know if you have any requirement or issues. PRs are also welcome.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors