Unverified Commit 668dbd6d authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Make Server Function logging opt-in via `logging.serverFunctions` (#89321)

Server Function logging (#88277) is now opt-in, consistent with how
fetch logging works. This reduces noise in development logs by default.

Additionally, for `'use cache'` Server Functions called from the client,
the logging needs more work to be useful. Keeping it opt-in avoids
exposing incomplete functionality by default.

Server Function logging can be enabled in `next.config.js`:

```js
module.exports = {
  logging: {
    serverFunctions: true,
  },
}
```
parent 2e3fb9bd
...@@ -33,6 +33,25 @@ module.exports = { ...@@ -33,6 +33,25 @@ module.exports = {
} }
``` ```
### Server Functions
You can enable logging of [Server Function](https://react.dev/reference/rsc/server-functions) invocations during development by setting `logging.serverFunctions` to `true`.
```js filename="next.config.js"
module.exports = {
logging: {
serverFunctions: true,
},
}
```
When enabled, the terminal will display each Server Function call with its function name, arguments, and duration:
```bash filename="Terminal"
POST /
└─ ƒ myAction(arg1, arg2) in 5ms app/actions.ts
```
### Incoming Requests ### Incoming Requests
By default all the incoming requests will be logged in the console during development. You can use the `incomingRequests` option to decide which requests to ignore. By default all the incoming requests will be logged in the console during development. You can use the `incomingRequests` option to decide which requests to ignore.
......
...@@ -684,6 +684,9 @@ export async function handler( ...@@ -684,6 +684,9 @@ export async function handler(
cacheLifeProfiles: nextConfig.cacheLife, cacheLifeProfiles: nextConfig.cacheLife,
basePath: nextConfig.basePath, basePath: nextConfig.basePath,
serverActions: nextConfig.experimental.serverActions, serverActions: nextConfig.experimental.serverActions,
logServerFunctions:
typeof nextConfig.logging === 'object' &&
Boolean(nextConfig.logging.serverFunctions),
...(isDebugStaticShell || ...(isDebugStaticShell ||
isDebugDynamicAccesses || isDebugDynamicAccesses ||
......
...@@ -150,6 +150,9 @@ async function requestHandler( ...@@ -150,6 +150,9 @@ async function requestHandler(
cacheLifeProfiles: nextConfig.cacheLife, cacheLifeProfiles: nextConfig.cacheLife,
basePath: nextConfig.basePath, basePath: nextConfig.basePath,
serverActions: nextConfig.experimental.serverActions, serverActions: nextConfig.experimental.serverActions,
logServerFunctions:
typeof nextConfig.logging === 'object' &&
Boolean(nextConfig.logging.serverFunctions),
cacheComponents: Boolean(nextConfig.cacheComponents), cacheComponents: Boolean(nextConfig.cacheComponents),
experimental: { experimental: {
isRoutePPREnabled: false, isRoutePPREnabled: false,
......
...@@ -1084,9 +1084,12 @@ export async function handleAction({ ...@@ -1084,9 +1084,12 @@ export async function handleAction({
actionId! actionId!
] ]
// Log server action call in development // Log server action call in development when enabled
let logInfo: ServerActionLogInfo | null = null let logInfo: ServerActionLogInfo | null = null
if (process.env.NODE_ENV === 'development') { if (
process.env.NODE_ENV === 'development' &&
ctx.renderOpts.logServerFunctions
) {
const serverActionsManifest = getServerActionsManifest() const serverActionsManifest = getServerActionsManifest()
const runtime = process.env.NEXT_RUNTIME === 'edge' ? 'edge' : 'node' const runtime = process.env.NEXT_RUNTIME === 'edge' ? 'edge' : 'node'
const actionInfo = serverActionsManifest[runtime]?.[actionId!] const actionInfo = serverActionsManifest[runtime]?.[actionId!]
......
...@@ -138,6 +138,7 @@ export interface RenderOptsPartial { ...@@ -138,6 +138,7 @@ export interface RenderOptsPartial {
bodySizeLimit?: SizeLimit bodySizeLimit?: SizeLimit
allowedOrigins?: string[] allowedOrigins?: string[]
} }
logServerFunctions?: boolean
params?: ParsedUrlQuery params?: ParsedUrlQuery
isPrefetch?: boolean isPrefetch?: boolean
htmlLimitedBots: string | undefined htmlLimitedBots: string | undefined
......
...@@ -576,6 +576,9 @@ export default abstract class Server< ...@@ -576,6 +576,9 @@ export default abstract class Server<
onInstrumentationRequestError: onInstrumentationRequestError:
this.instrumentationOnRequestError.bind(this), this.instrumentationOnRequestError.bind(this),
reactMaxHeadersLength: this.nextConfig.reactMaxHeadersLength, reactMaxHeadersLength: this.nextConfig.reactMaxHeadersLength,
logServerFunctions:
typeof this.nextConfig.logging === 'object' &&
Boolean(this.nextConfig.logging.serverFunctions),
} }
this.pagesManifest = this.getPagesManifest() this.pagesManifest = this.getPagesManifest()
......
...@@ -640,6 +640,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -640,6 +640,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
}), }),
]) ])
.optional(), .optional(),
serverFunctions: z.boolean().optional(),
browserToTerminal: z browserToTerminal: z
.union([z.boolean(), z.enum(['error', 'warn'])]) .union([z.boolean(), z.enum(['error', 'warn'])])
.optional(), .optional(),
......
...@@ -305,6 +305,11 @@ export interface LoggingConfig { ...@@ -305,6 +305,11 @@ export interface LoggingConfig {
*/ */
incomingRequests?: boolean | IncomingRequestLoggingConfig incomingRequests?: boolean | IncomingRequestLoggingConfig
/**
* If true, Server Function invocations will be logged.
*/
serverFunctions?: boolean
/** /**
* Forward browser console logs to terminal. * Forward browser console logs to terminal.
* - `false`: Disable browser log forwarding * - `false`: Disable browser log forwarding
...@@ -1703,6 +1708,7 @@ export interface NextConfigRuntime { ...@@ -1703,6 +1708,7 @@ export interface NextConfigRuntime {
skipProxyUrlNormalize: NextConfigComplete['skipProxyUrlNormalize'] skipProxyUrlNormalize: NextConfigComplete['skipProxyUrlNormalize']
pageExtensions: NextConfigComplete['pageExtensions'] pageExtensions: NextConfigComplete['pageExtensions']
useFileSystemPublicRoutes: NextConfigComplete['useFileSystemPublicRoutes'] useFileSystemPublicRoutes: NextConfigComplete['useFileSystemPublicRoutes']
logging?: NextConfigComplete['logging']
experimental: Pick< experimental: Pick<
NextConfigComplete['experimental'], NextConfigComplete['experimental'],
...@@ -1843,6 +1849,7 @@ export function getNextConfigRuntime( ...@@ -1843,6 +1849,7 @@ export function getNextConfigRuntime(
skipProxyUrlNormalize: config.skipProxyUrlNormalize, skipProxyUrlNormalize: config.skipProxyUrlNormalize,
pageExtensions: config.pageExtensions, pageExtensions: config.pageExtensions,
useFileSystemPublicRoutes: config.useFileSystemPublicRoutes, useFileSystemPublicRoutes: config.useFileSystemPublicRoutes,
logging: config.logging,
experimental, experimental,
} }
......
module.exports = {
logging:
process.env.NEXT_TEST_SERVER_FUNCTION_LOGGING === 'true'
? { serverFunctions: true }
: undefined,
}
...@@ -6,6 +6,9 @@ describe('server-action-logging', () => { ...@@ -6,6 +6,9 @@ describe('server-action-logging', () => {
const { next, isNextStart, skipped } = nextTestSetup({ const { next, isNextStart, skipped } = nextTestSetup({
skipDeployment: true, skipDeployment: true,
files: __dirname, files: __dirname,
env: {
NEXT_TEST_SERVER_FUNCTION_LOGGING: 'true',
},
}) })
if (skipped) return if (skipped) return
...@@ -187,3 +190,28 @@ describe('server-action-logging', () => { ...@@ -187,3 +190,28 @@ describe('server-action-logging', () => {
}) })
}) })
}) })
describe('server-action-logging when logging.serverFunctions is not enabled', () => {
const { next, isNextDev, skipped } = nextTestSetup({
skipDeployment: true,
files: __dirname,
})
if (skipped) return
it('should not log server actions', async () => {
const browser = await next.browser('/')
const outputIndex = next.cliOutput.length
await browser.elementByCss('#success-action').click()
await browser.waitForElementByCss('#result')
await retry(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
if (isNextDev) {
expect(logs).toContain('POST /')
}
expect(logs).not.toContain('└─ ƒ successAction')
})
})
})
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