META Logger

Lightweight logging library for NodeJS with support of facilities and multiple transports.

Get on GitHub

Installation

npm install meta-logger --save

Installation

META Logger can be installed via NPM

Package meta-logger on NPM

var logger = require("meta-logger");

//Setup targets
logger.toConsole({
    level: "debug",
    timestamp: true,
    colorize: true
}).toFile("demo.log", {
    level: "info",
    timestamp: true,
    facilities: [ "test" ]
}).toJsonFile("demo.json", {
    level: "warn"
});

//Logging without specifying facility
logger.log("info", "Hello %s", "world");
logger.debug("Hello %s", "debug");
logger.info("Hello %s", "info");
logger.warn("Hello %s", "warn");
logger.error("Hello %s", "error");

//Create facility shorthand
var facilityLogger = logger.facility("test");

//Log with facility prefix
facilityLogger.log("info", "Hello %s", "world");
facilityLogger.debug("Hello %s", "debug");
facilityLogger.info("Hello %s", "info");
facilityLogger.warn("Hello %s", "warn");
facilityLogger.error("Hello %s", "error");

Usage

Import module

var logger = require("meta-logger");

Setup logging target:

logger.toConsole({
    level: "debug",
    timestamp: true,
    colorize: true
});

Console target is set by default.

Log few messages:

logger.log("info", "Hello %s", "world");
logger.debug("Hello %s", "debug");
logger.info("Hello %s", "info");
logger.warn("Hello %s", "warn");
logger.error("Hello %s", "error");

Logging methods accepts same arguments as console.log function.

Create facility and log something:

var facilityLogger = logger.facility("test");

facilityLogger.log("info", "Hello %s", "world");
facilityLogger.debug("Hello %s", "debug");
facilityLogger.info("Hello %s", "info");
facilityLogger.warn("Hello %s", "warn");
facilityLogger.error("Hello %s", "error");

Logging targets

Console

logger.toConsole({
    level: "debug",
    facilities: [...], //or null to accept all facilities
    timestamp: true,
    colorize: true,
    colors: {
        debug:  logger.colors.cyan,
        info:   logger.colors.white,
        warn:   logger.colors.yellow,
        error:  logger.colors.red
    }
});

Prints log messages to stdout (console).

Console targets accepts following configuration properties:

Property Type Description
level string Logging level (debug / info / warn / error)
facilities array Log only specified facilities
timestamp boolean Print timestamp
colorize boolean Print messages with colors
colors object Colors for message levels - see colors module.
logger.to(new Logger.ConsoleTarget({
    ///... options ...
}));

Method toConsole overrides previous console target settings.

You can use more console targets (for multiple configurations) by creating target instance manually.

File

logger.toFile(filename, {
    level: "debug",
    facilities: [...], //or null to accept all facilities
    timestamp: true
}).toFile(anotherFilename, {
    level: "error",
    timestamp: true,
    //...
});

Append log messages to specified file. Message is formatted same way as to console.

File target accepts following configuration properties:

Property Type Description
level string Logging level (debug / info / warn / error)
facilities array Log only specified facilities
timestamp boolean Print timestamp

File target can be set for multiple files with different configurations.

JSON file

logger.toJsonFile(filename, {
    level: "debug",
    facilities: [...], //or null to accept all facilities
    timestamp: true
});

Format of JSON log file

{},
{"timestamp":"2015-06-04T21:20:54.627Z","level":"warn","facility":null,"msg":["Hello %s","warn"]},
{"timestamp":"2015-06-04T21:20:54.629Z","level":"error","facility":"test","msg":["Hello %s","error"]}
...

Append log messages to specified file in JSON format.

JSON target accepts following configuration properties:

Property Type Description
level string Logging level (debug / info / warn / error)
facilities array Log only specified facilities
timestamp boolean Print timestamp

Custom logging target

var util = require("util");

var MyTarget = function(options){

    options = options || {};
    this.prefix = options.prefix || "";

};

MyTarget.prototype.log = function(level, facility, args){

    console.log(level, facility, this.prefix, util.format.apply(this, args));

};

logger.to(new MyTarget({
    prefix: "my-target"
}));

Custom target is defined by instance of simple object which implements log method and is registered to logger via to method.

You can also extend BaseTarget.

var MyTarget = function(options){

    options = options || {};
    this.prefix = options.prefix || "";

    logger.BaseTarget.call(this, options);

};

util.inherits(MyTarget, logger.BaseTarget);

MyLogger.prototype.write = function(level, facility, msg){

    console.log(level, facility, this.prefix, msg.join(" "));

};
logger.levels = {
    custom: 4,
    debug:  3,
    info:   2,
    warn:   1,
    error:  0
};

Custom logging levels

Logging levels can be set by modifying logger.levels property.

License

This library is published under MIT license.

Copyright (c) 2015 Jiri Hybek, jiri.hybek@cryonix.cz