mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-04 07:24:13 +00:00 
			
		
		
		
	Better logger
This commit is contained in:
		
							parent
							
								
									a91f95451a
								
							
						
					
					
						commit
						80aa45372a
					
				
					 2 changed files with 14 additions and 28 deletions
				
			
		
							
								
								
									
										11
									
								
								src/index.ts
									
										
									
									
									
								
							
							
						
						
									
										11
									
								
								src/index.ts
									
										
									
									
									
								
							| 
						 | 
					@ -25,8 +25,9 @@ import { Config } from './config/types';
 | 
				
			||||||
import { lessThan } from './prelude/array';
 | 
					import { lessThan } from './prelude/array';
 | 
				
			||||||
import * as pkg from '../package.json';
 | 
					import * as pkg from '../package.json';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const bootLogger = new Logger('boot');
 | 
					const logger = new Logger('core');
 | 
				
			||||||
const clusterLog = new Logger('cluster');
 | 
					const bootLogger = new Logger('boot', logger);
 | 
				
			||||||
 | 
					const clusterLog = new Logger('cluster', logger);
 | 
				
			||||||
const ev = new Xev();
 | 
					const ev = new Xev();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (process.env.NODE_ENV != 'production' && process.env.DEBUG == null) {
 | 
					if (process.env.NODE_ENV != 'production' && process.env.DEBUG == null) {
 | 
				
			||||||
| 
						 | 
					@ -86,7 +87,7 @@ async function masterMain() {
 | 
				
			||||||
		await spawnWorkers(config.clusterLimit);
 | 
							await spawnWorkers(config.clusterLimit);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`);
 | 
						bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`, true);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
| 
						 | 
					@ -264,12 +265,12 @@ process.on('unhandledRejection', console.dir);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Display detail of uncaught exception
 | 
					// Display detail of uncaught exception
 | 
				
			||||||
process.on('uncaughtException', err => {
 | 
					process.on('uncaughtException', err => {
 | 
				
			||||||
	console.error(err);
 | 
						logger.error(err);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Dying away...
 | 
					// Dying away...
 | 
				
			||||||
process.on('exit', code => {
 | 
					process.on('exit', code => {
 | 
				
			||||||
	Logger.info(`The process is going to exit with code ${code}`);
 | 
						logger.info(`The process is going to exit with code ${code}`);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//#endregion
 | 
					//#endregion
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,41 +10,26 @@ export default class Logger {
 | 
				
			||||||
		this.parentLogger = parentLogger;
 | 
							this.parentLogger = parentLogger;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public log(level: string, message: string): void {
 | 
						public log(level: string, message: string, important = false): void {
 | 
				
			||||||
		if (this.parentLogger) {
 | 
							if (this.parentLogger) {
 | 
				
			||||||
			this.parentLogger.log(level, `[${this.domain}] ${message}`);
 | 
								this.parentLogger.log(level, `[${this.domain}]\t${message}`, important);
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			const time = dateformat(new Date(), 'HH:MM:ss');
 | 
								const time = dateformat(new Date(), 'HH:MM:ss');
 | 
				
			||||||
			console.log(`${chalk.gray(time)} ${level} [${this.domain}] ${message}`);
 | 
								const log = `${chalk.gray(time)} ${level} [${this.domain}]\t${message}`;
 | 
				
			||||||
 | 
								console.log(important ? chalk.bold(log) : log);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public static error(message: string): void {
 | 
						public error(message: string | Error): void { // 実行を継続できない状況で使う
 | 
				
			||||||
		(new Logger('')).error(message);
 | 
							this.log(chalk.red.bold('ERROR'), chalk.red.bold(message.toString()));
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public static warn(message: string): void {
 | 
					 | 
				
			||||||
		(new Logger('')).warn(message);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public static succ(message: string): void {
 | 
					 | 
				
			||||||
		(new Logger('')).succ(message);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public static info(message: string): void {
 | 
					 | 
				
			||||||
		(new Logger('')).info(message);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public error(message: string): void { // 実行を継続できない状況で使う
 | 
					 | 
				
			||||||
		this.log(chalk.red.bold('ERROR'), chalk.red.bold(message));
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public warn(message: string): void { // 実行を継続できるが改善すべき状況で使う
 | 
						public warn(message: string): void { // 実行を継続できるが改善すべき状況で使う
 | 
				
			||||||
		this.log(chalk.yellow.bold('WARN'), chalk.yellow.bold(message));
 | 
							this.log(chalk.yellow.bold('WARN'), chalk.yellow.bold(message));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public succ(message: string): void { // 何かに成功した状況で使う
 | 
						public succ(message: string, important = false): void { // 何かに成功した状況で使う
 | 
				
			||||||
		this.log(chalk.blue.green('DONE'), chalk.green.bold(message));
 | 
							this.log(chalk.blue.green('DONE'), chalk.green.bold(message), important);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public info(message: string): void { // それ以外
 | 
						public info(message: string): void { // それ以外
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue