-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathlog-level.service.ts
More file actions
97 lines (78 loc) · 3.05 KB
/
log-level.service.ts
File metadata and controls
97 lines (78 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { FeatureFlagsKeysEnum } from '@novu/shared';
import { PinoLogger } from 'nestjs-pino';
import { getLogLevel, loggingLevelArr } from '../../logging';
import { FeatureFlagsService } from '../feature-flags';
const LOG_CONTEXT = 'LogLevelService';
const DEFAULT_POLLING_INTERVAL_MS = 60_000; // one minute
@Injectable()
export class LogLevelService implements OnModuleInit, OnModuleDestroy {
private pollingInterval: NodeJS.Timeout | null = null;
private currentLogLevel: string;
private readonly pollingIntervalMs: number;
constructor(private featureFlagsService: FeatureFlagsService) {
this.pollingIntervalMs = Number(process.env.LOG_LEVEL_POLLING_INTERVAL_MS) || DEFAULT_POLLING_INTERVAL_MS;
this.currentLogLevel = getLogLevel();
}
async onModuleInit(): Promise<void> {
await this.updateLogLevel();
this.pollingInterval = setInterval(async () => {
try {
await this.updateLogLevel();
} catch (error) {
Logger.error(`Failed to update log level: ${(error as Error).message}`, (error as Error).stack, LOG_CONTEXT);
}
}, this.pollingIntervalMs);
Logger.log(`Log level polling started with interval of ${this.pollingIntervalMs}ms`, LOG_CONTEXT);
}
onModuleDestroy(): void {
if (this.pollingInterval) {
clearInterval(this.pollingInterval);
this.pollingInterval = null;
Logger.log('Log level polling stopped', LOG_CONTEXT);
}
}
private async updateLogLevel(): Promise<void> {
const logLevelFromFlag = await this.getLogLevelFromFeatureFlag();
const newLogLevel = logLevelFromFlag || this.getFallbackLogLevel();
if (!this.isValidLogLevel(newLogLevel)) {
Logger.warn(
`Invalid log level "${newLogLevel}". Valid levels: ${loggingLevelArr.join(', ')}. Keeping current level: ${this.currentLogLevel}`,
LOG_CONTEXT
);
return;
}
if (newLogLevel !== this.currentLogLevel) {
this.setLogLevel(newLogLevel);
Logger.log(`Log level changed from "${this.currentLogLevel}" to "${newLogLevel}"`, LOG_CONTEXT);
this.currentLogLevel = newLogLevel;
}
}
private async getLogLevelFromFeatureFlag(): Promise<string | undefined> {
try {
const flagValue = await this.featureFlagsService.getFlag<string | undefined>({
key: FeatureFlagsKeysEnum.LOG_LEVEL_STR,
defaultValue: undefined,
user: { _id: 'system' },
});
if (flagValue && flagValue !== 'undefined') {
return flagValue;
}
return undefined;
} catch (error) {
Logger.warn(`Failed to get log level from feature flag: ${(error as Error).message}`, LOG_CONTEXT);
return undefined;
}
}
private getFallbackLogLevel(): string {
return process.env.LOG_LEVEL || process.env.LOGGING_LEVEL || 'info';
}
private isValidLogLevel(level: string): boolean {
return loggingLevelArr.includes(level);
}
private setLogLevel(level: string): void {
if (PinoLogger.root) {
PinoLogger.root.level = level;
}
}
}