@@ -3,11 +3,11 @@ import type { IncomingMessage } from 'http';
33import chalk from 'chalk' ;
44
55export interface ProxyLoggerOptions {
6- /**
7- * 是否启用详细日志
8- * @default false
6+ /**
7+ * 日志类型:'req'(仅请求前)、'res'(仅请求后)、'all'(全部)
8+ * @default 'res'
99 */
10- verbose ?: boolean ;
10+ logType ?: 'req' | 'res' | 'all' ;
1111 /**
1212 * 是否显示请求头信息
1313 * @default false
@@ -46,7 +46,7 @@ interface ProxyLogInfo {
4646}
4747
4848const defaultOptions : ProxyLoggerOptions = {
49- verbose : false ,
49+ logType : 'res' , // 默认显示请求后的日志
5050 showHeaders : false ,
5151 showTiming : true ,
5252 showProxyPath : true ,
@@ -104,7 +104,7 @@ export function proxyLogger(options: ProxyLoggerOptions = {}): Plugin {
104104 const reqId = `${ req . method } -${ req . url } }` ;
105105 requestTimes . set ( reqId , Date . now ( ) ) ;
106106
107- if ( opts . verbose ) {
107+ if ( ( opts . logType === 'req' || opts . logType === 'all' ) ) {
108108 const logInfo : ProxyLogInfo = {
109109 method : req . method || 'UNKNOWN' ,
110110 url : req . url || '' ,
@@ -126,18 +126,19 @@ export function proxyLogger(options: ProxyLoggerOptions = {}): Plugin {
126126 const duration = startTime ? Date . now ( ) - startTime : undefined ;
127127 requestTimes . delete ( reqId ) ;
128128
129- const logInfo : ProxyLogInfo = {
130- method : req . method || 'UNKNOWN' ,
131- url : req . url || '' ,
132- target : String ( options . target || '' ) ,
133- proxyPath : opts . showProxyPath ? key : undefined ,
134- statusCode : proxyRes . statusCode ,
135- duration : opts . showTiming ? duration : undefined ,
136- headers : opts . showHeaders ? proxyRes . headers : undefined ,
137- timestamp : new Date ( ) ,
138- } ;
139-
140- console . log ( formatLog ( logInfo ) ) ;
129+ if ( ( opts . logType === 'res' || opts . logType === 'all' ) ) {
130+ const logInfo : ProxyLogInfo = {
131+ method : req . method || 'UNKNOWN' ,
132+ url : req . url || '' ,
133+ target : String ( options . target || '' ) ,
134+ proxyPath : opts . showProxyPath ? key : undefined ,
135+ statusCode : proxyRes . statusCode ,
136+ duration : opts . showTiming ? duration : undefined ,
137+ headers : opts . showHeaders ? proxyRes . headers : undefined ,
138+ timestamp : new Date ( ) ,
139+ } ;
140+ console . log ( formatLog ( logInfo ) ) ;
141+ }
141142 } ) ;
142143
143144 // 错误处理
0 commit comments