MCPcopy
hub / github.com/vercel/next.js / logFetchMetric

Function logFetchMetric

packages/next/src/server/dev/log-requests.ts:148–198  ·  view source on GitHub ↗
(
  fetchMetric: FetchMetric,
  loggingConfig: LoggingConfig | undefined
)

Source from the content-addressed store, hash-verified

146}
147
148function logFetchMetric(
149 fetchMetric: FetchMetric,
150 loggingConfig: LoggingConfig | undefined
151): void {
152 let {
153 cacheReason,
154 cacheStatus,
155 cacheWarning,
156 end,
157 method,
158 start,
159 status,
160 url,
161 } = fetchMetric
162
163 if (cacheStatus === 'hmr' && !loggingConfig?.fetches?.hmrRefreshes) {
164 // Cache hits during HMR refreshes are intentionally not logged, unless
165 // explicitly enabled in the logging config.
166 return
167 }
168
169 if (loggingConfig?.fetches) {
170 if (url.length > 48 && !loggingConfig.fetches.fullUrl) {
171 url = truncateUrl(url)
172 }
173
174 writeLine(
175 white(
176 `${method} ${url} ${status} in ${Math.round(end - start)}ms ${formatCacheStatus(cacheStatus)}`
177 ),
178 1
179 )
180
181 if (cacheStatus === 'skip' || cacheStatus === 'miss') {
182 writeLine(
183 gray(
184 `Cache ${cacheStatus === 'skip' ? 'skipped' : 'missed'} reason: (${white(cacheReason)})`
185 ),
186 2
187 )
188 }
189 } else if (cacheWarning) {
190 // When logging for fetches is not enabled, we still want to print any
191 // associated warnings, so we print the request first to provide context.
192 writeLine(white(`${method} ${url}`), 1)
193 }
194
195 if (cacheWarning) {
196 writeLine(`${yellow(bold('⚠'))} ${white(cacheWarning)}`, 2)
197 }
198}
199
200function writeLine(text: string, indentationLevel = 0): void {
201 process.stdout.write(` ${'│ '.repeat(indentationLevel)}${text}\n`)

Callers 1

logRequestsFunction · 0.85

Calls 5

truncateUrlFunction · 0.85
writeLineFunction · 0.85
formatCacheStatusFunction · 0.85
grayFunction · 0.85
yellowFunction · 0.85

Tested by

no test coverage detected