From 206718032813c6dfde93e545d5b1e134784d2aa7 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 10 Dec 2022 15:45:30 +0900 Subject: [PATCH] improve nest logger --- packages/backend/src/NestLogger.ts | 49 +++++++++++++++++++++++++++++ packages/backend/src/boot/master.ts | 5 ++- packages/backend/src/boot/worker.ts | 5 ++- packages/backend/src/logger.ts | 22 ++++++------- 4 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 packages/backend/src/NestLogger.ts diff --git a/packages/backend/src/NestLogger.ts b/packages/backend/src/NestLogger.ts new file mode 100644 index 0000000000..448098b831 --- /dev/null +++ b/packages/backend/src/NestLogger.ts @@ -0,0 +1,49 @@ +import { LoggerService } from '@nestjs/common'; +import Logger from '@/logger.js'; + +const logger = new Logger('core', 'cyan'); +const nestLogger = logger.createSubLogger('nest', 'green', false); + +export class NestLogger implements LoggerService { + /** + * Write a 'log' level log. + */ + log(message: any, ...optionalParams: any[]) { + const ctx = optionalParams[0]; + nestLogger.info(ctx + ': ' + message); + } + + /** + * Write an 'error' level log. + */ + error(message: any, ...optionalParams: any[]) { + const ctx = optionalParams[0]; + nestLogger.error(ctx + ': ' + message); + } + + /** + * Write a 'warn' level log. + */ + warn(message: any, ...optionalParams: any[]) { + const ctx = optionalParams[0]; + nestLogger.warn(ctx + ': ' + message); + } + + /** + * Write a 'debug' level log. + */ + debug?(message: any, ...optionalParams: any[]) { + if (process.env.NODE_ENV === 'production') return; + const ctx = optionalParams[0]; + nestLogger.debug(ctx + ': ' + message); + } + + /** + * Write a 'verbose' level log. + */ + verbose?(message: any, ...optionalParams: any[]) { + if (process.env.NODE_ENV === 'production') return; + const ctx = optionalParams[0]; + nestLogger.debug(ctx + ': ' + message); + } +} diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index fc07510ffb..4630217c4c 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -16,6 +16,7 @@ import { DaemonModule } from '@/daemons/DaemonModule.js'; import { JanitorService } from '@/daemons/JanitorService.js'; import { QueueStatsService } from '@/daemons/QueueStatsService.js'; import { ServerStatsService } from '@/daemons/ServerStatsService.js'; +import { NestLogger } from '@/NestLogger.js'; import { envOption } from '../env.js'; const _filename = fileURLToPath(import.meta.url); @@ -78,7 +79,9 @@ export async function masterMain() { bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`, null, true); if (!envOption.noDaemons) { - const daemons = await NestFactory.createApplicationContext(DaemonModule); + const daemons = await NestFactory.createApplicationContext(DaemonModule, { + logger: new NestLogger(), + }); daemons.enableShutdownHooks(); daemons.get(JanitorService).start(); daemons.get(QueueStatsService).start(); diff --git a/packages/backend/src/boot/worker.ts b/packages/backend/src/boot/worker.ts index 1a5f1ee81a..f29e37de78 100644 --- a/packages/backend/src/boot/worker.ts +++ b/packages/backend/src/boot/worker.ts @@ -4,13 +4,16 @@ import { envOption } from '@/env.js'; import { ChartManagementService } from '@/core/chart/ChartManagementService.js'; import { ServerService } from '@/server/ServerService.js'; import { QueueProcessorService } from '@/queue/QueueProcessorService.js'; +import { NestLogger } from '@/NestLogger.js'; import { RootModule } from '../RootModule.js'; /** * Init worker process */ export async function workerMain() { - const app = await NestFactory.createApplicationContext(RootModule); + const app = await NestFactory.createApplicationContext(RootModule, { + logger: new NestLogger(), + }); app.enableShutdownHooks(); // start server diff --git a/packages/backend/src/logger.ts b/packages/backend/src/logger.ts index d09b479c42..5cc0d9795c 100644 --- a/packages/backend/src/logger.ts +++ b/packages/backend/src/logger.ts @@ -5,7 +5,7 @@ import { format as dateFormat } from 'date-fns'; import { bindThis } from '@/decorators.js'; import { envOption } from './env.js'; -type Domain = { +type Context = { name: string; color?: string; }; @@ -13,14 +13,14 @@ type Domain = { type Level = 'error' | 'success' | 'warning' | 'debug' | 'info'; export default class Logger { - private domain: Domain; + private context: Context; private parentLogger: Logger | null = null; private store: boolean; private syslogClient: any | null = null; - constructor(domain: string, color?: string, store = true, syslogClient = null) { - this.domain = { - name: domain, + constructor(context: string, color?: string, store = true, syslogClient = null) { + this.context = { + name: context, color: color, }; this.store = store; @@ -28,20 +28,20 @@ export default class Logger { } @bindThis - public createSubLogger(domain: string, color?: string, store = true): Logger { - const logger = new Logger(domain, color, store); + public createSubLogger(context: string, color?: string, store = true): Logger { + const logger = new Logger(context, color, store); logger.parentLogger = this; return logger; } @bindThis - private log(level: Level, message: string, data?: Record | null, important = false, subDomains: Domain[] = [], store = true): void { + private log(level: Level, message: string, data?: Record | null, important = false, subContexts: Context[] = [], store = true): void { if (envOption.quiet) return; if (!this.store) store = false; if (level === 'debug') store = false; if (this.parentLogger) { - this.parentLogger.log(level, message, data, important, [this.domain].concat(subDomains), store); + this.parentLogger.log(level, message, data, important, [this.context].concat(subContexts), store); return; } @@ -54,7 +54,7 @@ export default class Logger { level === 'debug' ? chalk.gray('VERB') : level === 'info' ? chalk.blue('INFO') : null; - const domains = [this.domain].concat(subDomains).map(d => d.color ? chalk.rgb(...convertColor.keyword.rgb(d.color))(d.name) : chalk.white(d.name)); + const contexts = [this.context].concat(subContexts).map(d => d.color ? chalk.rgb(...convertColor.keyword.rgb(d.color))(d.name) : chalk.white(d.name)); const m = level === 'error' ? chalk.red(message) : level === 'warning' ? chalk.yellow(message) : @@ -63,7 +63,7 @@ export default class Logger { level === 'info' ? message : null; - let log = `${l} ${worker}\t[${domains.join(' ')}]\t${m}`; + let log = `${l} ${worker}\t[${contexts.join(' ')}]\t${m}`; if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log; console.log(important ? chalk.bold(log) : log);