Allow to completely disable logging.

This commit is contained in:
baldo 2021-08-23 21:05:17 +02:00
parent b2e2d93bb0
commit 12a13f47bf
4 changed files with 94 additions and 74 deletions

View file

@ -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.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.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.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.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` * **`server.logging.profile`** Gibt an, ob Profiling-Output geloggt werden soll (Achtung, viel!), z. B.: `false`

View file

@ -7,6 +7,7 @@
"peersPath": "/tmp/peers", "peersPath": "/tmp/peers",
"logging": { "logging": {
"enabled": true,
"directory": "/tmp/logs", "directory": "/tmp/logs",
"debug": false, "debug": false,
"profile": false, "profile": false,

View file

@ -5,90 +5,106 @@ function procConsole() {
return process.console; return process.console;
} }
function init(): void { const noopTaggedLogger: TaggedLogger = {
const app = require('./app').app; debug(...args: any): void {},
const config = require('./config').config; info(...args: any): void {},
warn(...args: any): void {},
error(...args: any): void {},
profile(...args: any): void {},
};
// Hack to allow proper logging of Error. class ActivatableLogger implements Logger {
Object.defineProperty(Error.prototype, 'message', { private enabled: boolean = false;
configurable: true,
enumerable: true
});
Object.defineProperty(Error.prototype, 'stack', {
configurable: true,
enumerable: true
});
init(): void {
const app = require('./app').app;
const config = require('./config').config;
const scribe = require('scribe-js')({ const enabled = config.server.logging.enabled;
rootPath: config.server.logging.directory, this.enabled = enabled;
});
function addLogger(name: string, color: string, active: boolean) { // Hack to allow proper logging of Error.
if (active) { Object.defineProperty(Error.prototype, 'message', {
procConsole().addLogger(name, color, { configurable: true,
logInConsole: false enumerable: true
}); });
} else { Object.defineProperty(Error.prototype, 'stack', {
// @ts-ignore configurable: true,
procConsole()[name] = function () { enumerable: true
this._reset(); // forget tags, etc. for this logging event });
};
const scribe = require('scribe-js')({
rootPath: config.server.logging.directory,
});
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
};
}
} }
}
addLogger('debug', 'grey', config.server.logging.debug); addLogger('debug', 'grey', config.server.logging.debug);
addLogger('profile', 'blue', config.server.logging.profile); addLogger('profile', 'blue', config.server.logging.profile);
if (config.server.logging.logRequests) { if (enabled && config.server.logging.logRequests) {
app.use(scribe.express.logger()); app.use(scribe.express.logger());
} }
if (config.server.internal.active) { if (config.server.internal.active) {
const prefix = config.server.rootPath === '/' ? '' : config.server.rootPath; const prefix = config.server.rootPath === '/' ? '' : config.server.rootPath;
app.use(prefix + '/internal/logs', scribe.webPanel()); app.use(prefix + '/internal/logs', scribe.webPanel());
}
// 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 // Hack to allow correct logging of node.js Error objects.
for (const key of Object.keys(procConsole())) { // 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 // @ts-ignore
module.exports[key] = procConsole()[key]; for (const key of Object.keys(procConsole())) {
// @ts-ignore
module.exports[key] = enabled ? procConsole()[key] : (...args: any) => {};
}
}
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;
}
} }
} }
const logger: Logger = { export default new ActivatableLogger() as Logger;
init,
tag(...tags): TaggedLogger {
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);
},
}
}
};
export default logger;

View file

@ -6,6 +6,7 @@ export type Version = string;
export class LoggingConfig { export class LoggingConfig {
constructor( constructor(
@Field("enabled") public enabled: boolean,
@Field("directory") public directory: string, @Field("directory") public directory: string,
@Field("debug") public debug: boolean, @Field("debug") public debug: boolean,
@Field("profile") public profile: boolean, @Field("profile") public profile: boolean,