Allow to completely disable logging.
This commit is contained in:
parent
b2e2d93bb0
commit
12a13f47bf
|
@ -114,6 +114,8 @@ Dann die `config.json` anpassen nach belieben. Es gibt die folgenden Konfigurati
|
|||
* **`server.databaseFile`** Pfad zur Datenbank-Datei, z. B.: `"$FFFFNG_HOME/ffffng.sqlite"`
|
||||
* **`server.peersPath`** Verzeichnis unter dem die `fastd` Key-Files angelegt werden, z. B.: `"$FFFFNG_HOME/fastdkeys"`
|
||||
|
||||
* **`server.logging.enabled`** Ob geloggt werden soll (Achtung: `false` sorgt dafür, dass überhaupt kein Output mehr auf
|
||||
`stdout` oder in den Logs erscheint und überschreibt damit alle anderen Logging-Optionen!), z. B.: `false`
|
||||
* **`server.logging.directory`** Verzeichnis unter dem Log-Files abgelegt werden, z. B.: `"$FFFFNG_HOME/logs"`
|
||||
* **`server.logging.debug`** Gibt an, ob Debug-Output geloggt werden soll (Achtung, viel!), z. B.: `false`
|
||||
* **`server.logging.profile`** Gibt an, ob Profiling-Output geloggt werden soll (Achtung, viel!), z. B.: `false`
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
"peersPath": "/tmp/peers",
|
||||
|
||||
"logging": {
|
||||
"enabled": true,
|
||||
"directory": "/tmp/logs",
|
||||
"debug": false,
|
||||
"profile": false,
|
||||
|
|
|
@ -5,10 +5,24 @@ function procConsole() {
|
|||
return process.console;
|
||||
}
|
||||
|
||||
function init(): void {
|
||||
const noopTaggedLogger: TaggedLogger = {
|
||||
debug(...args: any): void {},
|
||||
info(...args: any): void {},
|
||||
warn(...args: any): void {},
|
||||
error(...args: any): void {},
|
||||
profile(...args: any): void {},
|
||||
};
|
||||
|
||||
class ActivatableLogger implements Logger {
|
||||
private enabled: boolean = false;
|
||||
|
||||
init(): void {
|
||||
const app = require('./app').app;
|
||||
const config = require('./config').config;
|
||||
|
||||
const enabled = config.server.logging.enabled;
|
||||
this.enabled = enabled;
|
||||
|
||||
// Hack to allow proper logging of Error.
|
||||
Object.defineProperty(Error.prototype, 'message', {
|
||||
configurable: true,
|
||||
|
@ -19,13 +33,12 @@ function init(): void {
|
|||
enumerable: true
|
||||
});
|
||||
|
||||
|
||||
const scribe = require('scribe-js')({
|
||||
rootPath: config.server.logging.directory,
|
||||
});
|
||||
|
||||
function addLogger(name: string, color: string, active: boolean) {
|
||||
if (active) {
|
||||
if (enabled && active) {
|
||||
procConsole().addLogger(name, color, {
|
||||
logInConsole: false
|
||||
});
|
||||
|
@ -40,7 +53,7 @@ function init(): void {
|
|||
addLogger('debug', 'grey', config.server.logging.debug);
|
||||
addLogger('profile', 'blue', config.server.logging.profile);
|
||||
|
||||
if (config.server.logging.logRequests) {
|
||||
if (enabled && config.server.logging.logRequests) {
|
||||
app.use(scribe.express.logger());
|
||||
}
|
||||
if (config.server.internal.active) {
|
||||
|
@ -65,13 +78,12 @@ function init(): void {
|
|||
// @ts-ignore
|
||||
for (const key of Object.keys(procConsole())) {
|
||||
// @ts-ignore
|
||||
module.exports[key] = procConsole()[key];
|
||||
module.exports[key] = enabled ? procConsole()[key] : (...args: any) => {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const logger: Logger = {
|
||||
init,
|
||||
tag(...tags): TaggedLogger {
|
||||
tag(...tags: any): TaggedLogger {
|
||||
if (this.enabled) {
|
||||
return {
|
||||
debug(...args: any): void {
|
||||
procConsole().tag(...tags).debug(...args);
|
||||
|
@ -89,6 +101,10 @@ const logger: Logger = {
|
|||
procConsole().tag(...tags).profile(...args);
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return noopTaggedLogger;
|
||||
}
|
||||
};
|
||||
export default logger;
|
||||
}
|
||||
}
|
||||
|
||||
export default new ActivatableLogger() as Logger;
|
||||
|
|
|
@ -6,6 +6,7 @@ export type Version = string;
|
|||
|
||||
export class LoggingConfig {
|
||||
constructor(
|
||||
@Field("enabled") public enabled: boolean,
|
||||
@Field("directory") public directory: string,
|
||||
@Field("debug") public debug: boolean,
|
||||
@Field("profile") public profile: boolean,
|
||||
|
|
Loading…
Reference in a new issue