'use strict' ; import { Config } from '@Base/config' ; import fs from 'fs' ; import http from 'http' ; import winston , { transports } from 'winston' ; import { Options } from 'morgan' ; interface ALogger { info ( msg ) : void ; warn ( msg ) : void ; debug ( msg ) : void ; cdebug ( flag , msg ) : void ; error ( msg ) : void ; setLogLevel ( level ) : void ; } ; export let logger = winston . createLogger ( { 'level' : 'debug' , 'format' : winston . format . combine ( winston . format . timestamp ( ) , winston . format . printf ( info => { return `${ info . timestamp } ${ info . level } ${ info . message }` ; } ) ) , 'transports' : [ new winston . transports . Console ( { 'level' : 'debug' } ) ] } ) ; export function initLogging ( ) { let logDir = Config . debug [ "log-directory" ] ; if ( ! fs . existsSync ( logDir ) ) { try { fs . mkdirSync ( logDir ) ; } catch ( e ) { logger . error ( `Logger: could not create log directory "${ logDir }": ${ e }` ) ; logDir = '.' ; } } ; const logTransports = [ ] ; if ( Config . debug [ "log-to-files" ] ) { logTransports . push ( new winston . transports . File ( { 'level' : Config . debug . loglevel , 'filename' : Config . debug [ 'log-filename' ] , 'dirname' : logDir , 'maxsize' : Config . debug [ 'log-max-size-megabytes' ] * 1000000 , 'maxFiles' : Config . debug [ 'log-max-files' ] , 'tailable' : Config . debug [ "log-tailable" ] , 'zippedArchive' : Config . debug [ "log-compress" ] } ) ) ; } ; if ( Config . debug [ "log-to-console" ] ) { logTransports . push ( new winston . transports . Console ( { 'level' : Config . debug . loglevel } ) ) ; } ; logger = winston . createLogger ( { 'level' : Config . debug . loglevel , 'format' : winston . format . combine ( winston . format . timestamp ( ) , winston . format . printf ( info => { return `${ info . timestamp } ${ info . level } ${ info . message }` ; } ) ) , 'transports' : logTransports , } ) ; } ; export const Logger = { info : ( msg ) => { logger . log ( 'info' , msg ) ; } , warn : ( msg ) => { logger . log ( 'warn' , msg ) ; } , debug : ( msg ) => { logger . log ( 'debug' , msg ) ; } , cdebug : ( flag , msg ) => { if ( ( Config . debug as any ) [ flag ] ) { logger . log ( 'debug' , msg ) ; } ; } , error : ( msg ) => { logger . log ( 'error' , msg ) ; } , setLogLevel : ( level ) => { if ( level ) { const aLevel = level . toLowerCase ( ) ; if ( [ 'info' , 'warn' , 'debug' , 'error' ] . includes ( aLevel ) ) { transports . File . level = aLevel ; transports . Console . level = aLevel ; } else { Logger . error ( `Logger.setLogLevel: unknown level name: ${ level }` ) ; Logger . setLogLevel ( 'info' ) ; } ; } else { Logger . error ( `Logger.setLogLevel: false level name: ${ level }` ) ; transports . File . level = 'debug' ; } ; } } ; export const morganOptions = { stream : { write ( msg ) { Logger . debug ( msg ) ; } } } ;