@wjunt/node-logger
v1.1.3
Published
Another simple logging for Node.js
Downloads
67
Readme
@wjunt/node-logger
Another simple logging for Node.js, which implements loglevel
Strongly recommend to use TypeScript
Usage
Note: Node.js>=8 is required.
ES6 module
import logger from '@wjunt/node-logger';
logger.info('info message');
CommonJS
const logger = require('@wjunt/node-logger').default;
logger.info('info message');
Documentation
The logger API is same as loglevel. Visit loglevel documentation for more details.
Basic logging method
logger.trace(...msg: any[]): void;
logger.debug(...msg: any[]): void;
logger.info(...msg: any[]): void;
logger.warn(...msg: any[]): void;
logger.error(...msg: any[]): void;
Get new logger
Use getLogger
to get new logger.
Example usage:
import logger, { Logger } from '@wjunt/node-logger';
const globalLogger = Logger.getLogger();
const logger1 = Logger.getLogger('new');
const logger2 = logger.getLogger('new');
const logger3 = logger1.getLogger('new');
When called with same name, getLogger
method will return same instance of Logger. In this example, logger
and globalLogger
are same. Also logger1
, logger2
and logger3
.
Logger options
level?: number | string
A valid log level.noDefaultWriter?: boolean
If set true, the default console writer will not be added.timeFormat?: string
Define how time formatted, passed to writer constructor.traceFunction?: Function
Base function to capture call stack,logger.trace
by default.
Use logger writer
A ConsoleLoggerWriter
is added by default, which write log message to stdout/stderr.
Use logger.addWriter(Writer: LoggerWriterConstructor, options?: LoggerWriterOptions): this
to add customized writer.
Example usage:
import { FileLoggerWriter, Logger } from '@wjunt/node-logger';
const logger = Logger.getLogger('clean').addWriter(FileLoggerWriter);
logger.info('some message');
Here we get a logger with name 'clean', which output message to both console and file. FileLoggerWriter enables log-rotate by default. See more details at FileLoggerWriter
FileLoggerWriter
Output message to file.
Options
dirname?: string
Where log files stored, './logs' by default.rotateFormat?: false | string
Format of rotated log files, 'YYYYMMDD' by default; if setfalse
rotating will be disabled.timeFormat?: string
YYYY-MM-DD HH:mm:ss.SSS
by default.
ConsoleLoggerWriter
Output message to console.
Options
timeFormat?: string
HH:mm:ss.SSS
by default.