From 05baa89508308f71f1869b128ac95f1921004e2d Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 3 Feb 2019 01:24:59 +0900 Subject: [PATCH] Refactoring of logger --- src/index.ts | 14 +++++++------- src/misc/logger.ts | 9 +++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 28bc9f3d4..1c54246c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,8 +26,8 @@ import { lessThan } from './prelude/array'; import * as pkg from '../package.json'; const logger = new Logger('core'); -const bootLogger = new Logger('boot', logger); -const clusterLog = new Logger('cluster', logger); +const bootLogger = logger.createSubLogger('boot'); +const clusterLog = logger.createSubLogger('cluster'); const ev = new Xev(); if (process.env.NODE_ENV != 'production' && process.env.DEBUG == null) { @@ -116,7 +116,7 @@ async function isPortAvailable(port: number): Promise { } async function showMachine() { - const logger = new Logger('Machine', bootLogger); + const logger = bootLogger.createSubLogger('machine'); logger.info(`Hostname: ${os.hostname()}`); logger.info(`Platform: ${process.platform}`); logger.info(`Architecture: ${process.arch}`); @@ -129,7 +129,7 @@ async function showMachine() { function showEnvironment(): void { const env = process.env.NODE_ENV; - const logger = new Logger('Env', bootLogger); + const logger = bootLogger.createSubLogger('env'); logger.info(typeof env == 'undefined' ? 'NODE_ENV is not set' : `NODE_ENV: ${env}`); if (env !== 'production') { @@ -147,7 +147,7 @@ async function init(): Promise { bootLogger.info('Welcome to Misskey!'); bootLogger.info(`<<< Misskey v${pkg.version} >>>`); - const nodejsLogger = new Logger('Nodejs', bootLogger); + const nodejsLogger = bootLogger.createSubLogger('nodejs'); nodejsLogger.info(`Version ${runningNodejsVersion.join('.')}`); @@ -159,7 +159,7 @@ async function init(): Promise { await showMachine(); showEnvironment(); - const configLogger = new Logger('Config', bootLogger); + const configLogger = bootLogger.createSubLogger('config'); let config; try { @@ -202,7 +202,7 @@ async function init(): Promise { const requiredMongoDBVersion = [3, 6]; function checkMongoDB(config: Config) { - const mongoDBLogger = new Logger('MongoDB', bootLogger); + const mongoDBLogger = bootLogger.createSubLogger('db'); const u = config.mongodb.user ? encodeURIComponent(config.mongodb.user) : null; const p = config.mongodb.pass ? encodeURIComponent(config.mongodb.pass) : null; const uri = `mongodb://${u && p ? `${u}:****@` : ''}${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}`; diff --git a/src/misc/logger.ts b/src/misc/logger.ts index 402660d97..0dbda4ca6 100644 --- a/src/misc/logger.ts +++ b/src/misc/logger.ts @@ -5,9 +5,14 @@ export default class Logger { private domain: string; private parentLogger: Logger; - constructor(domain: string, parentLogger?: Logger) { + constructor(domain: string) { this.domain = domain; - this.parentLogger = parentLogger; + } + + public createSubLogger(domain: string): Logger { + const logger = new Logger(domain); + logger.parentLogger = this; + return logger; } public log(level: string, message: string, important = false): void {