node-slogger

    2.2.1 • Public • Published

    node-slogger

    build status Test coverage David deps node version

    NPM
    A wrapper of logger package , let you wirte log easy.

    Install

    npm install node-slogger

    How to use

    The fist demo

    The slogger t will print log to console with prefix of time string and log level, for example slogger.debug('debug') will print Thu Dec 15 2016 11:04:50 GMT+0800 (CST) [DEBUG] debug . Different function will be printed with different color.

    var slogger = require('../index');
    
    slogger.debug('debug');
    slogger.info('info');
    slogger.trace('trace');
    slogger.warn('warn');
    slogger.error('error');

    Saving log to file

    const path = require('path');
    var slogger = require('../index');
    
    slogger.init({
        logFiles:[
            {category:'error',filename:path.join(__dirname , './log/error.log')}
        ]
    });
    
    slogger.debug('debug');//only print to console
    slogger.info('info');//only print to console
    slogger.trace('trace');//only print to console
    slogger.warn('warn');//only print to console
    slogger.error('error');//print to console and write to the file

    The code of file_test.js

    Saving log to kafka

    Slogger support sending log to kafka. When you use the function, you have to add queue-schedule and node-rdkafka to your dependecies manual.

    const Kafka = require('node-rdkafka');
    const {RdKafkaProducer} = require('queue-schedule');
    var slogger = require('node-slogger');
    // const {LOGSTASH_HOST,LOGSTASH_PORT} = process.env;
    const VALUE_FLUSH_INTERVAL = 100;
    const producerRd = new Kafka.HighLevelProducer({
        'metadata.broker.list': process.env.KAFKA_HOST,
        'linger.ms':0.1,
        'queue.buffering.max.ms': 500,
        'queue.buffering.max.messages':1000,
    });
    
    const producer = new RdKafkaProducer({
        name : 'slogger.warn',
        topic: 'topic.test',
        producer:producerRd,
        delayInterval: 500
    });
    
    slogger = slogger.init({
        flushInterval:VALUE_FLUSH_INTERVAL,
        producers:[{
            category: 'warn',
            producer
        }]
    });
    slogger.warn('the warnning message which will be sended to kafka serveer');

    Printing the log to console delayed with fixed interval.

    If you wanna you project run with high performance, printing to console frequently will cost a lot of cpu time. So slogger provide a feature of printing log in delay time with a fixed interval.

    var slogger = require('../index');
    slogger.init({flushInterval:500});
    slogger.debug('this is a delay log');//it will show after 500ms

    Setting the log level

    You can set the level of log , just use the option of level. For example we set it to warn:

    var slogger = require('../index').init({level:'warn'});
    
    slogger.debug('debug');
    slogger.info('info');
    slogger.trace('trace');
    slogger.warn('warn');
    slogger.error('error');

    The code of level_test.js
    Only the warn adn error log will be printed as we set the level option to warn.

    API

    See the document of api.

    Known issues

    1. Not showing log on VS Code's debug pannel

    You should modify the launch.json and add the parameter console with the value integratedTerminal. This is an example:

    {
        "type": "node",
        "request": "launch",
        "name": "Mocha Tests",
        "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
        "args": [
            "-u",
            "tdd",
            "--timeout",
            "999999",
            "--colors",
            "${workspaceFolder}/src/test/mocha"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "openOnSessionStart"
    },

    License

    MIT

    Keywords

    Install

    npm i node-slogger

    DownloadsWeekly Downloads

    5

    Version

    2.2.1

    License

    MIT

    Unpacked Size

    42.6 kB

    Total Files

    31

    Last publish

    Collaborators

    • yunnysunny