From d50e99c17bddc04ca7a09b85a70603ff5404739a Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Sat, 14 Jul 2018 20:58:21 +0900 Subject: Update log messages --- src/config/load.ts | 7 +------ src/index.ts | 22 +++++++++++----------- src/misc/dependencyInfo.ts | 2 +- src/misc/logger.ts | 46 ++++++++++++++++++++++------------------------ 4 files changed, 35 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/config/load.ts b/src/config/load.ts index fea89b989a..d0f659a681 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 c919dd9bee..5abd0f33e7 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 (typeof exception === 'string') { + configLogger.error(exception); + process.exit(1); + } if (exception.code === 'ENOENT') { - throw 'Configuration not found - Please run "npm run config" command.'; + 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 89af0d20fb..829f8d9809 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 deb5bab17a..51c8adf037 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)); } } -- cgit v1.2.3-freya