@@ -47,7 +47,7 @@ function getColoredMethod(method: string): string {
47
47
}
48
48
49
49
const color = methodColors [ method ] || colors . white
50
- return `${ color } ${ method . padEnd ( 7 ) } ${ colors . reset } `
50
+ return `${ color } ${ method } ${ colors . reset } `
51
51
}
52
52
53
53
/**
@@ -59,7 +59,9 @@ function getColoredDuration(ms: number): string {
59
59
else if ( ms > 500 ) color = colors . yellow
60
60
else if ( ms > 100 ) color = colors . cyan
61
61
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 } `
63
65
}
64
66
65
67
/**
@@ -105,7 +107,8 @@ function shouldLog(pathname: string): boolean {
105
107
* Enhanced middleware function with comprehensive HTTP access logging
106
108
*/
107
109
export function middleware ( request : NextRequest ) : NextResponse {
108
- const startTime = Date . now ( )
110
+ // Use high-resolution time for accurate duration
111
+ const startTime = process . hrtime ( )
109
112
const { pathname, search } = request . nextUrl
110
113
const method = request . method
111
114
const userAgent = request . headers . get ( 'user-agent' ) || 'unknown'
@@ -117,14 +120,17 @@ export function middleware(request: NextRequest): NextResponse {
117
120
118
121
// Only log if this request should be logged
119
122
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 )
122
126
123
127
// Log the request immediately (before processing)
124
128
const timestamp = new Date ( ) . toISOString ( )
125
129
const fullUrl = pathname + search
126
130
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
128
134
129
135
logger . info (
130
136
`${ colors . gray } [${ timestamp } ]${ colors . reset } ` +
0 commit comments