ffffng/server/logger.ts

111 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

import {Logger, TaggedLogger} from "./types";
function procConsole() {
// @ts-ignore
return process.console;
}
2021-08-23 21:05:17 +02:00
const noopTaggedLogger: TaggedLogger = {
debug(...args: any): void {},
info(...args: any): void {},
warn(...args: any): void {},
error(...args: any): void {},
profile(...args: any): void {},
};
2021-08-23 21:05:17 +02:00
class ActivatableLogger implements Logger {
private enabled: boolean = false;
2021-08-23 21:05:17 +02:00
init(): void {
const app = require('./app').app;
const config = require('./config').config;
2021-08-23 21:05:17 +02:00
const enabled = config.server.logging.enabled;
this.enabled = enabled;
2021-08-23 21:05:17 +02:00
// Hack to allow proper logging of Error.
Object.defineProperty(Error.prototype, 'message', {
configurable: true,
enumerable: true
});
Object.defineProperty(Error.prototype, 'stack', {
configurable: true,
enumerable: true
});
2021-08-23 21:05:17 +02:00
const scribe = require('scribe-js')({
rootPath: config.server.logging.directory,
});
2021-08-23 21:05:17 +02:00
function addLogger(name: string, color: string, active: boolean) {
if (enabled && active) {
procConsole().addLogger(name, color, {
logInConsole: false
});
} else {
// @ts-ignore
procConsole()[name] = function () {
this._reset(); // forget tags, etc. for this logging event
};
}
}
2021-08-23 21:05:17 +02:00
addLogger('debug', 'grey', config.server.logging.debug);
addLogger('profile', 'blue', config.server.logging.profile);
if (enabled && config.server.logging.logRequests) {
app.use(scribe.express.logger());
}
if (config.server.internal.active) {
const prefix = config.server.rootPath === '/' ? '' : config.server.rootPath;
app.use(prefix + '/internal/logs', scribe.webPanel());
}
2021-08-23 21:05:17 +02:00
// Hack to allow correct logging of node.js Error objects.
// See: https://github.com/bluejamesbond/Scribe.js/issues/70
Object.defineProperty(Error.prototype, 'toJSON', {
configurable: true,
value: function () {
const alt: {[key: string]: any} = {};
const storeKey = (key: string) => {
alt[key] = this[key];
};
Object.getOwnPropertyNames(this).forEach(storeKey, this);
return alt;
}
});
// @ts-ignore
2021-08-23 21:05:17 +02:00
for (const key of Object.keys(procConsole())) {
// @ts-ignore
module.exports[key] = enabled ? procConsole()[key] : (...args: any) => {};
}
}
2021-08-23 21:05:17 +02:00
tag(...tags: any): TaggedLogger {
if (this.enabled) {
return {
debug(...args: any): void {
procConsole().tag(...tags).debug(...args);
},
info(...args: any): void {
procConsole().tag(...tags).info(...args);
},
warn(...args: any): void {
procConsole().tag(...tags).warn(...args);
},
error(...args: any): void {
procConsole().tag(...tags).error(...args);
},
profile(...args: any): void {
procConsole().tag(...tags).profile(...args);
},
}
} else {
return noopTaggedLogger;
}
}
2021-08-23 21:05:17 +02:00
}
export default new ActivatableLogger() as Logger;