0
0

Implement logger

log-cool is no longer with Misskey
This commit is contained in:
Aya Morisawa 2016-12-29 17:36:03 +09:00
parent b87e6e8044
commit cf446ac53c
4 changed files with 32 additions and 17 deletions

View File

@ -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",

View File

@ -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<State> {
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;
}

View File

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

16
src/utils/logger.ts Normal file
View File

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