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

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

Merged
merged 17 commits into from
Jul 19, 2024
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
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 @@ -1118,7 +1118,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 } }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the normalize seems not useful here anymore

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// logging: boolean
if (typeof logging === 'boolean') {

  if (!logging) {
  
    // logging: false
  
  } else {
  
    // logging: true, so enable all options to true
    
  }
}

Maybe I should explicitly use true and false for better readability.

}
}

const loggingFetchesConfig = logging?.fetches
const enabledVerboseLogging = !!loggingFetchesConfig
const shouldTruncateUrl = !loggingFetchesConfig?.fullUrl

Expand All @@ -1132,6 +1147,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 @@ -1142,7 +1162,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 @@ -1252,7 +1271,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
Loading