1.1.3 • Published 1 year ago

handy-logger v1.1.3

Weekly downloads
5
License
MIT
Repository
github
Last release
1 year ago

Handy Logger

Version Build Status Downloads License Known Vulnerabilities

An easy log handler for Node.js application which is built on top of popular logger library winston.

Installation

npm i handy-logger

Get started

Import library

import { HandyLogger, HandyLoggerBase } from 'handy-logger';

HandyLogger

HandyLogger class creates a new logger instance with default or custom configuration.

Create logger with default configuration

const HandyLogger = new HandyLogger();

or with custom configuration

const myLogger = new HandyLogger(opts);

opts config has following properties -

OptionTypeDefaultDescription
titlestringundefinedApplication title can be provided which will be prefixed (or customized using logDataStringCustomFormat method) in log messages.
levelLogLevels | stringsillyLog level depending which different kind of log will be written. Read more here.
exitOnErrorbooleantrueIf false, handled exceptions will not cause process.exit. Read more here.
transportsHandyLoggerTransport{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))undefinedCustom 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)undefinedCustom 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.

HandyLoggerBase

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.


How to use

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

Todos

  • Add custom log levels
  • Add colorize option

License

MIT