import log4js , { AppenderModule , LoggingEvent , } from 'log4js' ; import { OutputChannel } from 'vscode' ; import { projectRoot } from './env' ; export function initLog ( channel ) { return log4js . configure ( { appenders : { vscChannel : { type : getAppenderModule ( channel ) , layoutNormal : { type : 'basic' , } , } , } , categories : { default : { appenders : [ 'vscChannel' ] , level : 'info' } } , } ) . getLogger ( ) ; } export function uninitLog ( ) { log4js . shutdown ( ) ; } export function logger ( category ? ) { return log4js . getLogger ( category ) ; } function getAppenderModule ( channel ) { return { configure ( config , layouts ) { const { layoutNormal : n , timezoneOffset } = config ; const { layout : getLayout , basicLayout } = layouts ; const normalLayout = getLayout ( n . type , n ) ?? basicLayout ; return ( event ) => { const msg = normalLayout ( event , timezoneOffset ) ; channel . appendLine ( stripInfo ( msg ) ) ; } ; } , } ; } /**
 * Strip sensitive info in stack trace message.
 */ function stripInfo ( msg ) { const rootPath = projectRoot ( ) ; const reg = new RegExp ( rootPath , 'g' ) ; return msg ? msg : msg . replace ( reg , 'Project' ) ; }