2019-04-09 00:11:36 +08:00
|
|
|
/*jshint esversion: 6 */
|
|
|
|
var Store = require("../storage/file");
|
|
|
|
|
|
|
|
var Logger = module.exports;
|
|
|
|
|
|
|
|
/* Every line in the log should contain:
|
|
|
|
* timestamp
|
|
|
|
* public key of initiator
|
|
|
|
* the action
|
|
|
|
* the event's tag
|
|
|
|
*/
|
|
|
|
var messageTemplate = function (type, time, tag, info) {
|
|
|
|
return JSON.stringify([type.toUpperCase(), time, tag, info]);
|
|
|
|
};
|
|
|
|
|
|
|
|
var write = function (ctx, content) {
|
2019-04-09 17:40:51 +08:00
|
|
|
if (!ctx.store) { return; }
|
2019-04-09 00:11:36 +08:00
|
|
|
ctx.store.log(ctx.channelName, content);
|
|
|
|
};
|
|
|
|
|
|
|
|
// various degrees of logging
|
|
|
|
const logLevels = ['silly', 'debug', 'verbose', 'feedback', 'info', 'warn', 'error'];
|
|
|
|
|
|
|
|
var handlers = {
|
|
|
|
silly: function (ctx, time, tag, info) {
|
|
|
|
console.log('[SILLY]', time, tag, info);
|
|
|
|
},
|
|
|
|
debug: function (ctx, time, tag, info) {
|
|
|
|
console.log('[DEBUG]', time, tag, info);
|
|
|
|
},
|
|
|
|
verbose: function (ctx, time, tag, info) {
|
|
|
|
console.log('[VERBOSE]', time, tag, info);
|
|
|
|
},
|
|
|
|
feedback: function (ctx, time, tag, info) {
|
|
|
|
console.log('[FEEDBACK]', time, tag, info);
|
|
|
|
},
|
|
|
|
info: function (ctx, time, tag, info) {
|
|
|
|
console.info('[INFO]', time, tag, info);
|
|
|
|
},
|
|
|
|
warn: function (ctx, time, tag, info) {
|
|
|
|
console.warn('[WARN]', time, tag, info);
|
|
|
|
},
|
|
|
|
error: function (ctx, time, tag, info) {
|
|
|
|
console.error('[ERROR]', time, tag, info);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-09 17:40:51 +08:00
|
|
|
var noop = function () {};
|
|
|
|
|
2019-04-09 00:11:36 +08:00
|
|
|
var createLogType = function (ctx, type) {
|
2019-04-09 17:40:51 +08:00
|
|
|
if (logLevels.indexOf(type) < logLevels.indexOf(ctx.logLevel)) {
|
|
|
|
return noop;
|
|
|
|
}
|
2019-04-09 00:11:36 +08:00
|
|
|
return function (tag, info) {
|
|
|
|
var time = new Date().toISOString();
|
|
|
|
var content;
|
|
|
|
try {
|
|
|
|
content = messageTemplate(type, time, tag, info);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ctx.logToStdout && typeof(handlers[type]) === 'function') {
|
|
|
|
handlers[type](ctx, time, tag, info);
|
|
|
|
}
|
|
|
|
write(ctx, content);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-04-09 17:40:51 +08:00
|
|
|
var createMethods = function (ctx) {
|
|
|
|
var log = {};
|
|
|
|
logLevels.forEach(function (type) {
|
|
|
|
log[type] = createLogType(ctx, type);
|
|
|
|
});
|
|
|
|
return log;
|
|
|
|
};
|
|
|
|
|
2019-04-09 00:11:36 +08:00
|
|
|
Logger.create = function (config, cb) {
|
2019-04-09 17:40:51 +08:00
|
|
|
if (typeof(config.logLevel) !== 'string') {
|
|
|
|
config.logLevel = 'info';
|
2019-04-09 00:11:36 +08:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:57:33 +08:00
|
|
|
var date = new Date();
|
|
|
|
var launchTime = ('' + date.getUTCFullYear()).slice(-2) + date.toISOString();
|
|
|
|
|
2019-04-09 17:40:51 +08:00
|
|
|
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)));
|
|
|
|
}
|
2019-04-09 00:11:36 +08:00
|
|
|
|
|
|
|
Store.create({
|
|
|
|
filePath: config.logPath,
|
|
|
|
}, function (store) {
|
2019-04-09 17:40:51 +08:00
|
|
|
ctx.store = store;
|
|
|
|
cb(Object.freeze(createMethods(ctx)));
|
2019-04-09 00:11:36 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|