From 23f728551c65e0115096ae28711e93fec6ecf47b Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Thu, 29 Dec 2016 17:36:03 +0900 Subject: [PATCH] Implement logger log-cool is no longer with Misskey --- package.json | 1 - src/index.ts | 24 ++++++++++++------------ src/utils/check-dependencies.ts | 8 ++++---- src/utils/logger.ts | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 src/utils/logger.ts diff --git a/package.json b/package.json index 1728ce917..038e0100c 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,6 @@ "inquirer": "2.0.0", "js-yaml": "3.7.0", "livescript": "1.5.0", - "log-cool": "1.1.0", "mime-types": "2.1.13", "mongodb": "2.2.16", "ms": "0.7.2", diff --git a/src/index.ts b/src/index.ts index b80b2da57..f22364a99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ import * as fs from 'fs'; import * as os from 'os'; import * as cluster from 'cluster'; const prominence = require('prominence'); -import { logInfo, logDone, logWarn, logFailed } from 'log-cool'; +import { log } from './utils/logger'; import * as chalk from 'chalk'; const git = require('git-last-commit'); const portUsed = require('tcp-port-used'); @@ -151,41 +151,41 @@ async function init(): Promise { console.log('\nInitializing...\n'); if (IS_DEBUG) { - logWarn('It is not in the Production mode. Do not use in the Production environment.'); + log('Warn', 'It is not in the Production mode. Do not use in the Production environment.'); } - logInfo(`environment: ${env}`); + log('Info', `environment: ${env}`); // Get machine info const totalmem = (os.totalmem() / 1024 / 1024 / 1024).toFixed(1); const freemem = (os.freemem() / 1024 / 1024 / 1024).toFixed(1); - logInfo(`MACHINE: ${os.hostname()}`); - logInfo(`MACHINE: CPU: ${os.cpus().length}core`); - logInfo(`MACHINE: MEM: ${totalmem}GB (available: ${freemem}GB)`); + log('Info', `MACHINE: ${os.hostname()}`); + log('Info', `MACHINE: CPU: ${os.cpus().length}core`); + log('Info', `MACHINE: MEM: ${totalmem}GB (available: ${freemem}GB)`); if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) { - logFailed('Configuration not found'); + log('Error', 'Configuration not found'); return State.failed; } - logDone('Success to load configuration'); - logInfo(`maintainer: ${config.maintainer}`); + log('Info', 'Success to load configuration'); + log('Info', `maintainer: ${config.maintainer}`); checkDependencies(); // Check if a port is being used if (await portUsed.check(config.port)) { - logFailed(`Port: ${config.port} is already used!`); + log('Error', `Port: ${config.port} is already used!`); return State.failed; } // Try to connect to MongoDB try { const db = await initdb(config); - logDone('Success to connect to MongoDB'); + log('Info', 'Success to connect to MongoDB'); db.close(); } catch (e) { - logFailed(`MongoDB: ${e}`); + log('Error', `MongoDB: ${e}`); return State.failed; } diff --git a/src/utils/check-dependencies.ts b/src/utils/check-dependencies.ts index 7bcb87a68..364c24581 100644 --- a/src/utils/check-dependencies.ts +++ b/src/utils/check-dependencies.ts @@ -1,4 +1,4 @@ -import {logInfo, logDone, logWarn} from 'log-cool'; +import { log } from './logger'; import {exec} from 'shelljs'; export default function(): void { @@ -6,7 +6,7 @@ export default function(): void { checkDependency('npm', 'npm -v', x => x.match(/^(.*)\r?\n$/)[1]); checkDependency('MongoDB', 'mongo --version', x => x.match(/^MongoDB shell version: (.*)\r?\n$/)[1]); checkDependency('Redis', 'redis-server --version', x => x.match(/v=([0-9\.]*)/)[1]); - logDone('Successfully checked external dependencies'); + log('Info', 'Successfully checked external dependencies'); } function checkDependency(serviceName: string, command: string, transform: (x: string) => string): void { @@ -16,8 +16,8 @@ function checkDependency(serviceName: string, command: string, transform: (x: st }; const x = exec(command, { silent: true }) as any; if (x.code === code.success) { - logInfo(`DEPS: ${serviceName} ${transform(x.stdout)}`); + log('Info', `DEPS: ${serviceName} ${transform(x.stdout)}`); } else if (x.code === code.notFound) { - logWarn(`Unable to find ${serviceName}`); + log('Warn', `Unable to find ${serviceName}`); } } diff --git a/src/utils/logger.ts b/src/utils/logger.ts new file mode 100644 index 000000000..01c3fdc76 --- /dev/null +++ b/src/utils/logger.ts @@ -0,0 +1,16 @@ +import * as chalk from 'chalk'; + +export type LogLevel = 'Error' | 'Warn' | 'Info'; + +function toLevelColor(level: LogLevel): chalk.ChalkStyle { + switch (level) { + case 'Error': return chalk.red; + case 'Warn': return chalk.yellow; + case 'Info': return chalk.blue; + } +} + +export function log(level: LogLevel, message: string): void { + let color = toLevelColor(level); + console.log(`[${color.bold(level.toUpperCase())}] ${message}`); +}