Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(api): read the log level and environment from the process env (#1824)

Hampus0d96a4574a01
diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts
index 2160f39a7..09c83afc4 100644
--- a/packages/logger/src/Logger.ts
+++ b/packages/logger/src/Logger.ts
@@ -20,14 +20,35 @@ interface PinoTransportOptions {
};
}
+const PINO_LEVELS: ReadonlyArray<pino.Level> = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
+
function isDevelopment(environment: string): boolean {
return environment === 'development';
}
+function isPinoLevel(value: string): value is pino.Level {
+ return (PINO_LEVELS as ReadonlyArray<string>).includes(value);
+}
+
+function resolveEnvironment(options: LoggerOptions): string {
+ return options.environment ?? process.env.FLUXER_ENV ?? 'production';
+}
+
+function resolveLevel(options: LoggerOptions, isDev: boolean): pino.Level {
+ if (options.level) {
+ return options.level;
+ }
+ const configured = process.env.LOG_LEVEL?.trim().toLowerCase();
+ if (configured && isPinoLevel(configured)) {
+ return configured;
+ }
+ return isDev ? 'debug' : 'info';
+}
+
function createPinoLogger(serviceName: string, options: LoggerOptions = {}): PinoLogger {
- const environment = options.environment ?? 'production';
+ const environment = resolveEnvironment(options);
const isDev = isDevelopment(environment);
- const level = options.level ?? (isDev ? 'debug' : 'info');
+ const level = resolveLevel(options, isDev);
const streams: Array<pino.StreamEntry> = [];
if (isDev) {
try {