import { IMidwayLogger , MidwayContextLogger , MidwayTransformableInfo , } from '@midwayjs/logger' ; import { genISO8601String } from '@waiting/shared-core' ; import { Application , Context } from '@/interface' ; class CustomContextLogger extends MidwayContextLogger < Context > { formatContextLabel ( ) { const { ctx } = this ; const userId = ( ctx . userId as string ) || '-' ; const traceId = ctx . reqId || '-' ; const use = Date . now ( ) - ctx . startTime ; const ret = userId + '/' + ctx . ip + '/' + traceId + '/' + use . toString ( ) + 'ms ' + ctx . method + ' ' + ctx . url ; return ret ; } } export function customLogger ( logger , app ) { logger . updateTransformableInfo ( updateTransformableInfo ) ; app . setContextLoggerClass ( CustomContextLogger ) ; } export function updateTransformableInfo ( info ) { const ret = { ... info , timestamp : genISO8601String ( ) , } ; return ret ; } declare module 'egg' { interface Application { setContextLoggerClass : ( BaseContextLoggerClass : any ) => void ; } }