|
|
@ -14,6 +14,7 @@ var messageTemplate = function (type, time, tag, info) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var write = function (ctx, content) {
|
|
|
|
var write = function (ctx, content) {
|
|
|
|
|
|
|
|
if (!ctx.store) { return; }
|
|
|
|
ctx.store.log(ctx.channelName, content);
|
|
|
|
ctx.store.log(ctx.channelName, content);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -44,7 +45,12 @@ var handlers = {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var noop = function () {};
|
|
|
|
|
|
|
|
|
|
|
|
var createLogType = function (ctx, type) {
|
|
|
|
var createLogType = function (ctx, type) {
|
|
|
|
|
|
|
|
if (logLevels.indexOf(type) < logLevels.indexOf(ctx.logLevel)) {
|
|
|
|
|
|
|
|
return noop;
|
|
|
|
|
|
|
|
}
|
|
|
|
return function (tag, info) {
|
|
|
|
return function (tag, info) {
|
|
|
|
var time = new Date().toISOString();
|
|
|
|
var time = new Date().toISOString();
|
|
|
|
var content;
|
|
|
|
var content;
|
|
|
@ -53,7 +59,6 @@ var createLogType = function (ctx, type) {
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ctx.logToStdout && typeof(handlers[type]) === 'function') {
|
|
|
|
if (ctx.logToStdout && typeof(handlers[type]) === 'function') {
|
|
|
|
handlers[type](ctx, time, tag, info);
|
|
|
|
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) {
|
|
|
|
Logger.create = function (config, cb) {
|
|
|
|
if (!config.logPath) {
|
|
|
|
if (typeof(config.logLevel) !== 'string') {
|
|
|
|
// XXX don't crash, print that you won't log to file
|
|
|
|
config.logLevel = 'info';
|
|
|
|
throw new Error("Logger: Expected filePath");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* config: {
|
|
|
|
var ctx = {
|
|
|
|
filePath: '???',
|
|
|
|
channelName: launchTime,
|
|
|
|
logLevel: 'silly',
|
|
|
|
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 date = new Date();
|
|
|
|
var launchTime = ('' + date.getUTCFullYear()).slice(-2) + date.toISOString();
|
|
|
|
var launchTime = ('' + date.getUTCFullYear()).slice(-2) + date.toISOString();
|
|
|
@ -79,20 +96,8 @@ Logger.create = function (config, cb) {
|
|
|
|
Store.create({
|
|
|
|
Store.create({
|
|
|
|
filePath: config.logPath,
|
|
|
|
filePath: config.logPath,
|
|
|
|
}, function (store) {
|
|
|
|
}, function (store) {
|
|
|
|
var ctx = {
|
|
|
|
ctx.store = store;
|
|
|
|
store: store,
|
|
|
|
cb(Object.freeze(createMethods(ctx)));
|
|
|
|
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));
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|