Unverified Commit 05decb11 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

feat: disable development logging by setting `logging: false` in next config (#67590)

### Why?

Logging compiled route `GET / in ...` is a very handy feature.
It would be more great if we have an option to opt-out the logging.

### How?

Set `logging` to false in next config.

```js
const nextConfig = {
 logging: false,
}
// ...
```

x-ref: #65992

Closes NEXT-3583
Closes NDX-26

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
Co-authored-by: 's avatarDelba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
parent b88b4428
...@@ -18,3 +18,11 @@ module.exports = { ...@@ -18,3 +18,11 @@ module.exports = {
}, },
} }
``` ```
In addition, you can disable the development logging by setting `logging` to `false`.
```js filename="next.config.js"
module.exports = {
logging: false,
}
```
...@@ -7,8 +7,18 @@ import * as Log from './log' ...@@ -7,8 +7,18 @@ import * as Log from './log'
const MAX_LOG_SKIP_DURATION = 500 // 500ms const MAX_LOG_SKIP_DURATION = 500 // 500ms
export type OutputState = export type OutputState =
| { bootstrap: true; appUrl: string | null; bindAddr: string | null } | {
| ({ bootstrap: false; appUrl: string | null; bindAddr: string | null } & ( bootstrap: true
appUrl: string | null
bindAddr: string | null
logging: boolean
}
| ({
bootstrap: false
appUrl: string | null
bindAddr: string | null
logging: boolean
} & (
| { | {
loading: true loading: true
trigger: string | undefined trigger: string | undefined
...@@ -41,9 +51,15 @@ export const store = createStore<OutputState>({ ...@@ -41,9 +51,15 @@ export const store = createStore<OutputState>({
appUrl: null, appUrl: null,
bindAddr: null, bindAddr: null,
bootstrap: true, bootstrap: true,
logging: true,
}) })
let lastStore: OutputState = { appUrl: null, bindAddr: null, bootstrap: true } let lastStore: OutputState = {
appUrl: null,
bindAddr: null,
bootstrap: true,
logging: true,
}
function hasStoreChanged(nextStore: OutputState) { function hasStoreChanged(nextStore: OutputState) {
if ( if (
( (
...@@ -66,6 +82,10 @@ let loadingLogTimer: NodeJS.Timeout | null = null ...@@ -66,6 +82,10 @@ let loadingLogTimer: NodeJS.Timeout | null = null
let traceSpan: Span | null = null let traceSpan: Span | null = null
store.subscribe((state) => { store.subscribe((state) => {
if (state.logging === false) {
return
}
if (!hasStoreChanged(state)) { if (!hasStoreChanged(state)) {
return return
} }
......
...@@ -533,13 +533,16 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -533,13 +533,16 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
}) })
.optional(), .optional(),
logging: z logging: z
.object({ .union([
fetches: z z.object({
.object({ fetches: z
fullUrl: z.boolean().optional(), .object({
}) fullUrl: z.boolean().optional(),
.optional(), })
}) .optional(),
}),
z.boolean(),
])
.optional(), .optional(),
modularizeImports: z modularizeImports: z
.record( .record(
......
...@@ -852,11 +852,13 @@ export interface NextConfig extends Record<string, any> { ...@@ -852,11 +852,13 @@ export interface NextConfig extends Record<string, any> {
} }
> >
logging?: { logging?:
fetches?: { | {
fullUrl?: boolean fetches?: {
} fullUrl?: boolean
} }
}
| boolean
/** /**
* period (in seconds) where the server allow to serve stale cache * period (in seconds) where the server allow to serve stale cache
......
...@@ -256,6 +256,7 @@ export async function createHotReloaderTurbopack( ...@@ -256,6 +256,7 @@ export async function createHotReloaderTurbopack(
loading: true, loading: true,
trigger: id, trigger: id,
url: requestUrl, url: requestUrl,
logging: nextConfig.logging,
} as OutputState, } as OutputState,
true true
) )
...@@ -272,6 +273,7 @@ export async function createHotReloaderTurbopack( ...@@ -272,6 +273,7 @@ export async function createHotReloaderTurbopack(
consoleStore.setState( consoleStore.setState(
{ {
loading: false, loading: false,
logging: nextConfig.logging,
} as OutputState, } as OutputState,
true true
) )
......
...@@ -1117,7 +1117,22 @@ export default class NextNodeServer extends BaseServer< ...@@ -1117,7 +1117,22 @@ export default class NextNodeServer extends BaseServer<
const normalizedReq = this.normalizeReq(req) const normalizedReq = this.normalizeReq(req)
const normalizedRes = this.normalizeRes(res) const normalizedRes = this.normalizeRes(res)
const loggingFetchesConfig = this.nextConfig.logging?.fetches let logging = this.nextConfig.logging
let isLoggingDisabled = false
if (typeof logging === 'boolean') {
if (logging === false) {
// disable all logging options if logging === false
logging = {}
isLoggingDisabled = true
}
// explicit check value for better readability
if (logging === true) {
// enable all logging options if logging === true
logging = { fetches: { fullUrl: true } }
}
}
const loggingFetchesConfig = logging?.fetches
const enabledVerboseLogging = !!loggingFetchesConfig const enabledVerboseLogging = !!loggingFetchesConfig
const shouldTruncateUrl = !loggingFetchesConfig?.fullUrl const shouldTruncateUrl = !loggingFetchesConfig?.fullUrl
...@@ -1131,6 +1146,11 @@ export default class NextNodeServer extends BaseServer< ...@@ -1131,6 +1146,11 @@ export default class NextNodeServer extends BaseServer<
const isMiddlewareRequest = getRequestMeta(req, 'middlewareInvoke') const isMiddlewareRequest = getRequestMeta(req, 'middlewareInvoke')
const reqCallback = () => { const reqCallback = () => {
const fetchMetrics = normalizedReq.fetchMetrics || []
delete normalizedReq.fetchMetrics
if (isLoggingDisabled) return
// we don't log for non-route requests // we don't log for non-route requests
const routeMatch = getRequestMeta(req).match const routeMatch = getRequestMeta(req).match
...@@ -1141,7 +1161,6 @@ export default class NextNodeServer extends BaseServer< ...@@ -1141,7 +1161,6 @@ export default class NextNodeServer extends BaseServer<
const isRSC = getRequestMeta(normalizedReq, 'isRSCRequest') const isRSC = getRequestMeta(normalizedReq, 'isRSCRequest')
const reqEnd = Date.now() const reqEnd = Date.now()
const fetchMetrics = normalizedReq.fetchMetrics || []
const reqDuration = reqEnd - reqStart const reqDuration = reqEnd - reqStart
const statusColor = (status?: number) => { const statusColor = (status?: number) => {
...@@ -1240,7 +1259,6 @@ export default class NextNodeServer extends BaseServer< ...@@ -1240,7 +1259,6 @@ export default class NextNodeServer extends BaseServer<
) )
} }
} }
delete normalizedReq.fetchMetrics
originalResponse.off('close', reqCallback) originalResponse.off('close', reqCallback)
} }
originalResponse.on('close', reqCallback) originalResponse.on('close', reqCallback)
......
export default function Page({ params: { id } }) {
return <p>{id}</p>
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'
describe('disable-logging-route', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should not log if disabled logging', async () => {
const html = await next.render('/slug1')
expect(html).toContain('slug1')
expect(next.cliOutput).not.toContain('GET /slug1')
// re-enable logging
await next.patchFile('next.config.js', (content) =>
content.replace('logging: false,', '')
)
// should log now
await check(async () => {
const html = await next.render('/slug1')
expect(html).toContain('slug1')
expect(next.cliOutput).toContain('GET /slug1')
return 'success'
}, 'success')
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
logging: false,
}
module.exports = nextConfig
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment