2017-06-23 23:32:24 +02:00
|
|
|
import moment from 'moment';
|
2017-07-04 21:12:30 +02:00
|
|
|
import { _ } from 'lib/locale.js';
|
2017-07-05 23:52:31 +02:00
|
|
|
import FsDriverDummy from 'lib/fs-driver-dummy.js';
|
2017-06-23 23:32:24 +02:00
|
|
|
|
|
|
|
class Logger {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.targets_ = [];
|
|
|
|
this.level_ = Logger.LEVEL_ERROR;
|
2017-06-25 14:49:46 +02:00
|
|
|
this.fileAppendQueue_ = []
|
2017-06-23 23:32:24 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 23:52:31 +02:00
|
|
|
static fsDriver() {
|
|
|
|
if (!Logger.fsDriver_) Logger.fsDriver_ = new FsDriverDummy();
|
|
|
|
return Logger.fsDriver_;
|
|
|
|
}
|
|
|
|
|
2017-06-23 23:32:24 +02:00
|
|
|
setLevel(level) {
|
|
|
|
this.level_ = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
level() {
|
|
|
|
return this.level_;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTargets() {
|
|
|
|
this.targets_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
addTarget(type, options = null) {
|
|
|
|
let target = { type: type };
|
|
|
|
for (let n in options) {
|
|
|
|
if (!options.hasOwnProperty(n)) continue;
|
|
|
|
target[n] = options[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.targets_.push(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
log(level, object) {
|
|
|
|
if (this.level() < level || !this.targets_.length) return;
|
|
|
|
|
|
|
|
let levelString = '';
|
|
|
|
if (this.level() == Logger.LEVEL_INFO) levelString = '[info] ';
|
|
|
|
if (this.level() == Logger.LEVEL_WARN) levelString = '[warn] ';
|
|
|
|
if (this.level() == Logger.LEVEL_ERROR) levelString = '[error] ';
|
|
|
|
let line = moment().format('YYYY-MM-DD HH:mm:ss') + ': ' + levelString;
|
|
|
|
|
|
|
|
for (let i = 0; i < this.targets_.length; i++) {
|
|
|
|
let t = this.targets_[i];
|
|
|
|
if (t.type == 'console') {
|
|
|
|
let fn = 'debug';
|
|
|
|
if (level = Logger.LEVEL_ERROR) fn = 'error';
|
|
|
|
if (level = Logger.LEVEL_WARN) fn = 'warn';
|
|
|
|
if (level = Logger.LEVEL_INFO) fn = 'info';
|
|
|
|
if (typeof object === 'object') {
|
|
|
|
console[fn](line, object);
|
|
|
|
} else {
|
|
|
|
console[fn](line + object);
|
|
|
|
}
|
|
|
|
} else if (t.type == 'file') {
|
2017-06-24 00:19:59 +02:00
|
|
|
let serializedObject = '';
|
|
|
|
|
|
|
|
if (typeof object === 'object') {
|
|
|
|
if (object instanceof Error) {
|
|
|
|
serializedObject = object.toString();
|
|
|
|
if (object.stack) serializedObject += "\n" + object.stack;
|
|
|
|
} else {
|
|
|
|
serializedObject = JSON.stringify(object);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
serializedObject = object;
|
|
|
|
}
|
2017-06-25 14:49:46 +02:00
|
|
|
|
2017-07-05 23:52:31 +02:00
|
|
|
Logger.fsDriver().appendFileSync(t.path, line + serializedObject + "\n");
|
2017-06-25 14:49:46 +02:00
|
|
|
|
2017-07-01 00:53:22 +02:00
|
|
|
// this.fileAppendQueue_.push({
|
|
|
|
// path: t.path,
|
|
|
|
// line: line + serializedObject + "\n",
|
|
|
|
// });
|
|
|
|
|
|
|
|
// this.scheduleFileAppendQueueProcessing_();
|
2017-06-27 21:26:29 +02:00
|
|
|
} else if (t.type == 'vorpal') {
|
|
|
|
t.vorpal.log(object);
|
2017-06-23 23:32:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 00:53:22 +02:00
|
|
|
// scheduleFileAppendQueueProcessing_() {
|
|
|
|
// if (this.fileAppendQueueTID_) return;
|
2017-06-25 14:49:46 +02:00
|
|
|
|
2017-07-01 00:53:22 +02:00
|
|
|
// this.fileAppendQueueTID_ = setTimeout(async () => {
|
|
|
|
// this.fileAppendQueueTID_ = null;
|
2017-06-25 14:49:46 +02:00
|
|
|
|
2017-07-01 00:53:22 +02:00
|
|
|
// let queue = this.fileAppendQueue_.slice(0);
|
|
|
|
// for (let i = 0; i < queue.length; i++) {
|
|
|
|
// let t = queue[i];
|
|
|
|
// await fs.appendFile(t.path, t.line);
|
|
|
|
// }
|
|
|
|
// this.fileAppendQueue_.splice(0, queue.length);
|
|
|
|
// }, 1);
|
|
|
|
// }
|
2017-06-25 14:49:46 +02:00
|
|
|
|
2017-06-23 23:32:24 +02:00
|
|
|
error(object) { return this.log(Logger.LEVEL_ERROR, object); }
|
|
|
|
warn(object) { return this.log(Logger.LEVEL_WARN, object); }
|
|
|
|
info(object) { return this.log(Logger.LEVEL_INFO, object); }
|
|
|
|
debug(object) { return this.log(Logger.LEVEL_DEBUG, object); }
|
|
|
|
|
2017-07-04 21:12:30 +02:00
|
|
|
static levelStringToId(s) {
|
|
|
|
if (s == 'none') return Logger.LEVEL_NONE;
|
|
|
|
if (s == 'error') return Logger.LEVEL_ERROR;
|
|
|
|
if (s == 'warn') return Logger.LEVEL_WARN;
|
|
|
|
if (s == 'info') return Logger.LEVEL_INFO;
|
|
|
|
if (s == 'debug') return Logger.LEVEL_DEBUG;
|
|
|
|
throw new Error(_('Unknown log level: %s', s));
|
|
|
|
}
|
|
|
|
|
2017-06-23 23:32:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Logger.LEVEL_NONE = 0;
|
|
|
|
Logger.LEVEL_ERROR = 10;
|
|
|
|
Logger.LEVEL_WARN = 20;
|
|
|
|
Logger.LEVEL_INFO = 30;
|
|
|
|
Logger.LEVEL_DEBUG = 40;
|
|
|
|
|
|
|
|
export { Logger };
|