@@ -34,8 +34,8 @@ export const log = async (ctx: Context, next: () => Promise<any>) => {
3434 await next ( ) ;
3535 const ms = Date . now ( ) - start ;
3636 ctx . set ( 'X-Response-Time' , `${ ms } ms` ) ;
37- const requestLog : boolean = config . getItem ( 'log.requestLog' ) ;
38- const level : string = config . getItem ( 'log.level' ) ;
37+ const requestLog : boolean = config . getItem ( 'log.requestLog' , true ) ;
38+ const level : string = config . getItem ( 'log.level' , 'INFO' ) ;
3939 if ( requestLog ) {
4040 if ( levels [ level ] <= levels [ 'DEBUG' ] ) {
4141 const data = {
@@ -61,6 +61,26 @@ export const log = async (ctx: Context, next: () => Promise<any>) => {
6161 ctx . app . emit ( 'error' , new HttpException ( { msg : ctx . message } ) , ctx ) ;
6262 }
6363 } catch ( err ) {
64+ const ms = Date . now ( ) - start ;
65+ const requestLog : boolean = config . getItem ( 'log.requestLog' , true ) ;
66+ const level : string = config . getItem ( 'log.level' , 'INFO' ) ;
67+ if ( requestLog ) {
68+ if ( levels [ level ] <= levels [ 'DEBUG' ] ) {
69+ const data = {
70+ param : ctx . request . query ,
71+ body : ctx . request . body
72+ } ;
73+ ctx . logger . debug (
74+ `[${ ctx . method } ] -> [${ ctx . url } ] from: ${
75+ ctx . ip
76+ } costs: ${ ms } ms data:${ JSON . stringify ( data , null , 4 ) } `
77+ ) ;
78+ } else {
79+ ctx . logger . info (
80+ `[${ ctx . method } ] -> [${ ctx . url } ] from: ${ ctx . ip } costs: ${ ms } ms`
81+ ) ;
82+ }
83+ }
6484 ctx . status = ctx . status || 500 ;
6585 ctx . app . emit ( 'error' , err , ctx ) ;
6686 }
0 commit comments