import type { Logger } from 'log4js' ; import log4js from 'log4js' ; import config from './config' ; declare module 'log4js' { export interface Logger { divider ( symbol ? , length ? ) : void ; logWithScope ( newScope , ... args ) : void ; infoWithScope ( newScope , ... args ) : void ; errorWithScope ( newScope , ... args ) : void ; traceWithScope ( newScope , ... args ) : void ; warnWithScope ( newScope , ... args ) : void ; debugWithScope ( newScope , ... args ) : void ; } } log4js . configure ( config ) ; param logLevel const withScopeFactory = ( logLevel ) : Main . LogWithScope => { return ( newScope , message , ... args ) => { const logger = log4js . getLogger ( newScope ) ; logger [ logLevel ] ( message , ... args ) ; } ; } ; export const getLogger = ( scope ) => { const logger = log4js . getLogger ( scope ) ; logger . divider = ( str = '-' , length = 10 ) => { let line = '' ; for ( let i = 0 ; i < length ; i += 1 ) { line += str ; } logger . info ( line ) ; } ; logger . infoWithScope = withScopeFactory ( 'info' ) ; logger . errorWithScope = withScopeFactory ( 'error' ) ; logger . traceWithScope = withScopeFactory ( 'trace' ) ; logger . warnWithScope = withScopeFactory ( 'warn' ) ; logger . debugWithScope = withScopeFactory ( 'debug' ) ; return logger ; } ; export const logger = getLogger ( ) ; /** 将 logger 方法注入全局 * */ declare global { namespace NodeJS { interface Global { logger : Logger ; getLogger : Main . GetLogger ; } } } global . logger = logger ; global . getLogger = getLogger ;