From ccba7586cfdca4b9d15c3ed00d5f7ee618cb9bab Mon Sep 17 00:00:00 2001 From: Renato Santos Date: Mon, 12 Aug 2024 21:08:11 -0300 Subject: [PATCH] [feature] logs remote loki --- package.json | 3 ++- src/config.ts | 3 ++- src/types/ServerOptions.ts | 3 +++ src/util/logger.ts | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 211ec0bd5e..2f4c1ce328 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,8 @@ "swagger-autogen": "^2.23.7", "swagger-ui-express": "^5.0.1", "unzipper": "^0.12.3", - "winston": "^3.14.1" + "winston": "^3.14.1", + "winston-loki": "^6.1.2" }, "devDependencies": { "@babel/cli": "^7.24.8", diff --git a/src/config.ts b/src/config.ts index 83b3438119..bc9521104b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -41,7 +41,8 @@ export default { }, log: { level: 'silly', // Before open a issue, change level to silly and retry a action - logger: ['console', 'file'], + logger: ['console', 'file', 'loki'], + loki: 'http://localhost:3100', }, createOptions: { browserArgs: [ diff --git a/src/types/ServerOptions.ts b/src/types/ServerOptions.ts index 50e6ae0cb6..84b0a39bf3 100644 --- a/src/types/ServerOptions.ts +++ b/src/types/ServerOptions.ts @@ -37,6 +37,9 @@ export interface ServerOptions { log: { level: string; logger: string[]; + loki?: { + host: string; // URL do Loki + }; }; createOptions: { browserArgs: string[]; diff --git a/src/util/logger.ts b/src/util/logger.ts index 41acf89c81..3a4c5c4367 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import winston from 'winston'; - +import LokiTransport from 'winston-loki'; // Use JSON logging for log files // Here winston.format.errors() just seem to work // because there is no winston.format.simple() @@ -62,5 +62,37 @@ export function createLogger(options: any) { ); } + if (options.logger.includes('loki')) { + logger.add( + new LokiTransport({ + host: 'http://localhost:3100', + json: true, + labels: { job: 'wppconnect-server' }, + format: winston.format.combine( + winston.format.errors({ stack: true }), + winston.format.timestamp(), + winston.format.printf(({ level, message, timestamp, stack }) => { + if (stack) { + return JSON.stringify({ + level: level, + timestamp: timestamp, + message: message, + stack: stack, + job: 'wppconnect-server', + }); + } + return JSON.stringify({ + level: level, + timestamp: timestamp, + message: message, + job: 'wppconnect-server', + }); + }) + ), + level: log_level, + }) + ); + } + return logger; -} +} \ No newline at end of file