import { Binding , BindingScope , Context , ContextTags , inject , injectable , Provider , } from '@loopback/core' ; import { RequestContext , RestBindings } from '@loopback/rest' ; import { format } from 'util' ; import { LOGGER_SERVICE } from '../keys' ; import { log , logContexts , Logger } from '../util' ; remarks @ injectable ( { scope : BindingScope . SINGLETON , tags : { [ ContextTags . KEY ] : LOGGER_SERVICE } , } ) export class LoggerService implements Provider < Logger > { constructor ( @ inject . context ( ) resolutionCtx , @ inject . binding ( ) private binding ) { logContexts ( resolutionCtx , binding ) ; } value ( ) { const logger = ( message , ... args ) => { log ( '<<%s>> %s' , this . binding . key , format ( message , ... args ) ) ; } ; return logger ; } } remarks @ injectable ( { scope : BindingScope . TRANSIENT } ) export class RequestLoggerService implements Provider < Logger > { constructor ( @ inject . context ( ) resolutionCtx , @ inject . binding ( ) private binding , @ inject ( RestBindings . Http . CONTEXT ) private requestCtx ) { logContexts ( resolutionCtx , binding ) ; } value ( ) { const logger = ( message , ... args ) => { log ( '<<%s>> (%s %s) %s' , this . binding . key , this . requestCtx . request . method , this . requestCtx . request . originalUrl , format ( message , ... args ) ) ; } ; return logger ; } }