Skip to content

YassaaaTU/eslint-plugin-pino-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

eslint-plugin-pino-logger

ESLint rule to enforce structured logging with Pino in Nuxt, Next, and general JS/TS projects. Replaces console.log, console.info, console.error, and console.warn with a composable logger pattern, with auto-import and framework-aware fixes. Reports all other console.* usage as problems (like no-console).

Features

  • Reports all console.* usage as problems (like no-console).
  • Auto-fixes (and quick fixes) for console.log, console.info, console.error, and console.warn to logger.info, logger.error, and logger.warn.
  • Auto-inserts const logger = usePinoLogger() if not present (Nuxt only).
  • Auto-inserts the import for usePinoLogger if not in a Nuxt project (Nuxt auto-imports composables).
  • Detects Nuxt projects by checking for nuxt.config.ts or nuxt.config.js in the project root.
  • Configurable logger variable name, import path, and import name for compatibility with any project structure.
  • Works in Nuxt 3, Next.js, and any JS/TS project (tested in Nuxt, logic is framework-agnostic).
  • Quick fix available in editors (e.g., VSCode).

Installation

npm install --save-dev eslint-plugin-pino-logger
# or
yarn add -D eslint-plugin-pino-logger
# or
bun add -d eslint-plugin-pino-logger

Usage

Add to your ESLint config:

// .eslintrc.js or eslint.config.mjs
{
  plugins: [
    'pino-logger'
  ],
  rules: {
    'pino-logger/no-console-to-pino-logger': 'warn' // or 'error'
  }
}

With Options

'rules': {
  'pino-logger/no-console-to-pino-logger': ['warn', {
    loggerVar: 'logger', // default
    importPath: '~/composables/usePinoLogger', // default for Nuxt
    importName: 'usePinoLogger' // default
  }]
}
  • loggerVar: The variable name to use for the logger (default: logger).
  • importPath: The import path for the logger composable/hook (default: ~/composables/usePinoLogger).
  • importName: The import name for the logger composable/hook (default: usePinoLogger).

How It Works

  • All console.* usage is reported as a problem (like no-console).
  • When you use console.log, console.info, console.error, or console.warn, the rule will:
    • Replace it with logger.info, logger.error, or logger.warn.
    • Insert const logger = usePinoLogger() at the top of the script if not present.
    • Insert the import for usePinoLogger if not in a Nuxt project (Nuxt auto-imports composables).
  • For all other console.* methods (e.g., console.debug, console.table), the rule reports a problem but does not auto-fix.
  • Nuxt detection is done by checking for nuxt.config.ts or nuxt.config.js in the project root.

Example

Before:

console.log('Hello')
console.info('Info!')
console.error('Oops!')
console.table(data)

After (Nuxt):

const logger = usePinoLogger()

logger.info('Hello')
logger.info('Info!')
logger.error('Oops!')
console.table(data) // still reported as a problem, but not auto-fixed

After (Next.js or other):

import { usePinoLogger } from '@/lib/usePinoLogger'
const logger = usePinoLogger()

logger.info('Hello')
logger.info('Info!')
logger.error('Oops!')
console.table(data) // still reported as a problem, but not auto-fixed

Advanced no-console Replacement

  • This rule is a drop-in, advanced replacement for no-console.
  • It reports all console.* usage, but only auto-fixes the most common logging methods to Pino logger equivalents.
  • You should disable the built-in no-console rule when using this plugin.

Framework Support

  • Nuxt 3: Auto-imports composables, so only the logger variable is inserted. → Tested and works.
  • Next.js/Other: Both import and variable are inserted. → Not tested, but should work as it uses the same logic.

License

MIT