mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-09 19:43:09 +02:00
Update logger
This commit is contained in:
parent
dcc32d5581
commit
5b0a227382
3 changed files with 68 additions and 35 deletions
51
src/index.ts
51
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 { log } from './utils/logger';
|
||||
import Logger from './utils/logger';
|
||||
import * as chalk from 'chalk';
|
||||
const git = require('git-last-commit');
|
||||
const portUsed = require('tcp-port-used');
|
||||
|
@ -72,14 +72,14 @@ async function master(): Promise<void> {
|
|||
|
||||
switch (state) {
|
||||
case State.failed:
|
||||
log('Error', chalk.red('Fatal error occurred :('));
|
||||
Logger.error(chalk.red('Fatal error occurred :('));
|
||||
process.exit();
|
||||
return;
|
||||
case State.warn:
|
||||
log('Warn', chalk.yellow('Some problem(s) :|'));
|
||||
Logger.warn(chalk.yellow('Some problem(s) :|'));
|
||||
break;
|
||||
case State.success:
|
||||
log('Info', chalk.green('OK :)'));
|
||||
Logger.info(chalk.green('OK :)'));
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -132,59 +132,64 @@ function worker(): void {
|
|||
async function init(): Promise<State> {
|
||||
let warn = false;
|
||||
|
||||
log('Info', 'Welcome to Misskey!');
|
||||
log('Info', chalk.bold('Misskey Core <aoi>'));
|
||||
log('Info', 'Initializing...');
|
||||
Logger.info('Welcome to Misskey!');
|
||||
Logger.info(chalk.bold('Misskey Core <aoi>'));
|
||||
Logger.info('Initializing...');
|
||||
|
||||
// Get commit info
|
||||
let lastCommitLogger = new Logger('LastCommit');
|
||||
try {
|
||||
const commit = await prominence(git).getLastCommit();
|
||||
const shortHash: string = commit.shortHash;
|
||||
const hash: string = commit.hash;
|
||||
const commitDate = new Date(parseInt(commit.committedOn, 10) * 1000).toLocaleDateString('ja-JP');
|
||||
const commitTime = new Date(parseInt(commit.committedOn, 10) * 1000).toLocaleTimeString('ja-JP');
|
||||
log('Info', `${shortHash}${chalk.gray(hash.substr(shortHash.length))}`, 'LastCommit');
|
||||
log('Info', `${commit.subject} ${chalk.green(`(${commitDate} ${commitTime})`)} ${chalk.blue(`<${commit.author.name}>`)}`, 'LastCommit');
|
||||
lastCommitLogger.info(`${shortHash}${chalk.gray(hash.substr(shortHash.length))}`);
|
||||
lastCommitLogger.info(`${commit.subject} ${chalk.green(`(${commitDate} ${commitTime})`)} ${chalk.blue(`<${commit.author.name}>`)}`);
|
||||
} 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) {
|
||||
log('Warn', 'The environment is not in production mode', 'Env');
|
||||
log('Warn', 'Do not use for production purpose', 'Env');
|
||||
envLogger.warn('The environment is not in production mode');
|
||||
envLogger.warn('Do not use for production purpose');
|
||||
}
|
||||
|
||||
// Get machine info
|
||||
const totalmem = (os.totalmem() / 1024 / 1024 / 1024).toFixed(1);
|
||||
const freemem = (os.freemem() / 1024 / 1024 / 1024).toFixed(1);
|
||||
log('Info', `${os.hostname()}`, 'Machine');
|
||||
log('Info', `CPU: ${os.cpus().length}core`, 'Machine');
|
||||
log('Info', `MEM: ${totalmem}GB (available: ${freemem}GB)`, 'Machine');
|
||||
let machineLogger = new Logger('Machine');
|
||||
machineLogger.info(os.hostname());
|
||||
machineLogger.info(`CPU: ${os.cpus().length}core`);
|
||||
machineLogger.info(`MEM: ${totalmem}GB (available: ${freemem}GB)`);
|
||||
|
||||
if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) {
|
||||
log('Error', 'Configuration not found');
|
||||
Logger.error('Configuration not found');
|
||||
return State.failed;
|
||||
}
|
||||
|
||||
log('Info', 'Successfully loaded', 'Config');
|
||||
log('Info', `maintainer: ${config.maintainer}`, 'Config');
|
||||
let configLogger = new Logger('Config');
|
||||
configLogger.info('Successfully loaded');
|
||||
configLogger.info(`maintainer: ${config.maintainer}`);
|
||||
|
||||
checkDependencies();
|
||||
|
||||
// Check if a port is being used
|
||||
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;
|
||||
}
|
||||
|
||||
// Try to connect to MongoDB
|
||||
let mongoDBLogger = new Logger('MongoDB');
|
||||
try {
|
||||
const db = await initdb(config);
|
||||
log('Info', 'Successfully connected', 'MongoDB');
|
||||
mongoDBLogger.info('Successfully connected');
|
||||
db.close();
|
||||
} catch (e) {
|
||||
log('Error', `${e}`, 'MongoDB');
|
||||
mongoDBLogger.error(`${e}`);
|
||||
return State.failed;
|
||||
}
|
||||
|
||||
|
@ -218,5 +223,5 @@ function spawn(callback: any): void {
|
|||
|
||||
// Dying away...
|
||||
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';
|
||||
|
||||
export default function(): void {
|
||||
|
@ -14,9 +14,10 @@ function checkDependency(serviceName: string, command: string, transform: (x: st
|
|||
notFound: 127
|
||||
};
|
||||
const x = exec(command, { silent: true }) as any;
|
||||
let depsLogger = new Logger('Deps');
|
||||
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) {
|
||||
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 function log(level: LogLevel, message: string): void;
|
||||
export function log(level: LogLevel, message: string, domain: string): void;
|
||||
export function log(x: string | LogLevel, message?: string, domain?: 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 {
|
||||
export default class Logger {
|
||||
domain: string;
|
||||
|
||||
static log(level: LogLevel, message: string): void {
|
||||
let color = toLevelColor(level);
|
||||
let time = (new Date()).toLocaleTimeString('ja-JP');
|
||||
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 a new issue