Skip to content

Commit

Permalink
[core] Add "debug" flag to session config
Browse files Browse the repository at this point in the history
  • Loading branch information
allburov committed Mar 29, 2024
1 parent 423d893 commit ebf0d99
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/site/content/en/docs/how-to/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
14 changes: 14 additions & 0 deletions docs/site/content/en/docs/how-to/sessions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
3 changes: 3 additions & 0 deletions docs/site/content/en/docs/overview/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

----

Expand Down
10 changes: 6 additions & 4 deletions src/core/manager.core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ConsoleLogger,
Injectable,
LogLevel,
NotFoundException,
UnprocessableEntityException,
} from '@nestjs/common';
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = {
Expand Down
9 changes: 5 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/structures/sessions.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ export class ProxyConfig {

export class SessionConfig {
webhooks?: WebhookConfig[];

@ApiProperty({
example: null,
})
proxy?: ProxyConfig;

debug: boolean = false;
}

export class SessionStartRequest {
Expand Down

0 comments on commit ebf0d99

Please sign in to comment.