Skip to content

Commit

Permalink
feat: disable development logging by setting logging: false in next…
Browse files Browse the repository at this point in the history
… 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: Jiachi Liu <inbox@huozhi.im>
Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 19, 2024
1 parent b88b442 commit 05decb1
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 18 deletions.
8 changes: 8 additions & 0 deletions docs/02-app/02-api-reference/05-next-config-js/logging.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
```
26 changes: 23 additions & 3 deletions packages/next/src/build/output/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ import * as Log from './log'
const MAX_LOG_SKIP_DURATION = 500 // 500ms

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
trigger: string | undefined
Expand Down Expand Up @@ -41,9 +51,15 @@ export const store = createStore<OutputState>({
appUrl: null,
bindAddr: null,
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) {
if (
(
Expand All @@ -66,6 +82,10 @@ let loadingLogTimer: NodeJS.Timeout | null = null
let traceSpan: Span | null = null

store.subscribe((state) => {
if (state.logging === false) {
return
}

if (!hasStoreChanged(state)) {
return
}
Expand Down
17 changes: 10 additions & 7 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,16 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
})
.optional(),
logging: z
.object({
fetches: z
.object({
fullUrl: z.boolean().optional(),
})
.optional(),
})
.union([
z.object({
fetches: z
.object({
fullUrl: z.boolean().optional(),
})
.optional(),
}),
z.boolean(),
])
.optional(),
modularizeImports: z
.record(
Expand Down
12 changes: 7 additions & 5 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,13 @@ export interface NextConfig extends Record<string, any> {
}
>

logging?: {
fetches?: {
fullUrl?: boolean
}
}
logging?:
| {
fetches?: {
fullUrl?: boolean
}
}
| boolean

/**
* period (in seconds) where the server allow to serve stale cache
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export async function createHotReloaderTurbopack(
loading: true,
trigger: id,
url: requestUrl,
logging: nextConfig.logging,
} as OutputState,
true
)
Expand All @@ -272,6 +273,7 @@ export async function createHotReloaderTurbopack(
consoleStore.setState(
{
loading: false,
logging: nextConfig.logging,
} as OutputState,
true
)
Expand Down
24 changes: 21 additions & 3 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,22 @@ export default class NextNodeServer extends BaseServer<
const normalizedReq = this.normalizeReq(req)
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 shouldTruncateUrl = !loggingFetchesConfig?.fullUrl

Expand All @@ -1131,6 +1146,11 @@ export default class NextNodeServer extends BaseServer<
const isMiddlewareRequest = getRequestMeta(req, 'middlewareInvoke')

const reqCallback = () => {
const fetchMetrics = normalizedReq.fetchMetrics || []
delete normalizedReq.fetchMetrics

if (isLoggingDisabled) return

// we don't log for non-route requests
const routeMatch = getRequestMeta(req).match

Expand All @@ -1141,7 +1161,6 @@ export default class NextNodeServer extends BaseServer<
const isRSC = getRequestMeta(normalizedReq, 'isRSCRequest')

const reqEnd = Date.now()
const fetchMetrics = normalizedReq.fetchMetrics || []
const reqDuration = reqEnd - reqStart

const statusColor = (status?: number) => {
Expand Down Expand Up @@ -1240,7 +1259,6 @@ export default class NextNodeServer extends BaseServer<
)
}
}
delete normalizedReq.fetchMetrics
originalResponse.off('close', reqCallback)
}
originalResponse.on('close', reqCallback)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page({ params: { id } }) {
return <p>{id}</p>
}
8 changes: 8 additions & 0 deletions test/development/app-dir/disable-logging-route/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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')
})
})
8 changes: 8 additions & 0 deletions test/development/app-dir/disable-logging-route/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
logging: false,
}

module.exports = nextConfig

0 comments on commit 05decb1

Please sign in to comment.