eslint-plugin-pino-logger
v1.0.2
Published
> 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. Repo
Maintainers
Readme
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, andconsole.warnwith a composable logger pattern, with auto-import and framework-aware fixes. Reports all otherconsole.*usage as problems (likeno-console).
Features
- Reports all
console.*usage as problems (likeno-console). - Auto-fixes (and quick fixes) for
console.log,console.info,console.error, andconsole.warntologger.info,logger.error, andlogger.warn. - Auto-inserts
const logger = usePinoLogger()if not present (Nuxt only). - Auto-inserts the import for
usePinoLoggerif not in a Nuxt project (Nuxt auto-imports composables). - Detects Nuxt projects by checking for
nuxt.config.tsornuxt.config.jsin 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-loggerUsage
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 (likeno-console). - When you use
console.log,console.info,console.error, orconsole.warn, the rule will:- Replace it with
logger.info,logger.error, orlogger.warn. - Insert
const logger = usePinoLogger()at the top of the script if not present. - Insert the import for
usePinoLoggerif not in a Nuxt project (Nuxt auto-imports composables).
- Replace it with
- 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.tsornuxt.config.jsin 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-fixedAfter (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-fixedAdvanced 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-consolerule 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
