From 721b5ec1cad6c0b3ced180837d72db3782b9521f Mon Sep 17 00:00:00 2001 From: Robin Dahlberg Date: Tue, 31 Dec 2024 11:51:36 +0100 Subject: [PATCH 1/3] initial implementation --- packages/next/src/bin/next.ts | 4 ++++ packages/next/src/cli/next-start.ts | 3 +++ packages/next/src/server/lib/start-server.ts | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/packages/next/src/bin/next.ts b/packages/next/src/bin/next.ts index 957018f1219f8..ce5b01cfe05a9 100755 --- a/packages/next/src/bin/next.ts +++ b/packages/next/src/bin/next.ts @@ -339,6 +339,10 @@ program '-H, --hostname ', 'Specify a hostname on which to start the application (default: 0.0.0.0).' ) + .option( + '--access-log ', + 'Specify the path to where to write the access log.' + ) .addOption( new Option( '--keepAliveTimeout ', diff --git a/packages/next/src/cli/next-start.ts b/packages/next/src/cli/next-start.ts index 8be522893356c..474dfc7df16e4 100755 --- a/packages/next/src/cli/next-start.ts +++ b/packages/next/src/cli/next-start.ts @@ -13,6 +13,7 @@ export type NextStartOptions = { port: number hostname?: string keepAliveTimeout?: number + accessLog?: string } /** @@ -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)) { @@ -37,6 +39,7 @@ const nextStart = async (options: NextStartOptions, directory?: string) => { hostname, port, keepAliveTimeout, + accessLog }) } diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index 2742d0e7f7a1a..e2563e53c3bf9 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -46,6 +46,7 @@ export interface StartServerOptions { allowRetry?: boolean customServer?: boolean minimalMode?: boolean + accessLog?: string keepAliveTimeout?: number // this is dev-server only selfSignedCertificate?: SelfSignedCertificate @@ -100,6 +101,7 @@ export async function startServer( allowRetry, keepAliveTimeout, selfSignedCertificate, + accessLog, } = serverOptions let { port } = serverOptions @@ -145,6 +147,14 @@ export async function startServer( } async function requestListener(req: IncomingMessage, res: ServerResponse) { + if(accessLog) { + try { + fs.appendFileSync(accessLog, `${new Date().toISOString()} ${req.method} ${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 From 3f6aabf8b46eda9536770350ca25352f57ea8c9d Mon Sep 17 00:00:00 2001 From: Robin Dahlberg Date: Tue, 31 Dec 2024 12:12:51 +0100 Subject: [PATCH 2/3] prettier --- packages/next/src/cli/next-start.ts | 4 ++-- packages/next/src/server/lib/start-server.ts | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/next/src/cli/next-start.ts b/packages/next/src/cli/next-start.ts index 474dfc7df16e4..a9a4de08a952c 100755 --- a/packages/next/src/cli/next-start.ts +++ b/packages/next/src/cli/next-start.ts @@ -26,7 +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 accessLog = options.accessLog const keepAliveTimeout = options.keepAliveTimeout if (isPortIsReserved(port)) { @@ -39,7 +39,7 @@ const nextStart = async (options: NextStartOptions, directory?: string) => { hostname, port, keepAliveTimeout, - accessLog + accessLog, }) } diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index e2563e53c3bf9..224c873cdd4f4 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -147,12 +147,15 @@ export async function startServer( } async function requestListener(req: IncomingMessage, res: ServerResponse) { - if(accessLog) { + if (accessLog) { try { - fs.appendFileSync(accessLog, `${new Date().toISOString()} ${req.method} ${req.url} ${req.headers['user-agent']}\n`); + fs.appendFileSync( + accessLog, + `${new Date().toISOString()} ${req.method} ${req.url} ${req.headers['user-agent']}\n` + ) } catch (err) { - Log.error(`Failed to write to access log: ${accessLog}`); - console.error(err); + Log.error(`Failed to write to access log: ${accessLog}`) + console.error(err) } } try { From c6f9c97bab2fc8c4873b4a46bab270e250498012 Mon Sep 17 00:00:00 2001 From: Robin Dahlberg Date: Tue, 31 Dec 2024 12:46:54 +0100 Subject: [PATCH 3/3] added remoteAddress. --- packages/next/src/server/lib/start-server.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index 224c873cdd4f4..20492063b733a 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -149,10 +149,19 @@ export async function startServer( async function requestListener(req: IncomingMessage, res: ServerResponse) { if (accessLog) { try { - fs.appendFileSync( - accessLog, - `${new Date().toISOString()} ${req.method} ${req.url} ${req.headers['user-agent']}\n` - ) + 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)