@@ -47,7 +47,7 @@ function getColoredMethod(method: string): string {
4747 }
4848
4949 const color = methodColors [ method ] || colors . white
50- return `${ color } ${ method . padEnd ( 7 ) } ${ colors . reset } `
50+ return `${ color } ${ method } ${ colors . reset } `
5151}
5252
5353/**
@@ -59,7 +59,9 @@ function getColoredDuration(ms: number): string {
5959 else if ( ms > 500 ) color = colors . yellow
6060 else if ( ms > 100 ) color = colors . cyan
6161
62- return `${ color } ${ ms . toFixed ( 0 ) } ms${ colors . reset } `
62+ // Show sub-millisecond precision if < 10ms
63+ const formatted = ms < 10 ? ms . toFixed ( 2 ) : ms < 100 ? ms . toFixed ( 1 ) : ms . toFixed ( 0 )
64+ return `${ color } ${ formatted } ms${ colors . reset } `
6365}
6466
6567/**
@@ -105,7 +107,8 @@ function shouldLog(pathname: string): boolean {
105107 * Enhanced middleware function with comprehensive HTTP access logging
106108 */
107109export function middleware ( request : NextRequest ) : NextResponse {
108- const startTime = Date . now ( )
110+ // Use high-resolution time for accurate duration
111+ const startTime = process . hrtime ( )
109112 const { pathname, search } = request . nextUrl
110113 const method = request . method
111114 const userAgent = request . headers . get ( 'user-agent' ) || 'unknown'
@@ -117,14 +120,17 @@ export function middleware(request: NextRequest): NextResponse {
117120
118121 // Only log if this request should be logged
119122 if ( shouldLog ( pathname ) ) {
120- // Add a custom header to track response time
121- response . headers . set ( 'x-request-start' , startTime . toString ( ) )
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 )
122126
123127 // Log the request immediately (before processing)
124128 const timestamp = new Date ( ) . toISOString ( )
125129 const fullUrl = pathname + search
126130 const status = response . status
127- const duration = Date . now ( ) - startTime
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
128134
129135 logger . info (
130136 `${ colors . gray } [${ timestamp } ]${ colors . reset } ` +
0 commit comments