From ebf0d994176f7b80c5b707a4cdadfb9e37cd42fa Mon Sep 17 00:00:00 2001 From: allburov Date: Fri, 29 Mar 2024 15:54:09 +0700 Subject: [PATCH] [core] Add "debug" flag to session config --- docs/site/content/en/docs/how-to/config/index.md | 1 + docs/site/content/en/docs/how-to/sessions/index.md | 14 ++++++++++++++ docs/site/content/en/docs/overview/changelog.md | 3 +++ src/core/manager.core.ts | 10 ++++++---- src/helpers.ts | 9 +++++---- src/main.ts | 2 +- src/structures/sessions.dto.ts | 3 +++ 7 files changed, 33 insertions(+), 9 deletions(-) diff --git a/docs/site/content/en/docs/how-to/config/index.md b/docs/site/content/en/docs/how-to/config/index.md index ec30dcc7..f4537c18 100644 --- a/docs/site/content/en/docs/how-to/config/index.md +++ b/docs/site/content/en/docs/how-to/config/index.md @@ -26,6 +26,7 @@ The following environment variables can be used to configure the WAHA. ### Common - `DEBUG=1`: Set this variable to any value to enable debug and verbose logs. + - You can also do it for a **specific session** by setting `config.debug` field to `true` when starting a session. - `WHATSAPP_API_PORT=3000`: The port number that the HTTP server will listen on. The default value is `3000`. - `WHATSAPP_API_HOSTNAME=localhost`: The hostname for the HTTP server. The default value is `localhost`. diff --git a/docs/site/content/en/docs/how-to/sessions/index.md b/docs/site/content/en/docs/how-to/sessions/index.md index 3a098d6a..f5bfde01 100644 --- a/docs/site/content/en/docs/how-to/sessions/index.md +++ b/docs/site/content/en/docs/how-to/sessions/index.md @@ -84,6 +84,20 @@ Read more about it in [Autostart section](#autostart). You can configure proxy when for all sessions by set up environment variables. Read more about it on [**Configuration page** ->]({{< relref "/docs/how-to/config#proxy" >}}). +**Enable debug** +You can enable debug mode for a session by setting `config.debug` field to `true`. +It'll show you more logs in the console. +Can be useful for debugging purposes when you're experiencing some issues. + +```json +{ + "name": "default", + "config": { + "debug": true + } +} +``` + ### List To get session list - call `GET /api/sessions`. diff --git a/docs/site/content/en/docs/overview/changelog.md b/docs/site/content/en/docs/overview/changelog.md index 233afeae..b7946859 100644 --- a/docs/site/content/en/docs/overview/changelog.md +++ b/docs/site/content/en/docs/overview/changelog.md @@ -21,6 +21,9 @@ You even can **subscribe to get new updates** there! ## 2024.3 ### 🎉 New +---- + +Add `config.debug` field to `POST /api/session/start` to enable debug and verbose logs for the session. ---- diff --git a/src/core/manager.core.ts b/src/core/manager.core.ts index 565611fb..f744dfde 100644 --- a/src/core/manager.core.ts +++ b/src/core/manager.core.ts @@ -1,6 +1,7 @@ import { ConsoleLogger, Injectable, + LogLevel, NotFoundException, UnprocessableEntityException, } from '@nestjs/common'; @@ -38,8 +39,8 @@ export class OnlyDefaultSessionIsAllowed extends UnprocessableEntityException { } } -export function buildLogger(name) { - return new ConsoleLogger(name, { logLevels: getLogLevels() }); +export function buildLogger(name: string, levels: LogLevel[]) { + return new ConsoleLogger(name, { logLevels: levels }); } @Injectable() @@ -107,12 +108,13 @@ export class SessionManagerCore extends SessionManager { const name = request.name; this.log.log(`'${name}' - starting session...`); - const log = buildLogger(`WhatsappSession - ${name}`); + const levels = getLogLevels(request.config?.debug); + const log = buildLogger(`WhatsappSession - ${name}`, levels); const mediaManager = new CoreMediaManager( new MediaStorageCore(), this.config.mimetypes, ); - const webhookLog = buildLogger(`Webhook - ${name}`); + const webhookLog = buildLogger(`Webhook - ${name}`, levels); const webhook = new this.WebhookConductorClass(webhookLog); const proxyConfig = this.getProxyConfig(request); const sessionConfig: SessionParams = { diff --git a/src/helpers.ts b/src/helpers.ts index 2934f24a..80c28905 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -33,10 +33,11 @@ function splitAt(str: string, index) { return [fst.join(''), snd.join('')]; } -function getLogLevels(): LogLevel[] { - return process.env.DEBUG != undefined - ? ['log', 'debug', 'error', 'verbose', 'warn'] - : ['log', 'error', 'warn', 'verbose']; +function getLogLevels(debug: boolean): LogLevel[] { + const enableDebug = process.env.DEBUG != undefined || debug; + return enableDebug + ? ['log', 'error', 'warn', 'debug', 'verbose'] + : ['log', 'error', 'warn']; } export { flipObject, getLogLevels, parseBool, splitAt }; diff --git a/src/main.ts b/src/main.ts index 173e94e3..f8648d11 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,7 +31,7 @@ async function loadModules(): Promise< async function bootstrap() { const [AppModule, SwaggerModule] = await loadModules(); const app = await NestFactory.create(AppModule, { - logger: getLogLevels(), + logger: getLogLevels(false), }); app.enableShutdownHooks(); diff --git a/src/structures/sessions.dto.ts b/src/structures/sessions.dto.ts index 67837a72..0add6ded 100644 --- a/src/structures/sessions.dto.ts +++ b/src/structures/sessions.dto.ts @@ -39,10 +39,13 @@ export class ProxyConfig { export class SessionConfig { webhooks?: WebhookConfig[]; + @ApiProperty({ example: null, }) proxy?: ProxyConfig; + + debug: boolean = false; } export class SessionStartRequest {