From d50e99c17bddc04ca7a09b85a70603ff5404739a Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Sat, 14 Jul 2018 20:58:21 +0900 Subject: [PATCH] Update log messages --- package-lock.json | 23 +++++++++++-------- package.json | 2 ++ src/config/load.ts | 7 +----- src/index.ts | 24 ++++++++++---------- src/misc/dependencyInfo.ts | 2 +- src/misc/logger.ts | 46 ++++++++++++++++++-------------------- 6 files changed, 52 insertions(+), 52 deletions(-) diff --git a/package-lock.json b/package-lock.json index 32bd513db..ad5fdb89a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "misskey", - "version": "4.17.1", + "version": "4.19.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -154,6 +154,11 @@ "@types/node": "*" } }, + "@types/dateformat": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@types/dateformat/-/dateformat-1.0.1.tgz", + "integrity": "sha1-LlsjXIxVZSxP7ChFBtKjb+Zf6H4=" + }, "@types/debug": { "version": "0.0.30", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-0.0.30.tgz", @@ -164,11 +169,6 @@ "resolved": "https://registry.npmjs.org/@types/deep-equal/-/deep-equal-1.0.1.tgz", "integrity": "sha512-mMUu4nWHLBlHtxXY17Fg6+ucS/MnndyOWyOe7MmwkoMYxvfQU2ajtRaEvqSUv+aVkMqH/C0NCI8UoVfRNQ10yg==" }, - "@types/elasticsearch": { - "version": "5.0.24", - "resolved": "https://registry.npmjs.org/@types/elasticsearch/-/elasticsearch-5.0.24.tgz", - "integrity": "sha512-QRpGleGwKv70hEcdklBh3HiLZ3OHPp40nRiVfhLk9wlQ4+V//SX+n90uIHN/mfKz828bjSSAxSG/kDUEp4Yp8Q==" - }, "@types/events": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@types/events/-/events-1.2.0.tgz", @@ -3729,9 +3729,9 @@ "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=" }, "dateformat": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-2.2.0.tgz", - "integrity": "sha1-QGXiATz5+5Ft39gu+1Bq1MZ2kGI=" + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-3.0.3.tgz", + "integrity": "sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==" }, "de-indent": { "version": "1.0.2", @@ -7723,6 +7723,11 @@ "supports-color": "^2.0.0" } }, + "dateformat": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-2.2.0.tgz", + "integrity": "sha1-QGXiATz5+5Ft39gu+1Bq1MZ2kGI=" + }, "object-assign": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-3.0.0.tgz", diff --git a/package.json b/package.json index 3ac968fd3..bee60f4f3 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "@prezzemolo/rap": "0.1.2", "@prezzemolo/zip": "0.0.3", "@types/bcryptjs": "2.4.1", + "@types/dateformat": "1.0.1", "@types/debug": "0.0.30", "@types/deep-equal": "1.0.1", "@types/elasticsearch": "5.0.25", @@ -92,6 +93,7 @@ "chalk": "2.4.1", "crc-32": "1.2.0", "css-loader": "1.0.0", + "dateformat": "3.0.3", "debug": "3.1.0", "deep-equal": "1.0.1", "deepcopy": "0.6.3", diff --git a/src/config/load.ts b/src/config/load.ts index fea89b989..d0f659a68 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -26,7 +26,7 @@ export default function load() { const mixin = {} as Mixin; // Validate URLs - if (!isUrl(config.url)) urlError(config.url); + if (!isUrl(config.url)) throw `url="${config.url}" is not a valid URL`; const url = new URL(config.url); config.url = normalizeUrl(config.url); @@ -50,8 +50,3 @@ export default function load() { function normalizeUrl(url: string) { return url[url.length - 1] === '/' ? url.substr(0, url.length - 1) : url; } - -function urlError(url: string) { - console.error(`「${url}」は、正しいURLではありません。先頭に http:// または https:// をつけ忘れてないかなど確認してください。`); - process.exit(99); -} diff --git a/src/index.ts b/src/index.ts index c919dd9be..5abd0f33e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -63,18 +63,15 @@ async function masterMain() { config = await init(); } catch (e) { console.error(e); - Logger.error(chalk.red('Fatal error occurred during initializing :(')); + Logger.error('Fatal error occurred during initialization'); process.exit(1); } - Logger.info(chalk.green('Successfully initialized :)')); + Logger.succ('Successfully initialized'); spawnWorkers(() => { - Logger.info(chalk.bold.green( - `Now listening on port ${chalk.underline(config.port.toString())}`)); - + Logger.info(chalk.bold.green(`Now listening on port ${chalk.underline(config.port.toString())}`)); Logger.info(chalk.bold.green(config.url)); - Logger.info(chalk.bold.green('Now processing jobs')); }); } @@ -98,7 +95,6 @@ async function workerMain() { */ async function init(): Promise { Logger.info('Welcome to Misskey!'); - Logger.info('Initializing...'); EnvironmentInfo.show(); MachineInfo.show(); @@ -110,14 +106,18 @@ async function init(): Promise { try { config = loadConfig(); } catch (exception) { - if (exception.code === 'ENOENT') { - throw 'Configuration not found - Please run "npm run config" command.'; + if (typeof exception === 'string') { + configLogger.error(exception); + process.exit(1); + } + if (exception.code === 'ENOENT') { + configLogger.error('Configuration file not found'); + process.exit(1); } - throw exception; } - configLogger.info('Successfully loaded'); + configLogger.succ('Successfully loaded'); configLogger.info(`Maintainer: ${config.maintainer.name}`); if (process.platform === 'linux' && !isRoot() && config.port < 1024) { @@ -133,7 +133,7 @@ async function init(): Promise { // Try to connect to MongoDB const mongoDBLogger = new Logger('MongoDB'); const db = require('./db/mongodb').default; - mongoDBLogger.info('Successfully connected'); + mongoDBLogger.succ('Successfully connected'); db.close(); return config; diff --git a/src/misc/dependencyInfo.ts b/src/misc/dependencyInfo.ts index 89af0d20f..829f8d980 100644 --- a/src/misc/dependencyInfo.ts +++ b/src/misc/dependencyInfo.ts @@ -21,7 +21,7 @@ export default class { const x = execSync(command, { stdio: ['pipe', 'pipe', 'ignore'] }); const ver = transform(x.toString()); if (ver != null) { - this.logger.info(`${serviceName} ${ver[1]} found`); + this.logger.succ(`${serviceName} ${ver[1]} found`); } else { this.logger.warn(`${serviceName} not found`); this.logger.warn(`Regexp used for version check of ${serviceName} is probably messed up`); diff --git a/src/misc/logger.ts b/src/misc/logger.ts index deb5bab17..51c8adf03 100644 --- a/src/misc/logger.ts +++ b/src/misc/logger.ts @@ -1,14 +1,5 @@ -import chalk, { Chalk } from 'chalk'; - -export type LogLevel = 'Error' | 'Warn' | 'Info'; - -function toLevelColor(level: LogLevel): Chalk { - switch (level) { - case 'Error': return chalk.red; - case 'Warn': return chalk.yellow; - case 'Info': return chalk.blue; - } -} +import chalk from 'chalk'; +import * as dateformat from 'dateformat'; export default class Logger { private domain: string; @@ -17,38 +8,45 @@ export default class Logger { this.domain = domain; } - public static log(level: LogLevel, message: string): void { - const color = toLevelColor(level); - const time = (new Date()).toLocaleTimeString('ja-JP'); - const coloredMessage = level === 'Info' ? message : color.bold(message); - console.log(`[${time} ${color.bold(level.toUpperCase())}]: ${coloredMessage}`); + public static log(level: string, message: string): void { + const time = dateformat(new Date(), 'HH:MM:ss'); + console.log(`[${time} ${level}] ${message}`); } public static error(message: string): void { - Logger.log('Error', message); + (new Logger('')).error(message); } public static warn(message: string): void { - Logger.log('Warn', message); + (new Logger('')).warn(message); } public static info(message: string): void { - Logger.log('Info', message); + (new Logger('')).info(message); } - public log(level: LogLevel, message: string): void { - Logger.log(level, `[${this.domain}] ${message}`); + public static succ(message: string): void { + (new Logger('')).succ(message); + } + + public log(level: string, message: string) { + const domain = this.domain.length > 0 ? `[${this.domain}] ` : ''; + Logger.log(level, `${domain}${message}`); } public error(message: string): void { - this.log('Error', message); + this.log(chalk.red.bold('ERROR'), chalk.red.bold(message)); } public warn(message: string): void { - this.log('Warn', message); + this.log(chalk.yellow.bold('WARN'), chalk.yellow.bold(message)); } public info(message: string): void { - this.log('Info', message); + this.log(chalk.blue.bold('INFO'), message); + } + + public succ(message: string): void { + this.log(chalk.blue.bold('INFO'), chalk.green.bold(message)); } }