diff --git a/CHANGELOG.md b/CHANGELOG.md index 119c01a8..22fec8b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Omit `variables` from log if it is an empty object + ### Fixed ### Removed diff --git a/lib/logger.js b/lib/logger.js index ee865feb..ee3c626e 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -5,6 +5,8 @@ const util = require('util') const IS_PRODUCTION = process.env.NODE_ENV === 'production' +const _isEmptyObject = o => Object.keys(o).length === 0 + const queryLogger = (req, _, next) => { let query = req.body?.query || (req.query.query && decodeURIComponent(req.query.query)) // Only log requests that contain a query @@ -25,13 +27,17 @@ const queryLogger = (req, _, next) => { // Ignore parsing errors, handled by GraphQL server } } - if (IS_PRODUCTION && variables) variables = '***' + if (IS_PRODUCTION && variables && !_isEmptyObject(variables)) variables = '***' - // Only add properties to object that aren't undefined - const queryInfo = Object.fromEntries(Object.entries({ operationName, variables }).filter(([, v]) => v)) + // Only add properties to object that aren't undefined or empty + const queryInfo = { + ...(operationName && { operationName }), + ...(variables && !_isEmptyObject(variables) && { variables }) + } // Only format queryInfo if it contains properties - const formattedQueryInfo = - Object.keys(queryInfo).length > 0 ? util.formatWithOptions({ colors: false, depth: null }, queryInfo) : undefined + const formattedQueryInfo = _isEmptyObject(queryInfo) + ? undefined + : util.formatWithOptions({ colors: false, depth: null }, queryInfo) // If query is multiline string, add newline padding to front let formattedQuery = query.includes('\n') ? `\n${query}` : query