import { Injectable , IMiddleware } from '@tiejs/common' ; import { Request , Response , NextFunction } from 'express' ; import { ctxLogger } from './loggers/ctxLogger' ; declare global { namespace Express { interface Request { logger : any ; } } } @ Injectable ( ) export class LoggerMiddleware implements IMiddleware { async use ( req , _ , next ) { req . logger = { } ; const levels = [ 'debug' , 'info' , 'trace' , 'warn' , 'error' ] ; for ( const level of levels ) { req . logger [ level ] = ( ... msg ) => { ctxLogger [ level ] ( { ... getMsg ( ... msg ) , request : req , } ) ; } ; } next ( ) ; } } function getMsg ( ... args ) { if ( args . length == 0 ) return { message : '' } ; if ( args . length === 1 ) { if ( typeof args [ 0 ] === 'object' ) { return { ... args [ 0 ] } ; } else { return { message : args [ 0 ] } ; } } else { return { message : args } ; } }