Update log messages

This commit is contained in:
Aya Morisawa 2018-07-14 20:58:21 +09:00
parent c99e864dbc
commit d50e99c17b
6 changed files with 52 additions and 52 deletions

View file

@ -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);
}

View file

@ -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<Config> {
Logger.info('Welcome to Misskey!');
Logger.info('Initializing...');
EnvironmentInfo.show();
MachineInfo.show();
@ -110,14 +106,18 @@ async function init(): Promise<Config> {
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<Config> {
// 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;

View file

@ -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`);

View file

@ -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));
}
}