Skip to content

Commit d73d07f

Browse files
committed
feat: add configurable log level and enhance logging functionality
1 parent 4825af0 commit d73d07f

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ The server will return a response with the specified status code.
7474
- `-e BOTVIEW_BLOCK_URLS_REGEX=""` - Comma or space separated list of regular expressions to block URLs, default empty.
7575
Example: `".*\.ads\..*,.*tracking.*"` will block URLs containing ".ads." or "tracking" anywhere in the URL.
7676

77+
- `-e BOTVIEW_LOG_LEVEL=info` - Log level for the application, default "info".
78+
Available levels: `trace`, `debug`, `info`, `warn`, `error`, `fatal`.
79+
7780
## Troubleshooting Timeout Issues
7881

7982
If you encounter timeout errors during prerendering, it's usually caused by "leaked requests" - network requests that don't complete properly and prevent the page from finishing loading.

src/config.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// array of basic auths in format "url:login:password" (URL-encoding is optional), use comma or space as separator
2+
import { LogLevels } from "./services/json-logger.service";
3+
24
process.env.BASIC_AUTHS ||= void 0;
35
process.env.BOTVIEW_BASIC_AUTHS ||= process.env.BASIC_AUTHS; // BASIC_AUTHS - legacy env
46
process.env.BOTVIEW_NAV_TIMEOUT ||= "30000"; // ms
@@ -10,11 +12,27 @@ process.env.BOTVIEW_BLOCK_FONTS ||= "true"; // true to block fonts
1012
process.env.BOTVIEW_BLOCK_MEDIA ||= "true"; // true to block media
1113
process.env.BOTVIEW_BLOCK_URLS ||= "https://an.yandex.ru, https://mc.yandex.ru"; // comma or space separated list of urls to block (URL-encoding is optional)
1214
process.env.BOTVIEW_BLOCK_URLS_REGEX ||= ""; // comma or space separated list of regexes to block (URL-encoding is optional)
15+
process.env.BOTVIEW_LOG_LEVEL ||= LogLevels.INFO.toString(); // log level: trace, debug, info, warn, error, fatal
16+
17+
function getLogLevelChecked(logLevel: string): number {
18+
// Если это число и оно есть в enum — используем
19+
const numLevel = Number(logLevel);
20+
if (!isNaN(numLevel) && Object.values(LogLevels).includes(numLevel)) {
21+
return numLevel;
22+
}
23+
// Если это строка-ключ enum (например, "INFO")
24+
if (typeof logLevel === "string" && logLevel.toUpperCase() in LogLevels) {
25+
return LogLevels[
26+
logLevel.toUpperCase() as keyof typeof LogLevels
27+
] as number;
28+
}
29+
throw new Error(`Unknown BOTVIEW_LOG_LEVEL: ${logLevel}`);
30+
}
1331

1432
export const config = {
1533
basicAuth: process.env.BOTVIEW_BASIC_AUTHS,
1634
basicAuthParsed: process.env.BOTVIEW_BASIC_AUTHS
17-
? process.env.BOTVIEW_BASIC_AUTHS.split(/[,\s]+/).map((auth) => {
35+
? process.env.BOTVIEW_BASIC_AUTHS.split(/[\s,]+/).map((auth) => {
1836
const parts = decodeURIComponent(auth.trim()).split(":");
1937
return [parts[0], parts[1], parts[2]] as [string, string, string];
2038
})
@@ -27,13 +45,14 @@ export const config = {
2745
blockFonts: process.env.BOTVIEW_BLOCK_FONTS === "true",
2846
blockMedia: process.env.BOTVIEW_BLOCK_MEDIA === "true",
2947
blockUrls: process.env.BOTVIEW_BLOCK_URLS
30-
? process.env.BOTVIEW_BLOCK_URLS.split(/[,\s]+/)
48+
? process.env.BOTVIEW_BLOCK_URLS.split(/[\s,]+/)
3149
.map((url) => decodeURIComponent(url.trim()))
3250
.filter(Boolean)
3351
: [],
3452
blockUrlsRegex: process.env.BOTVIEW_BLOCK_URLS_REGEX
35-
? process.env.BOTVIEW_BLOCK_URLS_REGEX.split(/[,\s]+/)
53+
? process.env.BOTVIEW_BLOCK_URLS_REGEX.split(/[\s,]+/)
3654
.map((regex) => new RegExp(decodeURIComponent(regex.trim())))
3755
.filter(Boolean)
3856
: [],
57+
logLevel: getLogLevelChecked(process.env.BOTVIEW_LOG_LEVEL),
3958
};

src/services/json-logger.service.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LoggerService } from "@nestjs/common";
22
import { hostname } from "os";
3+
import { config } from "../config";
34

45
export enum LogLevels {
56
FATAL = 60,
@@ -12,6 +13,12 @@ export enum LogLevels {
1213

1314
export class JsonLogger implements LoggerService {
1415
protected hostname: string = hostname();
16+
private minLogLevel: number;
17+
18+
constructor() {
19+
// Получаем минимальный уровень логирования из конфигурации
20+
this.minLogLevel = config.logLevel;
21+
}
1522

1623
/**
1724
* Write a 'log' level log.
@@ -27,6 +34,13 @@ export class JsonLogger implements LoggerService {
2734
this.writeJson(message, LogLevels.ERROR);
2835
}
2936

37+
/**
38+
* Write a 'fatal' level log.
39+
*/
40+
public fatal(message: string) {
41+
this.writeJson(message, LogLevels.FATAL);
42+
}
43+
3044
/**
3145
* Write a 'warn' level log.
3246
*/
@@ -48,6 +62,13 @@ export class JsonLogger implements LoggerService {
4862
this.writeJson(message, LogLevels.TRACE);
4963
}
5064

65+
/**
66+
* Write a 'trace' level log.
67+
*/
68+
public trace(message: string) {
69+
this.writeJson(message, LogLevels.TRACE);
70+
}
71+
5172
public extraLogs(
5273
message: string,
5374
level: number,
@@ -61,6 +82,11 @@ export class JsonLogger implements LoggerService {
6182
level: number,
6283
extraProps: object = {},
6384
): void {
85+
// Проверяем, нужно ли выводить лог в зависимости от уровня
86+
if (level < this.minLogLevel) {
87+
return;
88+
}
89+
6490
console.log(
6591
JSON.stringify({
6692
message: message,

0 commit comments

Comments
 (0)