Skip to content

Commit

Permalink
Add option to support log full request url in verbose mode (#55111)
Browse files Browse the repository at this point in the history
Fixes #52239
Closes NEXT-1604

Change the logging option to object for flexibility
```js
experimental: {
  logging: {
    level: 'verbose', // control log level
    fullUrl: true, // console fetching url logging
  }
}
```

### After vs Before
<img src="https://github.com/vercel/next.js/assets/4800338/e0a05e2d-b69e-4075-afe1-8179c827f548" width="400">
<img src="https://github.com/vercel/next.js/assets/4800338/89a73ae3-1999-49ac-86f0-2c7248adc22c" width="400">
  • Loading branch information
huozhi committed Sep 7, 2023
1 parent 904d8ee commit 8076d0c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
10 changes: 9 additions & 1 deletion packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,15 @@ const configSchema = {
},
},
logging: {
type: 'string',
type: 'object',
properties: {
level: {
type: 'string',
},
fullUrl: {
type: 'boolean',
},
},
},
serverMinification: {
type: 'boolean',
Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ export interface ExperimentalConfig {
useDeploymentId?: boolean
useDeploymentIdServerActions?: boolean
deploymentId?: string
logging?: 'verbose'
logging?: {
level?: 'verbose'
fullUrl?: false
}
appDocumentPreloading?: boolean
strictNextHead?: boolean
clientRouterFilter?: boolean
Expand Down
36 changes: 21 additions & 15 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ function writeStdoutLine(text: string) {
process.stdout.write(' ' + text + '\n')
}

function formatRequestUrl(url: string, maxLength: number | undefined) {
return maxLength !== undefined && url.length > maxLength
? url.substring(0, maxLength) + '..'
: url
}

export interface NodeRequestHandler {
(
req: IncomingMessage | BaseNextRequest,
Expand Down Expand Up @@ -1010,7 +1016,9 @@ export default class NextNodeServer extends BaseServer {
const normalizedRes = this.normalizeRes(res)

const enabledVerboseLogging =
this.nextConfig.experimental.logging === 'verbose'
this.nextConfig.experimental.logging?.level === 'verbose'
const shouldTruncateUrl = !this.nextConfig.experimental.logging?.fullUrl

if (this.renderOpts.dev) {
const _req = req as NodeNextRequest | IncomingMessage
const _res = res as NodeNextResponse | ServerResponse
Expand Down Expand Up @@ -1098,20 +1106,18 @@ export default class NextNodeServer extends BaseServer {

if (url.length > 48) {
const parsed = new URL(url)
const truncatedHost =
parsed.host.length > 16
? parsed.host.substring(0, 16) + '..'
: parsed.host

const truncatedPath =
parsed.pathname.length > 24
? parsed.pathname.substring(0, 24) + '..'
: parsed.pathname

const truncatedSearch =
parsed.search.length > 16
? parsed.search.substring(0, 16) + '..'
: parsed.search
const truncatedHost = formatRequestUrl(
parsed.host,
shouldTruncateUrl ? 16 : undefined
)
const truncatedPath = formatRequestUrl(
parsed.pathname,
shouldTruncateUrl ? 24 : undefined
)
const truncatedSearch = formatRequestUrl(
parsed.search,
shouldTruncateUrl ? 16 : undefined
)

url =
parsed.protocol +
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/app-dir/app-static/app-fetch-logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function parseLogsFromCli(cliOutput: string) {
}
parsedLogs.push(parsedLog)
}
// console.log('parsedLogs', parsedLogs)
return parsedLogs
}, [] as any[])
}
Expand All @@ -46,7 +45,7 @@ createNextDescribe(
'app/default-cache/page.js': new FileRef(
path.join(__dirname, 'app/default-cache/page.js')
),
'next.config.js': `module.exports = { experimental: { logging: 'verbose' } }`,
'next.config.js': `module.exports = { experimental: { logging: { level: 'verbose', fullUrl: true } } }`,
},
},
({ next, isNextDev }) => {
Expand Down Expand Up @@ -81,6 +80,9 @@ createNextDescribe(
log.url.includes('api/random?no-cache')
)

// expend full url
expect(logs.every((log) => log.url.includes('..'))).toBe(false)

if (logEntry?.cache === 'cache: no-cache') {
return 'success'
}
Expand Down Expand Up @@ -115,7 +117,6 @@ createNextDescribe(
log.url.includes('api/random?auto-cache')
)

console.log('logEntry?.cache', logEntry?.cache)
if (logEntry?.cache === 'cache-control: no-cache (hard refresh)') {
return 'success'
}
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/app-dir/app-static/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
logging: 'verbose',
logging: {
level: 'verbose',
},
incrementalCacheHandlerPath: process.env.CUSTOM_CACHE_HANDLER,
},

Expand Down

0 comments on commit 8076d0c

Please sign in to comment.