@goldix.org/loggers

    0.0.9 • Public • Published

    Install modules:

    npm i --save @goldix.org/loggers @goldix.org/logger-winston-console @goldix.org/logger-winston-elastic
    

    Example with multiple loggers:

    const { Loggers }               = require('@goldix.org/loggers');
    const { LoggerWinstonConsole }  = require('@goldix.org/logger-winston-console');
    const { LoggerWinstonElastic }  = require('@goldix.org/logger-winston-elastic');
    
    Loggers.define('LoggerWinstonConsole', LoggerWinstonConsole);
    Loggers.define('LoggerWinstonElastic', LoggerWinstonElastic);
    
    const pools = {
        default: {
          LoggerWinstonElastic: {
            enabled: false,
            //@see https://github.com/vanthome/winston-elasticsearch
            level: 'info',
            indexPrefix: 'example',
            clientOpts: {
              //@see https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html
              host: 'http://localhost:9200'
            }
          },
          LoggerWinstonConsole: {
            enabled: true,
            //@see https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport
            level: 'info',
            colorize: true,
            timestamp: true,
            prettyPrint: true
          }
        }
    };
    
    function transformMessage(level, message, payload, options) {
        message = `${options && options.prefix} ${level} ${message}`;
        if(options.someParameter === 1 && options.loggerClass === 'LoggerWinstonElastic') {
            //Disable write this message to elastic
            options.skip = true;
        }
        if(options.options.someParameter2 === true) {
            payload.memory = process.memoryUsage();
        }
        return {
            level,
            message,
            payload,
            options
        }
    }
    
    const loggers = new Loggers({ pools, transformMessage });
    
    const dbLogger = loggers.get('default', { prefix: 'DB', someParameter: 1 })
    
    dbLogger.debug('Users count 12', null, { someParameter2: true });
    
    const httpLogger = loggers.get('default', { transformMessage: (level, message, payload, options) => {
    let req = payload.req;
    if(req) {
        payload.ip = req.ip;
        delete payload.req;
        if(req.originalUrl === '/favicon.ico') {
            options.skip = true;
        }
    }
    return {
        level,
        message: `HTTP ${req.method} ${req.originalUrl}`,
        payload,
        options
    }
    })
    
    app.use((req, res,  next) => {
    
        next();
    })

    Install

    npm i @goldix.org/loggers

    DownloadsWeekly Downloads

    1

    Version

    0.0.9

    License

    ISC

    Unpacked Size

    5.74 kB

    Total Files

    3

    Last publish

    Collaborators

    • goldix.org