0.0.3 • Published 7 years ago

g-logger v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

g-logger

npm version

logo

A logging library for node.js.

    • Logs to console
    • Logs to file
    • Log file auto splitting
    • Get log files to JSON
    • Delete log files

Install

npm g-logger -g

Usage

var logger = require('g-logger');
var log = new logger();

// String
log.info('some info message ...');

// String format
log.info('hello %s', 'world');

// Object
log.info({item: 'test'});

// Object and string
log.info({item: 'test'}, 'hello %s', 'world');

// Error Object
log.error(new Error('test'), 'error is about %s', 'test');

Config

var logger = require('g-logger'),
    process = require("process"),
    chalk = require('chalk');

// 1. Create config
// The following is the default
var config =  {
        levels: ["trace", "debug", "info", "warn", "error", "fatal", "done", "fail"],
        enFile: true, // enable save logs to file
        enConsole: true, // enable logs console
        // format is an arraylist to serialization the console message
        // name: name of the log
        // align: display alignment
        // size: display width
        // before: function befoe sizing
        // after: function after sizing
        format: [
            {
                name: "time",
                before: function (strVal, originalVal) {
                    return chalk.gray("| " + originalVal.toLocaleString() + " | ");
                }
            },
            {
                name: "level",
                size: 6,
                after: function (strVal, originalVal) {
                    var color = colors[originalVal],
                        out = strVal.toUpperCase();
                    return (color ? color(out) : out) + " | ";
                }
            },
            // log.error(new Error('test'));
            // if message is an error object, it will add an 'err' item that include the error's stack 
            // and it's 'msg' is error's name and message
            {
                name: "err",
                before: function (strVal, originalVal) {
                    return originalVal.stack.replace("<br />", "/n");
                }
            },
            // just display the msg ...
            {
                name: "msg"
            }
        ],
        // Directory of log files
        dir: path.resolve(process.cwd(), "log"),
        // max file size of log file, if greater than this value, logs will save to a new file
        maxSize: 30 // 30MB
};

// 2. Use config
logger.config = config;

// OR
var log = new logger(config);

// ...

Get

var logger = require('g-logger');
// Get 'mylog' log files from 'd:/log/'
// matches: regexp result of filename
// log: each line of logs
logger.get('d:/log/', 'mylog', function(matches, log){
    // skip if it's not an error log
    if(!('err' in log)){
        return false;
    }
});

Delete

var logger = require('g-logger');
// Delete 'mylog' log files from 'd:/log/'
// matches: regexp result of filename
logger.del('d:/log/', 'mylog', function(matches){
   ...
});

License

MIT © GE YONG