You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-12-05 22:57:29 +02:00
First pass at linting lib dir
This commit is contained in:
@@ -4,11 +4,10 @@ const { time } = require('lib/time-utils.js');
|
||||
const { FsDriverDummy } = require('lib/fs-driver-dummy.js');
|
||||
|
||||
class Logger {
|
||||
|
||||
constructor() {
|
||||
this.targets_ = [];
|
||||
this.level_ = Logger.LEVEL_INFO;
|
||||
this.fileAppendQueue_ = []
|
||||
this.fileAppendQueue_ = [];
|
||||
this.lastDbCleanup_ = time.unixMs();
|
||||
}
|
||||
|
||||
@@ -49,10 +48,10 @@ class Logger {
|
||||
if (typeof object === 'object') {
|
||||
if (object instanceof Error) {
|
||||
output = object.toString();
|
||||
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;
|
||||
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;
|
||||
} else {
|
||||
output = JSON.stringify(object);
|
||||
}
|
||||
@@ -60,7 +59,7 @@ class Logger {
|
||||
output = object;
|
||||
}
|
||||
|
||||
return output;
|
||||
return output;
|
||||
}
|
||||
|
||||
objectsToString(...object) {
|
||||
@@ -81,7 +80,7 @@ class Logger {
|
||||
\`timestamp\` INT NOT NULL
|
||||
);
|
||||
`;
|
||||
return output.split("\n").join(' ');
|
||||
return output.split('\n').join(' ');
|
||||
}
|
||||
|
||||
// Only for database at the moment
|
||||
@@ -94,7 +93,7 @@ class Logger {
|
||||
const target = this.targets_[i];
|
||||
if (target.type == 'database') {
|
||||
let sql = 'SELECT * FROM logs WHERE level IN (' + options.levels.join(',') + ') ORDER BY timestamp DESC';
|
||||
if (limit !== null) sql += ' LIMIT ' + limit
|
||||
if (limit !== null) sql += ' LIMIT ' + limit;
|
||||
return await target.database.selectAll(sql);
|
||||
}
|
||||
}
|
||||
@@ -103,7 +102,7 @@ class Logger {
|
||||
|
||||
targetLevel(target) {
|
||||
if ('level' in target) return target.level;
|
||||
return this.level();
|
||||
return this.level();
|
||||
}
|
||||
|
||||
log(level, ...object) {
|
||||
@@ -117,7 +116,7 @@ class Logger {
|
||||
|
||||
for (let i = 0; i < this.targets_.length; i++) {
|
||||
let target = this.targets_[i];
|
||||
|
||||
|
||||
if (this.targetLevel(target) < level) continue;
|
||||
|
||||
if (target.type == 'console') {
|
||||
@@ -128,14 +127,16 @@ class Logger {
|
||||
console[fn](line + this.objectsToString(...object));
|
||||
} else if (target.type == 'file') {
|
||||
let serializedObject = this.objectsToString(...object);
|
||||
Logger.fsDriver().appendFileSync(target.path, line + serializedObject + "\n");
|
||||
Logger.fsDriver().appendFileSync(target.path, line + serializedObject + '\n');
|
||||
} else if (target.type == 'database') {
|
||||
let msg = this.objectsToString(...object);
|
||||
|
||||
let queries = [{
|
||||
sql: 'INSERT INTO logs (`source`, `level`, `message`, `timestamp`) VALUES (?, ?, ?, ?)',
|
||||
params: [target.source, level, msg, time.unixMs()],
|
||||
}];
|
||||
let queries = [
|
||||
{
|
||||
sql: 'INSERT INTO logs (`source`, `level`, `message`, `timestamp`) VALUES (?, ?, ?, ?)',
|
||||
params: [target.source, level, msg, time.unixMs()],
|
||||
},
|
||||
];
|
||||
|
||||
const now = time.unixMs();
|
||||
if (now - this.lastDbCleanup_ > 1000 * 60 * 60) {
|
||||
@@ -152,10 +153,18 @@ class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
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); }
|
||||
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);
|
||||
}
|
||||
|
||||
static levelStringToId(s) {
|
||||
if (s == 'none') return Logger.LEVEL_NONE;
|
||||
@@ -187,7 +196,6 @@ class Logger {
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Logger.LEVEL_NONE = 0;
|
||||
@@ -196,4 +204,4 @@ Logger.LEVEL_WARN = 20;
|
||||
Logger.LEVEL_INFO = 30;
|
||||
Logger.LEVEL_DEBUG = 40;
|
||||
|
||||
module.exports = { Logger };
|
||||
module.exports = { Logger };
|
||||
|
||||
Reference in New Issue
Block a user