1.0.8 • Published 5 years ago

digio-logger v1.0.8

Weekly downloads
27
License
-
Repository
-
Last release
5 years ago

digio-logger

This is just an opinionated winston v3 logger wrapper. There are several ways of using this logger. Just choose the best it suits you.

One of many uses (default logger)

const Log = require('digio-logger')

// This will create a default logger using the configuration 
// in config.js inside this package
const logger = Log.logger();

// Log an info message in a file called logs.log
logger.info('Sending messages to log', {
    extraData: 'extra extra!!',
    moreContextData: 'this is so funny!'
});

// Same as the first one
Log.log(Log.levels.info, 'test', {
    extraData: 'extra extra!!',
    moreContextData: 'this is so funny!'
});

Every time you make use of Log.logger({/*winstonSettings*/}, {/*logSettings*/}) you are defining a custom log and its instance is used for every next log, until you add a new one. If you don't add any settings to the logger it will use the default settings provided by config.js, placed into the package folder.

Opinionated stuff

In order to identify each log message properly from any log processor, we do believe in logger tagging with "actions" and "logTypes". Actions are defined constants strings that give a log some meaning. Log Types basically add more context information so you can group all actions under the same log type.

By default digio-logger make use of JSON logs, which basically means that it creates log files where each line is a JSON object.

Custom logger example (opinionated)

const Log = require('digio-logger')
const transports = Log.winstonTransports;
            
// Create a custom logger
const logger2 = Log.logger({
    transports: [
        new transports.Console({
            // Define the logs format
            level: 'debug',
            format: Log.templates.console
        })
    ]
},{
    channel: 'logger2',
    filename: 'another_log_file.log',
    path: './logs/'
});

// Even though we're not using 'logger2' we use the same logger 
// as it was set as the default logger in the lines before
Log.info(Log.actions.worker.timeout, 'logtype',{
    extra: 'extra info del error',
    whatever: 'whatever extra info'
},'This is an irrelevant message');
    

In this case we can add a second set of settings to customize the log name and a channel tag, this will be added to the base log the logger produces. By default the path is empty, so it will set the log into the root folder of your index.js file if there is no custom transport defined.

{
    level: level,
    msg: msg,
    action: action,
    logType: logType,
    context: context,
    channel: defaultConfig.log.channel || 'default-channel'
}

Useful resources

const Log = require('digio-logger')

// You can find useful data in the form of constants inside Log

// These are the levels supported by the logger
const levels = Log.levels;
// Here you can find predifined and formated templates for your logs
const templates = Log.templates;   
// These are useful to add new transports
const transports = Log.winstonTransports;
// And as you can read, this is the default config
cosnt defaultConfig = Log.config;

// You can also set 2 default loggers and forget about console.log
Log.logger({
    transports: [
        new Log.winstonTransports.File({
            filename: 'logs2.log',
            format: Log.templates.file
        }),
        new Log.winstonTransports.Console({
            format: Log.templates.console
        })
    ]
});

// This will log in two diferents places at once
Log.info(Log.actions.worker.timeout,'logtype',{contextData:'data'})

Remove the current logger

When a logger is defined, then it is used forever until a new transport is set for the logger. To start clean, we can use the following:

Log.removeCurrentLogger();

This will destroy the current logger, so a new empty logger will use the default configuration. This is usefull when we use the second set of configuration options.

Check an example in test.js, logger4.

Keep in mind, that destroying the current object, and using any log method afterwards will lead to an error or exception.

Take a look to the test.js file

=> node test.js // inside this package

And take a moment to see how it works

It may continue....

1.0.8

5 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago