filestream-logger
v2.2.10
Published
A logger that creates a log-dir, that may change the logger's filename, that may use a formatter and that allows and extending various logger types
Downloads
2
Readme
filestream-logger
const FilestreamLogger = require("filestream-logger");
const FilestreamLogger = require("filestream-logger");
const TaskClock = require("task-clock");
const IndentModel = require("indent-model");
const LocaleTimezoneDate = require("locale-timezone-date");
//
// ...
//
// Choose a text formatter however you like,
const tabs5_4 = new IndentModel({ tabSize: 5, smallestSpace: 4 });
const formatter = (data, callback) => {
// I want to see the time in my locale timezone
const isoStr = new LocaleTimezoneDate().toLocaleISOString();
// I want to see data aligned in tabs
const logString = tabs5_4.tabify(isoStr, ...data);
callback(logString);
console.log(logString);
};
//
// ...
//
const logger = {};
logger.log = new FilestreamLogger("log", { formatter });
logger.error = new FilestreamLogger("error", { formatter, extend: [logger.log] });
logger.noob = new FilestreamLogger("noob");
logger.noob.extend([logger.log, logger.error]);
logger.noob.destroy();
logger.noob("never gonna happen");
logger.noob.onReady(() => console.log(`really never gonna happen,
all callbacks and logger.noob gets GC'd on the next line`));
delete (logger.noob);
//
// ...
//
// Every day at 24h in your locale timezone set logger's name to yyyy-mm-dd.log
class LoggerClock extends TaskClock {
constructor() {
super({ start: new Date(new Date().setHours(0, 0, 0, 0)), interval: { h: 24 } });
};
task(now, tick) {
const yyyymmdd = now.yyyymmdd();
logger.log.setName(yyyymmdd);
logger.error.setName(yyyymmdd);
};
get DateModel() {
return LocaleTimezoneDate;
};
};
const clock = new LoggerClock();
//
// ...
//
console.log(logger); // logger.noob is gone
logger.log("GET", "/v1/someapi/mongol/1", "spider", "monkey");
logger.log("CLOSED", "/v1/someapi/mongol/1", "spider", "monkey");
logger.error("FAILED", "/v1/someapi/mongol/1", "find errors in " + logger.error.filepath, "monkey!");
logger.error("FAILED", "/v1/someapi/mongol/2", "find errors in " + logger.error.filepath, "monkey!");
logger.log.setName("test");
logger.error.setName("noob"); // is empty so auto removed
logger.error.setName("mongol"); // is empty so auto removed
logger.error.setName("monkey");
logger.error("FAILED", "/v1/someapi/mongol/3", "find errors in " + logger.error.filepath, "monkey!");
logger.log("GET", "/v1/someapi/mongol/2", "spider", "monkey");
logger.log("CLOSED", "/v1/someapi/mongol/2", "spider", "monkey");
logger.log.destroy(); // the next error will not log to logger.log
logger.error("FAILED", "/v1/someapi/mongol/4", "find errors in " + logger.error.filepath, "monkey!");
//
//
// CONSOLE OUTPUT:
// {
// log: [Function (anonymous)] FilestreamLogger {
// formatter: [Function: formatter]
// },
// error: [Function (anonymous)] FilestreamLogger {
// formatter: [Function: formatter]
// }
// }
// 2021-04-15T12:58:56.598+0200 GET /v1/someapi/mongol/1 spider monkey
// 2021-04-15T12:58:56.598+0200 CLOSED /v1/someapi/mongol/1 spider monkey
// 2021-04-15T12:58:56.598+0200 FAILED /v1/someapi/mongol/1 find errors in loggers\error\2021-04-15.log monkey!
// 2021-04-15T12:58:56.599+0200 FAILED /v1/someapi/mongol/2 find errors in loggers\error\2021-04-15.log monkey!
// 2021-04-15T12:58:56.599+0200 FAILED /v1/someapi/mongol/3 find errors in loggers\error\monkey.log monkey!
// 2021-04-15T12:58:56.599+0200 GET /v1/someapi/mongol/2 spider monkey
// 2021-04-15T12:58:56.600+0200 CLOSED /v1/someapi/mongol/2 spider monkey
// 2021-04-15T12:58:56.600+0200 FAILED /v1/someapi/mongol/4 find errors in loggers\error\monkey.log monkey!
// destroyed loggers\noob
// destroyed loggers\log
//
//
// OUTPUT TO WHICH LOG-FILES:
// 1st OUTPUT: /loggers/log/2021-04-15.log
// 2nd OUTPUT: /loggers/log/2021-04-15.log
// 3rd OUTPUT: /loggers/error/2021-04-15.log + OUTPUT: /loggers/log/2021-04-15.log
// 4th OUTPUT: /loggers/error/2021-04-15.log + OUTPUT: /loggers/log/2021-04-15.log
// 5th OUTPUT: /loggers/error/monkey.log + OUTPUT: /loggers/log/test.log
// 6th OUTPUT: /loggers/log/test.log
// 7th OUTPUT: /loggers/log/test.log
// 8th OUTPUT: /loggers/error/monkey.log
//
// ...
//
process.on("SIGINT", () => {
logger.error("Node JS is now shutting down due to pressing ctrl + c");
// finish up all logs before exiting process
FilestreamLogger.destroyAll(() => process.exit());
// CONSOLE OUTPUT:
// 2021-04-15T13:01:37.152+0200 Node JS is now shutting down due to pressing ctrl + c
// destroyed loggers\error
//
// OUTPUT TO WHICH LOG-FILES:
// OUTPUT: /loggers/error/monkey.log
});