Fix type errors in logger service

This commit is contained in:
Michcio 2022-09-25 16:36:36 +02:00
parent 957a69779a
commit 79c71bf22a

View file

@ -6,9 +6,10 @@ import * as SyslogPro from 'syslog-pro';
import config from '@/config/index.js'; import config from '@/config/index.js';
import { envOption } from '@/env.js'; import { envOption } from '@/env.js';
type KeywordColor = Parameters<typeof convertColor.keyword.rgb>[0];
type Domain = { type Domain = {
name: string; name: string;
color?: string; color?: KeywordColor;
}; };
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info'; type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
@ -17,9 +18,9 @@ export default class Logger {
private domain: Domain; private domain: Domain;
private parentLogger: Logger | null = null; private parentLogger: Logger | null = null;
private store: boolean; private store: boolean;
private syslogClient: any | null = null; private syslogClient: SyslogPro.RFC5424 | null = null;
constructor(domain: string, color?: string, store = true) { constructor(domain: string, color?: KeywordColor, store = true) {
this.domain = { this.domain = {
name: domain, name: domain,
color, color,
@ -41,7 +42,7 @@ export default class Logger {
} }
} }
public createSubLogger(domain: string, color?: string, store = true): Logger { public createSubLogger(domain: string, color?: KeywordColor, store = true): Logger {
const logger = new Logger(domain, color, store); const logger = new Logger(domain, color, store);
logger.parentLogger = this; logger.parentLogger = this;
return logger; return logger;
@ -57,7 +58,7 @@ export default class Logger {
} }
const time = dateFormat(new Date(), 'HH:mm:ss'); const time = dateFormat(new Date(), 'HH:mm:ss');
const worker = cluster.isPrimary ? '*' : cluster.worker.id; const worker = cluster.isPrimary ? '*' : cluster.worker!.id;
const l = const l =
level === 'error' ? important ? chalk.bgRed.white('ERR ') : chalk.red('ERR ') : level === 'error' ? important ? chalk.bgRed.white('ERR ') : chalk.red('ERR ') :
level === 'warning' ? chalk.yellow('WARN') : level === 'warning' ? chalk.yellow('WARN') :
@ -85,7 +86,6 @@ export default class Logger {
level === 'error' ? this.syslogClient.error : level === 'error' ? this.syslogClient.error :
level === 'warning' ? this.syslogClient.warning : level === 'warning' ? this.syslogClient.warning :
level === 'success' ? this.syslogClient.info : level === 'success' ? this.syslogClient.info :
level === 'debug' ? this.syslogClient.info :
level === 'info' ? this.syslogClient.info : level === 'info' ? this.syslogClient.info :
null as never; null as never;
@ -94,7 +94,7 @@ export default class Logger {
} }
} }
public error(x: string | Error, data?: Record<string, any> = {}, important = false): void { // 実行を継続できない状況で使う public error(x: string | Error, data: Record<string, any> = {}, important = false): void { // 実行を継続できない状況で使う
if (x instanceof Error) { if (x instanceof Error) {
data.e = x; data.e = x;
this.log('error', x.toString(), data, important); this.log('error', x.toString(), data, important);