Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to support log full request url in verbose mode #55111

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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