cryptpad/lib/log.js

113 lines
3 KiB
JavaScript
Raw Normal View History

2023-10-20 14:35:26 +00:00
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2020-02-14 21:29:30 +00:00
var Store = require("./storage/file");
2023-08-23 08:59:02 +00:00
var Util = require("./common-util");
2019-04-08 16:11:36 +00:00
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 noop = function () {};
2023-08-23 08:57:11 +00:00
var write = function (ctx, content, cb) {
if (typeof(cb) !== "function") { cb = noop; }
if (!ctx.store) {
cb = Util.mkAsync(cb);
return void cb();
}
ctx.store.log(ctx.channelName, content, cb);
2019-04-08 16:11:36 +00:00
};
// various degrees of logging
const logLevels = Logger.levels = ['silly', 'verbose', 'debug', 'feedback', 'info', 'warn', 'error'];
2019-04-08 16:11:36 +00:00
var handlers = {};
['silly', 'debug', 'verbose', 'feedback', 'info'].forEach(function (level) {
handlers[level] = function (ctx, content) { console.log(content); };
});
['warn', 'error'].forEach(function (level) {
2020-04-29 21:23:29 +00:00
handlers[level] = function (ctx, content) { console.error(content); };
});
2019-04-08 16:11:36 +00:00
var createLogType = function (ctx, type) {
if (logLevels.indexOf(type) < logLevels.indexOf(ctx.logLevel)) {
return noop;
}
2023-08-23 08:57:11 +00:00
return function (tag, info, cb) {
2019-04-23 11:25:03 +00:00
if (ctx.shutdown) {
throw new Error("Logger has been shut down!");
}
2019-04-08 16:11:36 +00:00
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, content);
2019-04-08 16:11:36 +00:00
}
2023-08-23 08:57:11 +00:00
write(ctx, content, cb);
2019-04-08 16:11:36 +00:00
};
};
var createMethods = function (ctx) {
var log = {};
logLevels.forEach(function (type) {
log[type] = createLogType(ctx, type);
});
return log;
};
2019-04-08 16:11:36 +00:00
Logger.create = function (config, cb) {
if (typeof(config.logLevel) !== 'string') {
config.logLevel = 'info';
2019-04-08 16:11:36 +00:00
}
2019-04-09 09:57:33 +00:00
var date = new Date();
var launchTime = ('' + date.getUTCFullYear()).slice(-2) + date.toISOString();
var ctx = {
channelName: launchTime,
logFeedback: Boolean(config.logFeedback),
logLevel: config.logLevel,
logToStdout: config.logToStdout,
};
if (!config.logPath) {
console.log("No logPath configured. Logging to file disabled");
var logger = createMethods(ctx);
logger.shutdown = noop;
return void cb(Object.freeze(logger));
}
2019-04-08 16:11:36 +00:00
Store.create({
filePath: config.logPath,
archivePath: config.archivePath,
}, function (err, store) {
if (err) {
throw err;
}
ctx.store = store;
2019-04-23 11:25:03 +00:00
var logger = createMethods(ctx);
logger.shutdown = function () {
delete ctx.store;
ctx.shutdown = true;
store.shutdown();
};
cb(Object.freeze(logger));
2019-04-08 16:11:36 +00:00
});
};