https://github.com/yassaaatu/eslint-plugin-pino-logger
An enchaned no-console rule that auto replaces console statements with pino logger
https://github.com/yassaaatu/eslint-plugin-pino-logger
es-lint es-lint-config es-linter eslint eslint-plugin eslint-rules eslintplugin pino pino-logs
Last synced: 3 months ago
JSON representation
An enchaned no-console rule that auto replaces console statements with pino logger
- Host: GitHub
- URL: https://github.com/yassaaatu/eslint-plugin-pino-logger
- Owner: YassaaaTU
- License: mit
- Created: 2025-06-10T19:04:07.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2025-06-10T19:23:52.000Z (4 months ago)
- Last Synced: 2025-07-11T08:06:07.007Z (3 months ago)
- Topics: es-lint, es-lint-config, es-linter, eslint, eslint-plugin, eslint-rules, eslintplugin, pino, pino-logs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/eslint-plugin-pino-logger
- Size: 4.88 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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`, 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
```sh
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:
```js
// .eslintrc.js or eslint.config.mjs
{
plugins: [
'pino-logger'
],
rules: {
'pino-logger/no-console-to-pino-logger': 'warn' // or 'error'
}
}
```### With Options
```js
'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:**
```js
console.log('Hello')
console.info('Info!')
console.error('Oops!')
console.table(data)
```**After (Nuxt):**
```js
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):**
```js
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