pub-logger
Micro logger with publish/subscribe mechanism.
What it can do:
- log(...args) parses arguments to string and sends them to process.stdout,
- error(...args) parses arguments to string and sends them to process.stderr,
- log/error messages are published to subscribed functions,
- it can asynchronously read a file and run a callback with the contents,
- stdout/stderr can be sent to files; ex.: node app.js 2>&1 | tee mylogs.log.
Pub/Sub is useful if you require objects in your application to consume the log. For example: if you'd like to send new logs in realtime to an admin dashboard over web sockets, avoiding costly file operations.
USAGE
const Logger = "_log_"; // I. log & subscribe & unsubscribeconst sub1 = { console; }const sub2 = { console; }logger;logger; // goes to stdout//-> SUB-1: Sat Oct 14 2017 23:04:57 GMT+0100 (BST) :: _log_ :: logging...//-> SUB-2: Sat Oct 14 2017 23:04:57 GMT+0100 (BST) :: _log_ :: logging...logger; // also: unsubscribe(sub1, sub2); // II. error & subscribe & unsubscribeAllconst sub3 = { console; }const sub4 = { console; }logger;logger; // goes to stderr//-> SUB-3: Sat Oct 14 2017 23:04:57 GMT+0100 (BST) :: _log_ :: errors!!!//-> SUB-4: Sat Oct 14 2017 23:04:57 GMT+0100 (BST) :: _log_ :: errors!!!logger; // III. readLogsconst cb = { console; }// throws Error if there is a problem with of the file; IO errors go to stderrlogger; // async//-> log from file: ....