Update logger
This commit is contained in:
parent
dcc32d5581
commit
5b0a227382
51
src/index.ts
51
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 { log } from './utils/logger';
|
import Logger 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');
|
||||||
@ -72,14 +72,14 @@ async function master(): Promise<void> {
|
|||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case State.failed:
|
case State.failed:
|
||||||
log('Error', chalk.red('Fatal error occurred :('));
|
Logger.error(chalk.red('Fatal error occurred :('));
|
||||||
process.exit();
|
process.exit();
|
||||||
return;
|
return;
|
||||||
case State.warn:
|
case State.warn:
|
||||||
log('Warn', chalk.yellow('Some problem(s) :|'));
|
Logger.warn(chalk.yellow('Some problem(s) :|'));
|
||||||
break;
|
break;
|
||||||
case State.success:
|
case State.success:
|
||||||
log('Info', chalk.green('OK :)'));
|
Logger.info(chalk.green('OK :)'));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,59 +132,64 @@ function worker(): void {
|
|||||||
async function init(): Promise<State> {
|
async function init(): Promise<State> {
|
||||||
let warn = false;
|
let warn = false;
|
||||||
|
|
||||||
log('Info', 'Welcome to Misskey!');
|
Logger.info('Welcome to Misskey!');
|
||||||
log('Info', chalk.bold('Misskey Core <aoi>'));
|
Logger.info(chalk.bold('Misskey Core <aoi>'));
|
||||||
log('Info', 'Initializing...');
|
Logger.info('Initializing...');
|
||||||
|
|
||||||
// Get commit info
|
// Get commit info
|
||||||
|
let lastCommitLogger = new Logger('LastCommit');
|
||||||
try {
|
try {
|
||||||
const commit = await prominence(git).getLastCommit();
|
const commit = await prominence(git).getLastCommit();
|
||||||
const shortHash: string = commit.shortHash;
|
const shortHash: string = commit.shortHash;
|
||||||
const hash: string = commit.hash;
|
const hash: string = commit.hash;
|
||||||
const commitDate = new Date(parseInt(commit.committedOn, 10) * 1000).toLocaleDateString('ja-JP');
|
const commitDate = new Date(parseInt(commit.committedOn, 10) * 1000).toLocaleDateString('ja-JP');
|
||||||
const commitTime = new Date(parseInt(commit.committedOn, 10) * 1000).toLocaleTimeString('ja-JP');
|
const commitTime = new Date(parseInt(commit.committedOn, 10) * 1000).toLocaleTimeString('ja-JP');
|
||||||
log('Info', `${shortHash}${chalk.gray(hash.substr(shortHash.length))}`, 'LastCommit');
|
lastCommitLogger.info(`${shortHash}${chalk.gray(hash.substr(shortHash.length))}`);
|
||||||
log('Info', `${commit.subject} ${chalk.green(`(${commitDate} ${commitTime})`)} ${chalk.blue(`<${commit.author.name}>`)}`, 'LastCommit');
|
lastCommitLogger.info(`${commit.subject} ${chalk.green(`(${commitDate} ${commitTime})`)} ${chalk.blue(`<${commit.author.name}>`)}`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log('Info', `No commit information found`, 'LastCommit');
|
lastCommitLogger.info('No commit information found')
|
||||||
}
|
}
|
||||||
|
|
||||||
log('Info', typeof env == 'undefined' ? 'NODE_ENV is not set' : `NODE_ENV: ${env}`, 'Env');
|
let envLogger = new Logger('Env');
|
||||||
|
envLogger.info(typeof env == 'undefined' ? 'NODE_ENV is not set' : `NODE_ENV: ${env}`);
|
||||||
if (IS_DEBUG) {
|
if (IS_DEBUG) {
|
||||||
log('Warn', 'The environment is not in production mode', 'Env');
|
envLogger.warn('The environment is not in production mode');
|
||||||
log('Warn', 'Do not use for production purpose', 'Env');
|
envLogger.warn('Do not use for production purpose');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
log('Info', `${os.hostname()}`, 'Machine');
|
let machineLogger = new Logger('Machine');
|
||||||
log('Info', `CPU: ${os.cpus().length}core`, 'Machine');
|
machineLogger.info(os.hostname());
|
||||||
log('Info', `MEM: ${totalmem}GB (available: ${freemem}GB)`, 'Machine');
|
machineLogger.info(`CPU: ${os.cpus().length}core`);
|
||||||
|
machineLogger.info(`MEM: ${totalmem}GB (available: ${freemem}GB)`);
|
||||||
|
|
||||||
if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) {
|
if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) {
|
||||||
log('Error', 'Configuration not found');
|
Logger.error('Configuration not found');
|
||||||
return State.failed;
|
return State.failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
log('Info', 'Successfully loaded', 'Config');
|
let configLogger = new Logger('Config');
|
||||||
log('Info', `maintainer: ${config.maintainer}`, 'Config');
|
configLogger.info('Successfully loaded');
|
||||||
|
configLogger.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)) {
|
||||||
log('Error', `Port: ${config.port} is already used!`);
|
Logger.error(`Port: ${config.port} is already used!`);
|
||||||
return State.failed;
|
return State.failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to connect to MongoDB
|
// Try to connect to MongoDB
|
||||||
|
let mongoDBLogger = new Logger('MongoDB');
|
||||||
try {
|
try {
|
||||||
const db = await initdb(config);
|
const db = await initdb(config);
|
||||||
log('Info', 'Successfully connected', 'MongoDB');
|
mongoDBLogger.info('Successfully connected');
|
||||||
db.close();
|
db.close();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log('Error', `${e}`, 'MongoDB');
|
mongoDBLogger.error(`${e}`);
|
||||||
return State.failed;
|
return State.failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,5 +223,5 @@ function spawn(callback: any): void {
|
|||||||
|
|
||||||
// Dying away...
|
// Dying away...
|
||||||
process.on('exit', () => {
|
process.on('exit', () => {
|
||||||
log('Info', 'Misskey is going down');
|
Logger.info('Misskey is going down');
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { log } from './logger';
|
import Logger from './logger';
|
||||||
import { exec } from 'shelljs';
|
import { exec } from 'shelljs';
|
||||||
|
|
||||||
export default function(): void {
|
export default function(): void {
|
||||||
@ -14,9 +14,10 @@ function checkDependency(serviceName: string, command: string, transform: (x: st
|
|||||||
notFound: 127
|
notFound: 127
|
||||||
};
|
};
|
||||||
const x = exec(command, { silent: true }) as any;
|
const x = exec(command, { silent: true }) as any;
|
||||||
|
let depsLogger = new Logger('Deps');
|
||||||
if (x.code === code.success) {
|
if (x.code === code.success) {
|
||||||
log('Info', `${serviceName} ${transform(x.stdout)} found`, 'Deps');
|
depsLogger.info(`${serviceName} ${transform(x.stdout)} found`);
|
||||||
} else if (x.code === code.notFound) {
|
} else if (x.code === code.notFound) {
|
||||||
log('Warn', `${serviceName} not found`, 'Deps');
|
depsLogger.warn(`${serviceName} not found`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,17 +10,44 @@ function toLevelColor(level: LogLevel): chalk.ChalkStyle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function log(message: string): void;
|
export default class Logger {
|
||||||
export function log(level: LogLevel, message: string): void;
|
domain: string;
|
||||||
export function log(level: LogLevel, message: string, domain: string): void;
|
|
||||||
export function log(x: string | LogLevel, message?: string, domain?: string): void {
|
static log(level: LogLevel, message: string): void {
|
||||||
const level = typeof message == 'undefined' ? 'Info' : x as LogLevel;
|
|
||||||
message = typeof message == 'undefined' ? x : message;
|
|
||||||
if (typeof domain == 'string') {
|
|
||||||
log(level, `[${domain}] ${message}`);
|
|
||||||
} else {
|
|
||||||
let color = toLevelColor(level);
|
let color = toLevelColor(level);
|
||||||
let time = (new Date()).toLocaleTimeString('ja-JP');
|
let time = (new Date()).toLocaleTimeString('ja-JP');
|
||||||
console.log(`[${time} ${color.bold(level.toUpperCase())}]: ${message}`);
|
console.log(`[${time} ${color.bold(level.toUpperCase())}]: ${message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static error(message: string): void {
|
||||||
|
Logger.log('Error', message);
|
||||||
|
}
|
||||||
|
|
||||||
|
static warn(message: string): void {
|
||||||
|
Logger.log('Warn', message);
|
||||||
|
}
|
||||||
|
|
||||||
|
static info(message: string): void {
|
||||||
|
Logger.log('Info', message);
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(domain: string) {
|
||||||
|
this.domain = domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
log(level: LogLevel, message: string): void {
|
||||||
|
Logger.log(level, `[${this.domain}] ${message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
error(message: string): void {
|
||||||
|
this.log('Error', message);
|
||||||
|
}
|
||||||
|
|
||||||
|
warn(message: string): void {
|
||||||
|
this.log('Warn', message);
|
||||||
|
}
|
||||||
|
|
||||||
|
info(message: string): void {
|
||||||
|
this.log('Info', message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user