From b155ca655b9c9d52a5dfee2fdcffef2883541299 Mon Sep 17 00:00:00 2001 From: Florian Maunier Date: Tue, 25 Jun 2024 18:07:59 +0200 Subject: [PATCH] wip [skip ci] --- lib/kuzzle/Logger.ts | 93 ++++++++++++++++++++++++++++++++++++++++++++ lib/kuzzle/kuzzle.ts | 2 +- lib/kuzzle/log.js | 74 ----------------------------------- 3 files changed, 94 insertions(+), 75 deletions(-) create mode 100644 lib/kuzzle/Logger.ts delete mode 100644 lib/kuzzle/log.js diff --git a/lib/kuzzle/Logger.ts b/lib/kuzzle/Logger.ts new file mode 100644 index 0000000000..9c3b6e8583 --- /dev/null +++ b/lib/kuzzle/Logger.ts @@ -0,0 +1,93 @@ +/* + * Kuzzle, a backend software, self-hostable and ready to use + * to power modern apps + * + * Copyright 2015-2022 Kuzzle + * mailto: support AT kuzzle.io + * website: http://kuzzle.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { pino, Logger as PinoLogger } from "pino"; + +export class Logger { + private pino: PinoLogger<"silly" | "verbose">; + private failsafeModeString: string; + + constructor() { + this.pino = pino<"silly" | "verbose">({ + customLevels: { + silly: 5, + verbose: 25, + }, + }); + + this.failsafeModeString = global.kuzzle.config.plugins.common.failsafeMode + ? "[FAILSAFE MODE] " + : ""; + } + + trace(message: string) { + this.pino.trace(this.formatMessage(message)); + } + + debug(message: string) { + this.pino.debug(this.formatMessage(message)); + } + + info(message: string) { + this.pino.info(this.formatMessage(message)); + } + + warn(message: string) { + this.pino.warn(this.formatMessage(message)); + } + + error(message: string) { + this.pino.error(this.formatMessage(message)); + } + + fatal(message: string) { + this.pino.fatal(this.formatMessage(message)); + } + + silly(message: string) { + this.pino.warn( + "[DEPRECATED] Logger.silly is deprecated, use Logger.trace instead.", + ); + this.pino.silly(this.formatMessage(message)); + } + + verbose(message: string) { + this.pino.warn( + "[DEPRECATED] Logger.verbose is deprecated, use Logger.debug instead.", + ); + this.pino.verbose(this.formatMessage(message)); + } + + private formatMessage(message: string): string { + const nodeIdString = global.kuzzle.id ? `[${global.kuzzle.id}] ` : ""; + let requestIdString = ""; + + if ( + global.kuzzle.asyncStore.exists() && + global.kuzzle.asyncStore.has("REQUEST") + ) { + const request = global.kuzzle.asyncStore.get("REQUEST"); + + requestIdString = `[${request.id}] `; + } + return `${nodeIdString}${this.failsafeModeString}${requestIdString}${message}`; + } +} diff --git a/lib/kuzzle/kuzzle.ts b/lib/kuzzle/kuzzle.ts index 1285a229ca..37d6807d8b 100644 --- a/lib/kuzzle/kuzzle.ts +++ b/lib/kuzzle/kuzzle.ts @@ -38,7 +38,7 @@ import Router from "../core/network/router"; import Statistics from "../core/statistics"; import { TokenManager } from "../core/auth/tokenManager"; import Validation from "../core/validation"; -import Logger from "./log"; +import { Logger } from "./Logger"; import vault from "./vault"; import DumpGenerator from "./dumpGenerator"; import AsyncStore from "../util/asyncStore"; diff --git a/lib/kuzzle/log.js b/lib/kuzzle/log.js deleted file mode 100644 index 87e0c21d79..0000000000 --- a/lib/kuzzle/log.js +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Kuzzle, a backend software, self-hostable and ready to use - * to power modern apps - * - * Copyright 2015-2022 Kuzzle - * mailto: support AT kuzzle.io - * website: http://kuzzle.io - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -"use strict"; - -class Logger { - constructor() { - this.logMethods = ["info", "warn", "error", "silly", "debug", "verbose"]; - - this.failsafeModeString = global.kuzzle.config.plugins.common.failsafeMode - ? "[FAILSAFE MODE] " - : ""; - - this._useConsole(); - - global.kuzzle.once("core:kuzzleStart", this._useLogger.bind(this)); - } - - _useConsole() { - // until kuzzle has started, use the console to print logs - for (const method of this.logMethods) { - this[method] = (message) => { - /* eslint-disable-next-line no-console */ - const writer = console[method] || console.log; - - writer(`${this.failsafeModeString}${message}`); - }; - } - } - - _useLogger() { - // when kuzzle has started, use the event to dispatch logs - for (const method of this.logMethods) { - this[method] = (message) => { - if ( - global.kuzzle.asyncStore.exists() && - global.kuzzle.asyncStore.has("REQUEST") - ) { - const request = global.kuzzle.asyncStore.get("REQUEST"); - - global.kuzzle.emit( - `log:${method}`, - `[${global.kuzzle.id}] ${this.failsafeModeString}[${request.id}] ${message}`, - ); - } else { - global.kuzzle.emit( - `log:${method}`, - `[${global.kuzzle.id}] ${this.failsafeModeString}${message}`, - ); - } - }; - } - } -} - -module.exports = Logger;