add new Console global DI to abstract the node.js console
This commit is contained in:
parent
8059515db4
commit
9b99e8eba8
4 changed files with 24 additions and 19 deletions
|
|
@ -9,7 +9,7 @@ import { default as convertColor } from 'color-convert';
|
|||
import { format as dateFormat } from 'date-fns';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import { TimeService, NativeTimeService } from '@/global/TimeService.js';
|
||||
import { envOption } from './env.js';
|
||||
import { EnvService } from '@/global/EnvService.js';
|
||||
import type { KEYWORD } from 'color-convert/conversions.js';
|
||||
|
||||
type Context = {
|
||||
|
|
@ -28,6 +28,7 @@ export type Console = Pick<typeof global.console, 'error' | 'warn' | 'info' | 'l
|
|||
export const nativeConsole: Console = global.console;
|
||||
|
||||
const fallbackTimeService = new NativeTimeService();
|
||||
const fallbackEnvService = new EnvService();
|
||||
|
||||
const levelFuncs = {
|
||||
error: 'error',
|
||||
|
|
@ -41,8 +42,8 @@ const levelFuncs = {
|
|||
export default class Logger {
|
||||
private context: Context;
|
||||
private parentLogger: Logger | null = null;
|
||||
public readonly verbose: boolean;
|
||||
private readonly timeService: TimeService;
|
||||
private readonly envService: EnvService;
|
||||
|
||||
/**
|
||||
* Where to send the actual log strings.
|
||||
|
|
@ -50,26 +51,26 @@ export default class Logger {
|
|||
*/
|
||||
private readonly console: Console;
|
||||
|
||||
constructor(context: string, color?: KEYWORD, verbose?: boolean, console?: Console, timeService?: TimeService) {
|
||||
constructor(context: string, color?: KEYWORD, envService?: EnvService, timeService?: TimeService, console?: Console) {
|
||||
this.context = {
|
||||
name: context,
|
||||
color: color,
|
||||
};
|
||||
this.verbose = verbose ?? envOption.verbose;
|
||||
this.envService = envService ?? fallbackEnvService;
|
||||
this.console = console ?? nativeConsole;
|
||||
this.timeService = timeService ?? fallbackTimeService;
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public createSubLogger(context: string, color?: KEYWORD): Logger {
|
||||
const logger = new Logger(context, color, this.verbose, this.console, this.timeService);
|
||||
const logger = new Logger(context, color, this.envService, this.timeService, this.console);
|
||||
logger.parentLogger = this;
|
||||
return logger;
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private log(level: Level, message: string, data?: Data, important = false, subContexts: Context[] = []): void {
|
||||
if (envOption.quiet) return;
|
||||
if (this.envService.options.quiet) return;
|
||||
|
||||
if (this.parentLogger) {
|
||||
this.parentLogger.log(level, message, data, important, [this.context].concat(subContexts));
|
||||
|
|
@ -94,10 +95,10 @@ export default class Logger {
|
|||
level === 'info' ? message :
|
||||
null;
|
||||
|
||||
let log = envOption.hideWorkerId
|
||||
let log = this.envService.options.hideWorkerId
|
||||
? `${l}\t[${contexts.join(' ')}]\t\t${m}`
|
||||
: `${l} ${worker}\t[${contexts.join(' ')}]\t\t${m}`;
|
||||
if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log;
|
||||
if (this.envService.options.withLogTime) log = chalk.gray(time) + ' ' + log;
|
||||
|
||||
const args: unknown[] = [important ? chalk.bold(log) : log];
|
||||
if (Array.isArray(data)) {
|
||||
|
|
@ -137,7 +138,7 @@ export default class Logger {
|
|||
|
||||
@bindThis
|
||||
public debug(message: string, data?: Data, important = false): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報)
|
||||
if (process.env.NODE_ENV !== 'production' || this.verbose) {
|
||||
if (process.env.NODE_ENV !== 'production' || this.envService.options.verbose) {
|
||||
this.log('debug', message, data, important);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue