import log4js = require ( 'log4js' ) ; import * as env from 'dotenv' ; import * as fs from 'fs' ; import { Logger } from 'log4js' ; env . config ( ) ; since date author Luminous(BGLuminous)
  export class LogFactory { protected static currentPath = process . env . LOG_PATH ? process . env . LOG_PATH : './' ; protected static config = { appenders : { console : { type : 'console' , layout : { type : 'colored' } } , default : { type : 'dateFile' , filename : `${ LogFactory . currentPath }/onirii.log` , pattern : '.yyyy-MM-dd' , keepFileExt : true , encoding : 'UTF-8' , } , } , categories : { default : { appenders : [ `default` , 'console' ] , level : 'debug' , } , } , } ; private static dirReady = false ; param { string } key return { Logger } public static create ( key ) { if ( ! this . dirReady ) { if ( ! fs . existsSync ( this . currentPath ) ) { fs . mkdirSync ( this . currentPath ) ; } this . dirReady = true ; } if ( Object . keys ( this . config . appenders ) . includes ( key , 0 ) ) { return log4js . getLogger ( key ) ; } this . config . appenders = Object . assign ( this . config . appenders , LogFactory . generateAppender ( key ) ) ; this . config . categories = Object . assign ( this . config . categories , LogFactory . generateCategories ( key ) ) ; log4js . configure ( this . config ) ; return log4js . getLogger ( key ) ; } param { string } key return { { [ p : string ] : { filename : string , keepFileExt : boolean , pattern : string , type : string , encoding : string } } } private private static generateAppender ( key ) { return { [ key ] : { type : 'dateFile' , filename : `${ this . currentPath }/onirii-${ key }.log` , pattern : '.yyyy-MM-dd' , keepFileExt : true , encoding : 'UTF-8' , } , } ; } param { string } key return { { [ p : string ] : { level : string , appenders : string [ ] } } } private private static generateCategories ( key ) { return { [ key ] : { appenders : [ `${ key }` , 'console' ] , level : 'debug' , } , } ; } }