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

Request logging for web server #74438

Open
wants to merge 3 commits into
base: canary
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions packages/next/src/bin/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ program
'-H, --hostname <hostname>',
'Specify a hostname on which to start the application (default: 0.0.0.0).'
)
.option(
'--access-log <path>',
'Specify the path to where to write the access log.'
)
.addOption(
new Option(
'--keepAliveTimeout <keepAliveTimeout>',
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/cli/next-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type NextStartOptions = {
port: number
hostname?: string
keepAliveTimeout?: number
accessLog?: string
}

/**
Expand All @@ -25,6 +26,7 @@ const nextStart = async (options: NextStartOptions, directory?: string) => {
const dir = getProjectDir(directory)
const hostname = options.hostname
const port = options.port
const accessLog = options.accessLog
const keepAliveTimeout = options.keepAliveTimeout

if (isPortIsReserved(port)) {
Expand All @@ -37,6 +39,7 @@ const nextStart = async (options: NextStartOptions, directory?: string) => {
hostname,
port,
keepAliveTimeout,
accessLog,
})
}

Expand Down
22 changes: 22 additions & 0 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface StartServerOptions {
allowRetry?: boolean
customServer?: boolean
minimalMode?: boolean
accessLog?: string
keepAliveTimeout?: number
// this is dev-server only
selfSignedCertificate?: SelfSignedCertificate
Expand Down Expand Up @@ -100,6 +101,7 @@ export async function startServer(
allowRetry,
keepAliveTimeout,
selfSignedCertificate,
accessLog,
} = serverOptions
let { port } = serverOptions

Expand Down Expand Up @@ -145,6 +147,26 @@ export async function startServer(
}

async function requestListener(req: IncomingMessage, res: ServerResponse) {
if (accessLog) {
try {
let remoteAddress

if (req.headers['x-forwarded-for']) {
remoteAddress = req.headers['x-forwarded-for'].split(',')[0]
} else {
remoteAddress = req.socket.remoteAddress
}
res.on('finish', () => {
fs.appendFileSync(
accessLog,
`${remoteAddress} ${new Date().toISOString()} ${req.method} ${res.statusCode} ${req.url} ${req.headers['user-agent']}\n`
)
})
} catch (err) {
Log.error(`Failed to write to access log: ${accessLog}`)
console.error(err)
}
}
try {
if (handlersPromise) {
await handlersPromise
Expand Down
Loading