mirror of
https://example.com
synced 2024-11-23 05:46:39 +09:00
Implement logger
log-cool is no longer with Misskey
This commit is contained in:
parent
b87e6e8044
commit
cf446ac53c
4 changed files with 32 additions and 17 deletions
|
@ -94,7 +94,6 @@
|
||||||
"inquirer": "2.0.0",
|
"inquirer": "2.0.0",
|
||||||
"js-yaml": "3.7.0",
|
"js-yaml": "3.7.0",
|
||||||
"livescript": "1.5.0",
|
"livescript": "1.5.0",
|
||||||
"log-cool": "1.1.0",
|
|
||||||
"mime-types": "2.1.13",
|
"mime-types": "2.1.13",
|
||||||
"mongodb": "2.2.16",
|
"mongodb": "2.2.16",
|
||||||
"ms": "0.7.2",
|
"ms": "0.7.2",
|
||||||
|
|
24
src/index.ts
24
src/index.ts
|
@ -11,7 +11,7 @@ import * as fs from 'fs';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as cluster from 'cluster';
|
import * as cluster from 'cluster';
|
||||||
const prominence = require('prominence');
|
const prominence = require('prominence');
|
||||||
import { logInfo, logDone, logWarn, logFailed } from 'log-cool';
|
import { log } from './utils/logger';
|
||||||
import * as chalk from 'chalk';
|
import * as chalk from 'chalk';
|
||||||
const git = require('git-last-commit');
|
const git = require('git-last-commit');
|
||||||
const portUsed = require('tcp-port-used');
|
const portUsed = require('tcp-port-used');
|
||||||
|
@ -151,41 +151,41 @@ async function init(): Promise<State> {
|
||||||
console.log('\nInitializing...\n');
|
console.log('\nInitializing...\n');
|
||||||
|
|
||||||
if (IS_DEBUG) {
|
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
|
// Get machine info
|
||||||
const totalmem = (os.totalmem() / 1024 / 1024 / 1024).toFixed(1);
|
const totalmem = (os.totalmem() / 1024 / 1024 / 1024).toFixed(1);
|
||||||
const freemem = (os.freemem() / 1024 / 1024 / 1024).toFixed(1);
|
const freemem = (os.freemem() / 1024 / 1024 / 1024).toFixed(1);
|
||||||
logInfo(`MACHINE: ${os.hostname()}`);
|
log('Info', `MACHINE: ${os.hostname()}`);
|
||||||
logInfo(`MACHINE: CPU: ${os.cpus().length}core`);
|
log('Info', `MACHINE: CPU: ${os.cpus().length}core`);
|
||||||
logInfo(`MACHINE: MEM: ${totalmem}GB (available: ${freemem}GB)`);
|
log('Info', `MACHINE: MEM: ${totalmem}GB (available: ${freemem}GB)`);
|
||||||
|
|
||||||
if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) {
|
if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) {
|
||||||
logFailed('Configuration not found');
|
log('Error', 'Configuration not found');
|
||||||
return State.failed;
|
return State.failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
logDone('Success to load configuration');
|
log('Info', 'Success to load configuration');
|
||||||
logInfo(`maintainer: ${config.maintainer}`);
|
log('Info', `maintainer: ${config.maintainer}`);
|
||||||
|
|
||||||
checkDependencies();
|
checkDependencies();
|
||||||
|
|
||||||
// Check if a port is being used
|
// Check if a port is being used
|
||||||
if (await portUsed.check(config.port)) {
|
if (await portUsed.check(config.port)) {
|
||||||
logFailed(`Port: ${config.port} is already used!`);
|
log('Error', `Port: ${config.port} is already used!`);
|
||||||
return State.failed;
|
return State.failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to connect to MongoDB
|
// Try to connect to MongoDB
|
||||||
try {
|
try {
|
||||||
const db = await initdb(config);
|
const db = await initdb(config);
|
||||||
logDone('Success to connect to MongoDB');
|
log('Info', 'Success to connect to MongoDB');
|
||||||
db.close();
|
db.close();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logFailed(`MongoDB: ${e}`);
|
log('Error', `MongoDB: ${e}`);
|
||||||
return State.failed;
|
return State.failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {logInfo, logDone, logWarn} from 'log-cool';
|
import { log } from './logger';
|
||||||
import {exec} from 'shelljs';
|
import {exec} from 'shelljs';
|
||||||
|
|
||||||
export default function(): void {
|
export default function(): void {
|
||||||
|
@ -6,7 +6,7 @@ export default function(): void {
|
||||||
checkDependency('npm', 'npm -v', x => x.match(/^(.*)\r?\n$/)[1]);
|
checkDependency('npm', 'npm -v', x => x.match(/^(.*)\r?\n$/)[1]);
|
||||||
checkDependency('MongoDB', 'mongo --version', x => x.match(/^MongoDB shell version: (.*)\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]);
|
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 {
|
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;
|
const x = exec(command, { silent: true }) as any;
|
||||||
if (x.code === code.success) {
|
if (x.code === code.success) {
|
||||||
logInfo(`DEPS: ${serviceName} ${transform(x.stdout)}`);
|
log('Info', `DEPS: ${serviceName} ${transform(x.stdout)}`);
|
||||||
} else if (x.code === code.notFound) {
|
} else if (x.code === code.notFound) {
|
||||||
logWarn(`Unable to find ${serviceName}`);
|
log('Warn', `Unable to find ${serviceName}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
16
src/utils/logger.ts
Normal file
16
src/utils/logger.ts
Normal 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}`);
|
||||||
|
}
|
Loading…
Reference in a new issue