An easy log handler for Node.js application which is built on top of popular logger library winston
.
npm i handy-logger
import { HandyLogger, HandyLoggerBase } from 'handy-logger';
HandyLogger class creates a new logger instance with default or custom configuration.
const HandyLogger = new HandyLogger();
const myLogger = new HandyLogger(opts);
opts
config has following properties -
Option | Type | Default | Description |
---|---|---|---|
title | string | undefined |
Application title can be provided which will be prefixed (or customized using logDataStringCustomFormat method) in log messages. |
level | LogLevels | string | silly |
Log level depending which different kind of log will be written. Read more here. |
exitOnError | boolean | true |
If false, handled exceptions will not cause process.exit . Read more here. |
transports | HandyLoggerTransport | {console: [{}]} |
A transport is essentially a storage device for your logs. Like winston, handy-logger accepts multiple transports such as console , file etc. Read more here. |
timeStampFormat | (string | (() => string)) | undefined |
Custom timestamp format. It can be a string accepted by fetcha module or a method that returns formatted date. |
logDataStringCustomFormat | ((timestamp: string, level: string, title: string, message: string) => string) | undefined |
Custom log message format. You can pass a method with timestamp, level, title and message and return a formatted string as you want. |
Transport options (HandyLoggerTransport
) are as follows -
- file
Array<FileTransportOptions>
- Winston file transport options for logging in files. - rotate
Array<DailyRotateFileTransportOptions>
- Winston daily rotate file transport options for logging in file with rotational logics. - console
Array<ConsoleTransportOptions>
- Winston console transport options for logging in console. - http
Array<HttpTransportOptions>
- Winston http transport options for logging via HTTP. - stream
Array<StreamTransportOptions>
- Winston stream transport options for logging via stream.
This can be used to set type of the logger when we are calling getLogger()
. The type actually refers to winston.logger
, so you should be able to access all methods that winston.logger
provides.
import { HandyLogger, HandyLoggerBase, LogLevels } from 'handy-logger';
const loggerObj: HandyLogger = new HandyLogger({
title: 'My Awesome App',
level: LogLevels.Info,
transports: {
console: [
{
handleExceptions: false,
},
],
file: [
{
filename: 'app-error.log',
level: LogLevels.Error,
},
{
filename: 'app-warning.log',
level: LogLevels.Warn,
},
],
},
timeStampFormat: () => {
return new Date().toUTCString();
},
logDataStringCustomFormat: (ts, lv, title, msg) => {
return `APP: ${title} :: ${ts} :: [${lv}] :: ${msg}`;
},
});
const logger: HandyLoggerBase = loggerObj.getLogger();
logger.info('sunny day');
logger.warn('foo bar');
logger.error('err message');
This will log
APP: My Awesome App :: Wed, 05 Feb 2020 15:38:08 GMT :: [info] :: sunny day
APP: My Awesome App :: Wed, 05 Feb 2020 15:38:08 GMT :: [warn] :: foo bar
APP: My Awesome App :: Wed, 05 Feb 2020 15:38:08 GMT :: [error] :: err message
- Add custom log levels
- Add
colorize
option
MIT