From ec914d9f0c40fe443110481d45ab1bf4fbaa426b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Mon, 8 Jan 2024 20:26:39 +0900 Subject: [PATCH] =?UTF-8?q?feat(logging):=20JSON=E5=BD=A2=E5=BC=8F?= =?UTF-8?q?=E3=81=AE=E6=A7=8B=E9=80=A0=E5=8C=96=E3=83=AD=E3=82=B0=E3=81=A7?= =?UTF-8?q?=E3=83=AD=E3=82=B0=E3=82=92=E5=87=BA=E5=8A=9B=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=20(MisskeyIO#346)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/boot/master.ts | 4 ++-- packages/backend/src/env.ts | 1 + packages/backend/src/logger.ts | 15 ++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index 30f9477ccf..0b417b077e 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -28,7 +28,7 @@ const bootLogger = logger.createSubLogger('boot', 'magenta', false); const themeColor = chalk.hex('#86b300'); function greet() { - if (!envOption.quiet) { + if (!envOption.quiet && !envOption.logJson) { //#region Misskey logo const v = `v${meta.version}`; console.log(themeColor(' _____ _ _ ')); @@ -46,7 +46,7 @@ function greet() { } bootLogger.info('Welcome to Misskey!'); - bootLogger.info(`Misskey v${meta.version}`, null, true); + bootLogger.info(`Misskey v${meta.version}`, { version: meta.version, hostname: os.hostname(), pid: process.pid }, true); } /** diff --git a/packages/backend/src/env.ts b/packages/backend/src/env.ts index ba44cfa2e6..317b35b652 100644 --- a/packages/backend/src/env.ts +++ b/packages/backend/src/env.ts @@ -9,6 +9,7 @@ const envOption = { noDaemons: false, disableClustering: false, verbose: false, + logJson: false, withLogTime: false, quiet: false, }; diff --git a/packages/backend/src/logger.ts b/packages/backend/src/logger.ts index c6a0436c60..3c6cb1009d 100644 --- a/packages/backend/src/logger.ts +++ b/packages/backend/src/logger.ts @@ -42,7 +42,7 @@ export default class Logger { @bindThis private log(level: Level, message: string, data?: Record | null, important = false, subContexts: Context[] = [], store = true): void { - if (envOption.quiet) return; + if (envOption.quiet && !envOption.logJson) return; if (!this.store) store = false; if (level === 'debug') store = false; @@ -51,6 +51,19 @@ export default class Logger { return; } + if (envOption.logJson) { + console.log(JSON.stringify({ + time: new Date().toISOString(), + level: level, + message: message, + data: data, + important: important, + context: [this.context].concat(subContexts).join('.'), + cluster: cluster.isPrimary ? 'primary' : `worker-${cluster.worker!.id}`, + })); + return; + } + const time = dateFormat(new Date(), 'HH:mm:ss'); const worker = cluster.isPrimary ? '*' : cluster.worker!.id; const l =