Skip to content

Flexible distributed logger module with hot-reload support 🌋

License

Notifications You must be signed in to change notification settings

jahnestacado/logia

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM version downloads per month

logia


Flexible distributed logger module with hot-reload support

Features
  • Control loggers through configuration file during runtime without restarting your application (hot-reload).
  • Run both on the server and on the browser.
  • Master/slave mode support which allows logger orchestration in distributed systems
  • RegExp configuration matchers
  • Colored logs
  • Log file size limiters
  • Remote logging through websockets or http

Install

Install with npm:

$ npm install logia

Use

const Logia = require("logia");
const dbLogger = Logia("database");
const parserLogger = Logia("parser");

dbLogger.setLevel("trace");
parserLogger.setLevel("debug");

dbLogger.trace("Number of entries are 0.");
parserLogger.debug("Invoked with no arguments.");
dbLogger.info("Retrieving data...");
parserLogger.warn("Note that something is missing!");
dbLogger.error("Something exploded!");
parserLogger.fatal("something went terribly wrong :(");

Above code will give below output:

(31-03-2017 11:54:06) TRACE [database]:Number of entries are 0.
(31-03-2017 11:54:06) DEBUG [parser]  :Invoked with no arguments.
(31-03-2017 11:54:06) INFO  [database]:Retrieving data...
(31-03-2017 11:54:06) WARN  [parser]  :Note that something is missing!
(31-03-2017 11:54:06) ERROR [database]:Something exploded!
(31-03-2017 11:54:06) FATAL [parser]  :something went terribly wrong :(

Logs can also be parameterized

const pi = 3.14;
const e = 2.72;

logger.info("Value of 'pi' is '{0}' and value of 'e' is '{1}'", pi, e);

Configuration File

When requiring the logia module a logia.json configuration file is created under a config folder of the current working directory. Alternatively we can set the LOGIA_CONFIG_FILE_PATH environment variable if we want to change the default configuration filename and location.

Every change we make in the configuration file is immediately applied to the configuration of the loggers without the need of rebooting our application

{
    "level": {
        ".*": null
    },
    "appenders": {
        "$_fill_logger_name_regexp": {
            "filepath": null,
            "maxSize": null
        }
    },
    "remotes": {
        "$_fill_logger_name_regexp": {
            "protocol": null,
            "url": null
        }
    },
    "mode": {
        "type": null,
        "host": null,
        "port": null
    },
    "stdout": false,
    "dateFormat": "(DD-MM-YYYY HH:mm:ss)",
    "overwritable": true
}

level

In this section we can set the logging level of our loggers. We target loggers by writing a javascript regexp string which matches the name of the loggers and set its level.

For example, below configuration will set all loggers that start with the substring "database" to level info and all loggers that contain the substring "handler" to level error;

 "level": {
    "^database": "info",
    "*handler": "error"
  }

By setting the level of a logger we also activate the logger and we enable others features(e.g appenders, remotes) to further configure them.

appenders

In the appenders section we can define where the logs will be stored in our filesystem. We target the loggers by writing a javascript regexp string, the same way we did for the level section but instead for setting the logging level we specify the output file. Moreover we can set the maxSize property in which we specify the maximum size in MBs of the log file. If the size of that file reaches the specified limit then the first half of its contents will be deleted. The filepath is treated as an absolute path unless it starts with ./ which then is resolved as a relative path to the current working directory.

For example, below configuration will append the logs of the loggers which name starts with the "database-mongo" and "database-redis" substring to the "/temp/redis.log" and "/temp/mongo.log" files respectively and limit their size to 20MBs. Similarly, the logs of the loggers which contain the "handler" substring in their name will end up in "cwd/logs/handler-errors.log" file.

 "appenders": {
    "^database-mongo": {
        "filepath": "/temp/mongo.log",
        "maxSize": 20
    },
     "^database-redis": {
        "filepath": "/temp/redis.log",
        "maxSize": 20
    },
    "*handler": {
        "filepath": "./logs/handler-errors.log"
    }
  }

remotes

The remotes section is similar to the appenders section but instead of appending the logs in a file it allows us to send them in a remote location. We need to provide a destination url and a protocol. Supported protocols are ws(websockets) and http and both use JSON format.

For example, below configuration will send the logs of the loggers which name starts with the "database" substring to the websocket server that runs on "websocket.server.com:8989". Similarly, the logs of loggers with the "handler" substring in their name will be send to the http server that runs on "http.server.com:8080".

 "remotes": {
    "^database": {
        "url": "websocket.server.com:8989",
        "protocol": "ws"
    },
    "*handler": {
        "url": "http.server.com:8080",
        "protocol": "http"
    }
  }

mode

Logia can run both as a master or as a slave node.

master

Below configuration will boot up a master node on localhost:8080.

  "mode": {
        "type": "master",
        "host": "master.server.com",
        "port": 8080
    }

By running a logia instance as a master node it allows to control all connected slaves through the masters node configuration file.

slave

Below configuration will boot up a slave node that will be connected and controlled by the the master node specified above.

  "mode": {
        "type": "slave",
        "host": "master.server.com",
        "port": 8080
    }

The master/slave setup uses websockets which preserves the order of the logs. This enables us to gather logs for client-server applications and still read them from top to bottom without the need of applying any type of sorting.

stdout

By setting this properrty to true all logs will be printed in standard output.

dateFormat

Set the date format. Uses Moment.js display format.

overwritable

See Logia.overwriteConfigFile

API

All aforementioned features are also accesible through a programming interface. Check the API documentation in Markdown or HTML

License

Copyright (c) 2016 Ioannis Tzanellis
Released under the MIT license

Releases

No releases published

Packages

No packages published