2.1.9 • Published 4 years ago

feel-logger v2.1.9

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

Feel Logger

This is my default configuration for winston logger.

NPM

Table of contents

Motivation

Winston is an incredible and versatile logging library. To simplify the configuration process and keep the logs consistent across projects, i decided to create this package to easily setup winston with my formats and configurations.

This package has two default transports: Console and Dayle Rotate File.

Quick Start

You just need to import Logger and call the init method and a Console logger will be created.

import { Logger } from 'feel-logger';

Logger.init();
Logger.info('My Awesome Logger!');
// output: 2020-07-12 13:43:05 [Application] INFO: My Awesome Logger!

To activate the Daily Rotate File just pass a configuration object to init method.

import { Logger } from 'feel-logger';

Logger.init({ file: true });
Logger.info('My Awesome Logger!');

This will create a /logs folder in the project root and store there the logs files.

Output Format

The output format is composed by a timestamp, a identifier label, the log level, the log message and the associated metadata.

 YYYY-MM-DD HH:MM:SS [label] LEVEL: message Meta: ...

Configuration

The configurations accepted by the Logger are:

propertytypedefaultdescription
namestringApplicationthis will be the label
levelstringinfothe log level
exitOnErrorbooleanfalse
handleExceptionbooleanfalse
transportsWinston.Transports[]Console, Daily Rotate Filecustom transports to be add to the Logger
filebooleanfalseactive Dayle Rotate File transport
fileOptionsDailyRotateFileTransportOptionsnulloverrides default transport options
consoleWinstonLoggerOptionsnulloverrides default transport options

Dynamic Initialization

To make the Logger instance static across the application you have to create a logger.options file. When the lib is loaded by node it will be search for a configuration file (.js or .ts) in the project. This dynamic initialization overrides init method.

    +-- root
    |   +-- src
    |   |   +-- configuration.options.[ts|js]
    |   +-- package.json

The configuration file accepts the same object as the init method, but make sure that your logger.options.[ts|js] is inside a src folder under the root directory and follow the conventions:

/* logger.options.js */
module.exports = {
    file: true,
};
/* logger.options.ts */
export default {
    file: true,
};

Now you can import Logger in any file and use without pass through injection.

/* AccountController */
import { Logger } from 'feel-logger';

export class AwesomeClass {
    AwesomeMethod() {
        Logger.info('Awesome Method Called');
    }
}

Logger Interface

The following is the methods for logging:

interface LoggerOperations {
    log: (level: string, message: string) => void;
    info: (message: string, ...meta: unknown[]) => void;
    warning: (message: string, ...meta: unknown[]) => void;
    error: (message: string, ...meta: unknown[]) => void;
    debug: (message: string, ...meta: unknown[]) => void;
}

Logging Context

If you want to add a context information to the output log you can pass a special metadata called context and it will be add to the label part of the log.

import { Logger } from 'feel-logger';

Logger.info('My Awesome Logger', { context: 'MyContext' });
// output: 2020-07-12 14:42:29 [Application - MyContext] INFO: My Awesome Logger

Logger Instance

If you initialize your Logger dynamically, you can create instances of the logger to set a default context.

import { Logger } from 'feel-logger';

const logger = Logger.createLoggerInstance('MyDefaultContext');
logger.info('My Awesome Logger');
// output: 2020-07-12 14:47:31 [Application - MyDefaultContext] INFO: My Awesome Logger

You can pass a class type as well.

import { Logger } from 'feel-logger';

export class MyAwesomeClass {
    logger = Logger.createLoggerInstance(MyAwesomeClass);

    log() {
        this.logger.info('My Awesome Logger');
    }
}

new MyAwesomeClass().log();
// output: 2020-07-12 14:47:31 [Application - MyAwesomeClass] INFO: My Awesome Logger

Winston Instance

You can get the winston instance object by calling the method:

import { Logger } from 'feel-logger';
const winston = Logger.getWinstonInstance();
2.1.9

4 years ago

2.1.8

4 years ago

2.1.7

4 years ago

2.1.4

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago