diff --git a/middleware/jsrt/jsrt.go b/middleware/jsrt/jsrt.go index 6d58a53fd..cd691ef30 100644 --- a/middleware/jsrt/jsrt.go +++ b/middleware/jsrt/jsrt.go @@ -502,6 +502,11 @@ func JSRuntimeMiddleware() *gin.HandlerFunc { return } + duration := time.Since(start) + if duration > time.Millisecond*100 { + common.SysLog(fmt.Sprintf("JS Runtime PreProcess took %v", duration)) + } + // 后处理 if pool.hasPostProcessFunction() { writer := newResponseWriter(c.Writer) @@ -511,6 +516,8 @@ func JSRuntimeMiddleware() *gin.HandlerFunc { // 后处理响应 if writer.body.Len() > 0 { + start := time.Now() + statusCode, body, err := pool.PostProcessResponse(c, writer.statusCode, writer.body.Bytes()) if err == nil { c.Writer = writer.ResponseWriter @@ -548,6 +555,11 @@ func JSRuntimeMiddleware() *gin.HandlerFunc { common.SysError(fmt.Sprintf("JS Runtime PostProcess Error: %v", err)) } + + duration := time.Since(start) + if duration > time.Millisecond*100 { + common.SysLog(fmt.Sprintf("JS Runtime PostProcess took %v", duration)) + } } else { // 没有响应体时,恢复原始writer c.Writer = writer.ResponseWriter @@ -555,12 +567,6 @@ func JSRuntimeMiddleware() *gin.HandlerFunc { } else { c.Next() } - - // 记录处理时间 - duration := time.Since(start) - if duration > time.Millisecond*100 { - common.SysLog(fmt.Sprintf("JS Runtime processing took %v", duration)) - } } return &fn } diff --git a/scripts/01_utils.js b/scripts/01_utils.js index 03630beb5..316e21a54 100644 --- a/scripts/01_utils.js +++ b/scripts/01_utils.js @@ -1,8 +1,8 @@ // Utility functions for JavaScript runtime -function logWithTimestamp(message) { - const timestamp = new Date().toISOString(); - console.log(`[${timestamp}] ${message}`); +function logWithReq(message) { + let reqPath = req.url || 'unknown path'; + console.log(`[${req.method} ${reqPath}] ${message}`); } function safeJsonParse(str, defaultValue = null) { diff --git a/scripts/02_pre_process.js b/scripts/02_pre_process.js index ef01355a8..8dc86d02b 100644 --- a/scripts/02_pre_process.js +++ b/scripts/02_pre_process.js @@ -1,5 +1,5 @@ // Pre-processing function for incoming requests -function preProcessRequest(req) { - logWithTimestamp('Pre-processing request: ' + req.method + ' ' + req.url); +function preProcessRequest() { + logWithReq('Pre-processing request'); } diff --git a/scripts/03_post_process.js b/scripts/03_post_process.js index 92ae6b401..595959bde 100644 --- a/scripts/03_post_process.js +++ b/scripts/03_post_process.js @@ -1,5 +1,5 @@ // Post-processing function for outgoing responses -function postProcessResponse(req, res) { - logWithTimestamp('Post-processing response with status: ' + res.statusCode); +function postProcessResponse() { + logWithReq('Post-processing response with status: ' + req.statusCode); }