@apexara/logger
v1.0.0
Published
A minimal Node.js logger with middleware support
Keywords
Readme
@apexara/logger
A minimal Node.js logger with middleware support. Provides info, error, and debug logging, ANSI color output, and duplicate error suppression.
Install
npm install @apexara/loggerQuick start
import logger from '@apexara/logger';
logger.info('Server started');
logger.error('Connection failed', 'db');
logger.debug({ port: 8080 });The module also assigns a global instance:
import '@apexara/logger';
globalThis.logger.info('Using the global logger');API
logger.info(message, options?)
logger.info('Ready', { color: '36', tags: ['startup'] });logger.error(message, source, options?)
logger.error('Timeout', 'payments', { color: '33', meta: { timeoutMs: 5000 } });logger.debug(object, inspectOptions?, options?)
logger.debug({ config: { retries: 3 } }, { depth: 4 });Call options
color: ANSI color code as a string, e.g.'31'(red),'32'(green),'33'(yellow),'37'(white).tags: string array, intended for middleware.meta: free-form key/value data, intended for middleware.
Timestamps and formatting
- Timestamps are formatted in 24-hour time with the
Europe/Sofiatimezone. - Output is ANSI-colored; disable colors by running your process without a color-capable terminal.
Middleware
Middlewares can enrich or alter log behavior. They receive a mutable LoggerOptions object and a next function.
import logger, { LoggerMiddleware } from '@apexara/logger';
const addTag: LoggerMiddleware = async (options, next) => {
options.tags = [...(options.tags ?? []), 'api'];
await next();
};
logger.addMiddleware(addTag);Level-specific middleware:
logger.addInfoMidldeware(async (options, next) => {
options.color = options.color ?? '32';
await next();
});
logger.addDebugMidldeware(async (options, next) => {
options.meta = { ...(options.meta ?? {}), debug: true };
await next();
});
logger.addErrorMidldeware(async (options, next) => {
options.color = options.color ?? '31';
await next();
});Note: tags and meta are not rendered by the default logger output; they are intended for middleware or external handlers.
Duplicate error suppression
Repeated error logs with the same source and message are suppressed for 60 seconds by default. A summary is printed after the interval.
logger.suppressionIntervalMs = 10_000;Types
The package exports the following TypeScript types:
LogTypeLoggerOptionsLogCallOptionsLoggerMiddleware
License
ISC
