feat(web,server)!: runtime log level (#5672)

* feat: change log level at runtime

* chore: open api

* chore: prefer env over runtime

* chore: remove default env value
This commit is contained in:
Jason Rasmussen 2023-12-14 11:55:40 -05:00 committed by GitHub
parent f2270ad757
commit 9768931275
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 771 additions and 117 deletions

View file

@ -1,4 +1,8 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import { LogLevel, SystemConfig } from '@app/infra/entities';
import { ImmichLogger } from '@app/infra/logger';
import { Inject, Injectable } from '@nestjs/common';
import { instanceToPlain } from 'class-transformer';
import _ from 'lodash';
import {
ClientEvent,
ICommunicationRepository,
@ -22,7 +26,7 @@ import { SystemConfigCore, SystemConfigValidator } from './system-config.core';
@Injectable()
export class SystemConfigService {
private logger = new Logger(SystemConfigService.name);
private logger = new ImmichLogger(SystemConfigService.name);
private core: SystemConfigCore;
constructor(
@ -32,6 +36,13 @@ export class SystemConfigService {
) {
this.core = SystemConfigCore.create(repository);
this.communicationRepository.on(ServerEvent.CONFIG_UPDATE, () => this.handleConfigUpdate());
this.core.config$.subscribe((config) => this.setLogLevel(config));
this.core.addValidator((newConfig, oldConfig) => this.validateConfig(newConfig, oldConfig));
}
async init() {
const config = await this.core.getConfig();
await this.setLogLevel(config);
}
get config$() {
@ -106,4 +117,22 @@ export class SystemConfigService {
private async handleConfigUpdate() {
await this.core.refreshConfig();
}
private async setLogLevel({ logging }: SystemConfig) {
const envLevel = this.getEnvLogLevel();
const configLevel = logging.enabled ? logging.level : false;
const level = envLevel ? envLevel : configLevel;
ImmichLogger.setLogLevel(level);
this.logger.log(`LogLevel=${level} ${envLevel ? '(set via LOG_LEVEL)' : '(set via system config)'}`);
}
private getEnvLogLevel() {
return process.env.LOG_LEVEL as LogLevel;
}
private async validateConfig(newConfig: SystemConfig, oldConfig: SystemConfig) {
if (!_.isEqual(instanceToPlain(newConfig.logging), oldConfig.logging) && this.getEnvLogLevel()) {
throw new Error('Logging cannot be changed while the environment variable LOG_LEVEL is set.');
}
}
}