2017-11-03 02:09:34 +02:00
|
|
|
const moment = require('moment');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { time } = require('lib/time-utils.js');
|
2017-11-04 13:26:49 +02:00
|
|
|
const { FsDriverDummy } = require('lib/fs-driver-dummy.js');
|
2017-06-23 23:32:24 +02:00
|
|
|
|
|
|
|
class Logger {
|
|
|
|
constructor() {
|
|
|
|
this.targets_ = [];
|
2018-05-23 15:25:59 +02:00
|
|
|
this.level_ = Logger.LEVEL_INFO;
|
2019-07-29 15:43:53 +02:00
|
|
|
this.fileAppendQueue_ = [];
|
2017-07-16 18:20:25 +02:00
|
|
|
this.lastDbCleanup_ = time.unixMs();
|
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_;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:35:50 +02:00
|
|
|
targets() {
|
|
|
|
return this.targets_;
|
|
|
|
}
|
|
|
|
|
2017-06-23 23:32:24 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-07-06 21:48:17 +02:00
|
|
|
objectToString(object) {
|
|
|
|
let output = '';
|
|
|
|
|
|
|
|
if (typeof object === 'object') {
|
|
|
|
if (object instanceof Error) {
|
|
|
|
output = object.toString();
|
2019-07-29 15:43:53 +02:00
|
|
|
if (object.code) output += '\nCode: ' + object.code;
|
|
|
|
if (object.headers) output += '\nHeader: ' + JSON.stringify(object.headers);
|
|
|
|
if (object.request) output += '\nRequest: ' + (object.request.substr ? object.request.substr(0, 1024) : '');
|
|
|
|
if (object.stack) output += '\n' + object.stack;
|
2017-07-06 21:48:17 +02:00
|
|
|
} else {
|
|
|
|
output = JSON.stringify(object);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output = object;
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
return output;
|
2017-07-07 19:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
objectsToString(...object) {
|
|
|
|
let output = [];
|
|
|
|
for (let i = 0; i < object.length; i++) {
|
|
|
|
output.push('"' + this.objectToString(object[i]) + '"');
|
|
|
|
}
|
|
|
|
return output.join(', ');
|
2017-07-06 21:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static databaseCreateTableSql() {
|
|
|
|
let output = `
|
2017-07-07 19:19:24 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS logs (
|
2017-07-06 21:48:17 +02:00
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
source TEXT,
|
|
|
|
level INT NOT NULL,
|
|
|
|
message TEXT NOT NULL,
|
|
|
|
\`timestamp\` INT NOT NULL
|
|
|
|
);
|
|
|
|
`;
|
2019-07-29 15:43:53 +02:00
|
|
|
return output.split('\n').join(' ');
|
2017-07-06 21:48:17 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 19:19:24 +02:00
|
|
|
// Only for database at the moment
|
2018-01-31 21:51:29 +02:00
|
|
|
async lastEntries(limit = 100, options = null) {
|
|
|
|
if (options === null) options = {};
|
|
|
|
if (!options.levels) options.levels = [Logger.LEVEL_DEBUG, Logger.LEVEL_INFO, Logger.LEVEL_WARN, Logger.LEVEL_ERROR];
|
|
|
|
if (!options.levels.length) return [];
|
|
|
|
|
2017-07-07 19:19:24 +02:00
|
|
|
for (let i = 0; i < this.targets_.length; i++) {
|
|
|
|
const target = this.targets_[i];
|
|
|
|
if (target.type == 'database') {
|
2018-01-31 21:51:29 +02:00
|
|
|
let sql = 'SELECT * FROM logs WHERE level IN (' + options.levels.join(',') + ') ORDER BY timestamp DESC';
|
2019-07-29 15:43:53 +02:00
|
|
|
if (limit !== null) sql += ' LIMIT ' + limit;
|
2017-11-21 20:48:50 +02:00
|
|
|
return await target.database.selectAll(sql);
|
2017-07-07 19:19:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-05-11 18:53:56 +02:00
|
|
|
targetLevel(target) {
|
|
|
|
if ('level' in target) return target.level;
|
2019-07-29 15:43:53 +02:00
|
|
|
return this.level();
|
2019-05-11 18:53:56 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 19:19:24 +02:00
|
|
|
log(level, ...object) {
|
2019-05-11 18:53:56 +02:00
|
|
|
if (!this.targets_.length) return;
|
2017-06-23 23:32:24 +02:00
|
|
|
|
2017-07-07 19:19:24 +02:00
|
|
|
let line = moment().format('YYYY-MM-DD HH:mm:ss') + ': ';
|
|
|
|
|
2017-06-23 23:32:24 +02:00
|
|
|
for (let i = 0; i < this.targets_.length; i++) {
|
2017-07-06 21:48:17 +02:00
|
|
|
let target = this.targets_[i];
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2019-05-11 18:53:56 +02:00
|
|
|
if (this.targetLevel(target) < level) continue;
|
|
|
|
|
2017-07-06 21:48:17 +02:00
|
|
|
if (target.type == 'console') {
|
2017-07-11 20:41:18 +02:00
|
|
|
let fn = 'log';
|
2017-07-10 01:20:38 +02:00
|
|
|
if (level == Logger.LEVEL_ERROR) fn = 'error';
|
|
|
|
if (level == Logger.LEVEL_WARN) fn = 'warn';
|
|
|
|
if (level == Logger.LEVEL_INFO) fn = 'info';
|
2019-09-08 18:16:45 +02:00
|
|
|
const consoleObj = target.console ? target.console : console;
|
|
|
|
consoleObj[fn](line + this.objectsToString(...object));
|
2017-07-06 21:48:17 +02:00
|
|
|
} else if (target.type == 'file') {
|
2017-07-07 19:19:24 +02:00
|
|
|
let serializedObject = this.objectsToString(...object);
|
2019-07-29 15:43:53 +02:00
|
|
|
Logger.fsDriver().appendFileSync(target.path, line + serializedObject + '\n');
|
2017-07-06 21:48:17 +02:00
|
|
|
} else if (target.type == 'database') {
|
2017-07-07 19:19:24 +02:00
|
|
|
let msg = this.objectsToString(...object);
|
2017-07-16 18:20:25 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
let queries = [
|
|
|
|
{
|
|
|
|
sql: 'INSERT INTO logs (`source`, `level`, `message`, `timestamp`) VALUES (?, ?, ?, ?)',
|
|
|
|
params: [target.source, level, msg, time.unixMs()],
|
|
|
|
},
|
|
|
|
];
|
2017-07-16 18:20:25 +02:00
|
|
|
|
|
|
|
const now = time.unixMs();
|
|
|
|
if (now - this.lastDbCleanup_ > 1000 * 60 * 60) {
|
|
|
|
this.lastDbCleanup_ = now;
|
|
|
|
const dayKeep = 14;
|
|
|
|
queries.push({
|
|
|
|
sql: 'DELETE FROM logs WHERE `timestamp` < ?',
|
|
|
|
params: [now - 1000 * 60 * 60 * 24 * dayKeep],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
target.database.transactionExecBatch(queries);
|
2017-06-23 23:32:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +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-06-23 23:32:24 +02:00
|
|
|
|
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;
|
2017-08-20 22:11:32 +02:00
|
|
|
throw new Error(_('Unknown log level: %s', s));
|
|
|
|
}
|
|
|
|
|
|
|
|
static levelIdToString(id) {
|
|
|
|
if (id == Logger.LEVEL_NONE) return 'none';
|
|
|
|
if (id == Logger.LEVEL_ERROR) return 'error';
|
|
|
|
if (id == Logger.LEVEL_WARN) return 'warn';
|
|
|
|
if (id == Logger.LEVEL_INFO) return 'info';
|
|
|
|
if (id == Logger.LEVEL_DEBUG) return 'debug';
|
|
|
|
throw new Error(_('Unknown level ID: %s', id));
|
|
|
|
}
|
|
|
|
|
|
|
|
static levelIds() {
|
|
|
|
return [Logger.LEVEL_NONE, Logger.LEVEL_ERROR, Logger.LEVEL_WARN, Logger.LEVEL_INFO, Logger.LEVEL_DEBUG];
|
|
|
|
}
|
|
|
|
|
|
|
|
static levelEnum() {
|
|
|
|
let output = {};
|
|
|
|
const ids = this.levelIds();
|
|
|
|
for (let i = 0; i < ids.length; i++) {
|
|
|
|
output[ids[i]] = this.levelIdToString(ids[i]);
|
|
|
|
}
|
|
|
|
return output;
|
2017-07-04 21:12:30 +02:00
|
|
|
}
|
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;
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { Logger };
|