@mimik/eslint-plugin-logger
v1.0.3
Published
Validation of logger call parameters for mimik services
Readme
@mimik/eslint-plugin-logger
ESLint plugin that validates logger call arguments for mimik services.
Installation
npm install --save-dev @mimik/eslint-plugin-loggerUsage
Add the plugin to your eslint.config.js:
import loggerPlugin from '@mimik/eslint-plugin-logger';
export default [
{
plugins: { logger: loggerPlugin },
rules: {
'logger/validate-logger-args': 'error',
},
},
];Rule: validate-logger-args
Validates that logger calls (on default-imported modules) use the correct argument signatures.
Valid Signatures
All three access patterns are supported:
import logger from '@mimik/sumologic-winston-logger';
// Dot notation
logger.info('User logged in', correlationId);
// Bracket notation with string literal
logger['info']('User logged in', correlationId);
// Bracket notation with variable (any variable name)
logger[level]('User logged in', correlationId);
logger[severity]('User logged in', correlationId);The four argument signatures:
// (message, correlationId)
logger.info('User logged in', correlationId);
// (message, meta, correlationId)
logger.info('User logged in', { userId: 123 }, correlationId);
// (message, correlationId, options)
logger.info('User logged in', correlationId, { type: 'audit' });
// (message, meta, correlationId, options)
logger.info('User logged in', { userId: 123 }, correlationId, { type: 'audit' });Arguments
| Argument | Type | Required | Description |
|---|---|---|---|
| message | string | Yes | Log message |
| meta | object | No | Metadata object with additional context |
| correlationId | string | Yes | Correlation ID for request tracing |
| options | object | No | Options object, must contain a type string property |
Supported Log Levels
error, warn, info, verbose, debug, silly
What Gets Checked
- The rule only applies to default imports (e.g.
import logger from '...') - Named imports (
import { logger } from '...') are ignored - Supports dot notation (
logger.info), bracket notation with a string literal (logger['info']), and bracket notation with a variable (logger[level]) - For bracket notation with a string literal, the value must be a recognized log level
- For bracket notation with a variable, the call is always validated (the variable is assumed to hold a valid level)
- Minimum 2 arguments, maximum 4
messagemust be a string (when passed as a literal)correlationIdmust be a string (when passed as a literal)optionsobject must contain atypeproperty with a string value- When 2 arguments are passed and the 2nd is an object or array literal, it is treated as
(message, meta)with a missingcorrelationId - When 3 arguments are passed, the 2nd argument type determines the signature:
- Object literal →
(message, meta, correlationId) - Otherwise →
(message, correlationId, options)
- Object literal →
API Reference
The plugin exports a single object with the following shape:
{
rules: {
'validate-logger-args': { meta, create(context) }
}
}Error Messages
| Message ID | Description |
|---|---|
| tooFewArguments | Fewer than 2 arguments passed |
| tooManyArguments | More than 4 arguments passed |
| messageNotString | First argument is a non-string literal |
| correlationIdNotString | correlationId argument is a non-string literal |
| missingCorrelationId | 2nd argument is an object or array — correlationId is missing |
| optionsNotObject | Options argument is a literal instead of an object |
| optionsMissingType | Options object does not contain a type property |
| optionsTypeNotString | Options type property is a non-string literal |
License
MIT
