ESLint rule to enforce structured logging with Pino in Nuxt, Next, and general JS/TS projects. Replaces
console.log
,console.info
,console.error
, andconsole.warn
with a composable logger pattern, with auto-import and framework-aware fixes. Reports all otherconsole.*
usage as problems (likeno-console
).
- Reports all
console.*
usage as problems (likeno-console
). - Auto-fixes (and quick fixes) for
console.log
,console.info
,console.error
, andconsole.warn
tologger.info
,logger.error
, andlogger.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
ornuxt.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).
npm install --save-dev eslint-plugin-pino-logger
# or
yarn add -D eslint-plugin-pino-logger
# or
bun add -d eslint-plugin-pino-logger
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'
}
}
'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
).
- 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
usePinoLogger
if 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.ts
ornuxt.config.js
in the project root.
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
- 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.
- 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.
MIT