From 8baf00154f6050761c7ca4bfb80d52ce7c828678 Mon Sep 17 00:00:00 2001 From: Mike Surowiec Date: Tue, 31 May 2022 16:34:10 -0500 Subject: [PATCH] disable per-request logging in production (#28056) * disable per-request logging in production * cleanup log disabling Co-authored-by: Peter Bengtsson --- middleware/index.js | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/middleware/index.js b/middleware/index.js index 415fa40932f0..dea5de6a0f0e 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -68,7 +68,6 @@ import fastRootRedirect from './fast-root-redirect.js' import trailingSlashes from './trailing-slashes.js' const { DEPLOYMENT_ENV, NODE_ENV } = process.env -const isDevelopment = NODE_ENV === 'development' const isAzureDeployment = DEPLOYMENT_ENV === 'azure' const isTest = NODE_ENV === 'test' || process.env.GITHUB_ACTIONS === 'true' @@ -78,26 +77,6 @@ const asyncMiddleware = (fn) => (req, res, next) => { Promise.resolve(fn(req, res, next)).catch(next) } -// By default, `:remote-addr` is described as following in the morgon docs: -// -// The remote address of the request. This will use req.ip, otherwise -// the standard req.connection.remoteAddress value (socket address). -// -// But in production, by default, `req.ip` is the IP of the Azure machine -// which is something like "104.156.87.177:28244" which is *not* the -// end user. BUT! Because we configure `app.set('trust proxy', true)` -// *before* morgain is enabled, it will use the first entry from -// the `x-forwarded-for` header which is looking like this: -// "75.40.90.27, 157.52.111.52, 104.156.87.177:5786" which is -// "{USER'S IP}, {FASTLY'S POP IP}, {AZURE'S IP}". -// Incidentally, that first IP in the comma separated list is the -// same as the value of `req.headers['fastly-client-ip']` but -// Fastly will put that into the X-Forwarded-IP. -// By leaning in to X-Forwarded-IP (*and* the use -// `app.set('trust proxy', true)`) we can express ourselves here -// without having to use vendor specific headers. -const productionLogFormat = `:remote-addr - ":method :url" :status - :response-time ms` - export default function (app) { // *** Request connection management *** if (!isTest) app.use(timeout) @@ -129,13 +108,10 @@ export default function (app) { } // *** Request logging *** - // Enabled in development and azure deployed environments - // Not enabled in Heroku because the Heroku router + papertrail already logs the request information - app.use( - morgan(isAzureDeployment ? productionLogFormat : 'dev', { - skip: (req, res) => !(isDevelopment || isAzureDeployment), - }) - ) + // Not enabled in Azure deployment because the request information is logged via another layer of the stack + if (!isAzureDeployment) { + app.use(morgan('dev')) + } // *** Observability *** if (process.env.DD_API_KEY) {