improved logger behaviour and updated default configuration

This commit is contained in:
ansuz
2019-04-09 11:40:51 +02:00
parent b8aa962315
commit 4e9273f35c
4 changed files with 42 additions and 41 deletions

View File

@@ -14,6 +14,7 @@ var messageTemplate = function (type, time, tag, info) {
};
var write = function (ctx, content) {
if (!ctx.store) { return; }
ctx.store.log(ctx.channelName, content);
};
@@ -44,7 +45,12 @@ var handlers = {
}
};
var noop = function () {};
var createLogType = function (ctx, type) {
if (logLevels.indexOf(type) < logLevels.indexOf(ctx.logLevel)) {
return noop;
}
return function (tag, info) {
var time = new Date().toISOString();
var content;
@@ -53,7 +59,6 @@ var createLogType = function (ctx, type) {
} catch (e) {
return;
}
if (ctx.logToStdout && typeof(handlers[type]) === 'function') {
handlers[type](ctx, time, tag, info);
}
@@ -61,17 +66,29 @@ var createLogType = function (ctx, type) {
};
};
// Log.verbose('THING', x);
var createMethods = function (ctx) {
var log = {};
logLevels.forEach(function (type) {
log[type] = createLogType(ctx, type);
});
return log;
};
Logger.create = function (config, cb) {
if (!config.logPath) {
// XXX don't crash, print that you won't log to file
throw new Error("Logger: Expected filePath");
if (typeof(config.logLevel) !== 'string') {
config.logLevel = 'info';
}
/* config: {
filePath: '???',
logLevel: 'silly',
} */
var ctx = {
channelName: launchTime,
logFeedback: Boolean(config.logFeedback),
logLevel: config.logLevel,
};
if (!config.logPath) {
console.log("No logPath configured. Logging to file disabled");
return void cb(Object.freeze(createMethods(ctx)));
}
var date = new Date();
var launchTime = ('' + date.getUTCFullYear()).slice(-2) + date.toISOString();
@@ -79,20 +96,8 @@ Logger.create = function (config, cb) {
Store.create({
filePath: config.logPath,
}, function (store) {
var ctx = {
store: store,
channelName: launchTime,
logFeedback: Boolean(config.logFeedback),
// TODO respect configured log settings
logLevel: logLevels.indexOf(config.logLevel), // 0 for silly, 1 for debug
};
var log = {};
logLevels.forEach(function (type) {
log[type] = createLogType(ctx, type);
});
cb(Object.freeze(log));
ctx.store = store;
cb(Object.freeze(createMethods(ctx)));
});
};