@@ -107,8 +107,8 @@ function shouldLog(pathname: string): boolean {
107
107
* Enhanced middleware function with comprehensive HTTP access logging
108
108
*/
109
109
export function middleware ( request : NextRequest ) : NextResponse {
110
- // Use high-resolution time for accurate duration
111
- const startTime = process . hrtime ( )
110
+ // Use Date.now() for Edge Runtime compatibility
111
+ const startTime = Date . now ( )
112
112
const { pathname, search } = request . nextUrl
113
113
const method = request . method
114
114
const userAgent = request . headers . get ( 'user-agent' ) || 'unknown'
@@ -120,17 +120,15 @@ export function middleware(request: NextRequest): NextResponse {
120
120
121
121
// Only log if this request should be logged
122
122
if ( shouldLog ( pathname ) ) {
123
- // Add a custom header to track response time (in nanoseconds)
124
- const startTimeNs = ( startTime [ 0 ] * 1e9 + startTime [ 1 ] ) . toString ( )
125
- response . headers . set ( 'x-request-start' , startTimeNs )
123
+ // Add a custom header to track response time (in ms)
124
+ response . headers . set ( 'x-request-start' , startTime . toString ( ) )
126
125
127
126
// Log the request immediately (before processing)
128
127
const timestamp = new Date ( ) . toISOString ( )
129
128
const fullUrl = pathname + search
130
129
const status = response . status
131
- // Calculate high-resolution duration in ms (may include decimals)
132
- const diff = process . hrtime ( startTime )
133
- const duration = diff [ 0 ] * 1000 + diff [ 1 ] / 1e6
130
+ // Calculate duration in ms
131
+ const duration = Date . now ( ) - startTime
134
132
135
133
logger . info (
136
134
`${ colors . gray } [${ timestamp } ]${ colors . reset } ` +
0 commit comments