feat: add request data to logs

This commit is contained in:
Yarmo Mackenbach 2023-09-21 22:54:37 +02:00
parent 5f5e039a2c
commit a812bb0866
No known key found for this signature in database
GPG key ID: 3C57D093219103A3
5 changed files with 35 additions and 1 deletions

View file

@ -1,6 +1,7 @@
{ {
"env": { "env": {
"NODE_ENV": "development" "NODE_ENV": "development",
"LOG_LEVEL": "debug"
}, },
"ext": "js,json,css,pug,md" "ext": "js,json,css,pug,md"
} }

View file

@ -11,12 +11,14 @@
"doipjs": "^1.1.0", "doipjs": "^1.1.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.17.1", "express": "^4.17.1",
"express-http-context2": "^1.0.0",
"express-validator": "^6.13.0", "express-validator": "^6.13.0",
"got": "^11.8.2", "got": "^11.8.2",
"hash-wasm": "^4.9.0", "hash-wasm": "^4.9.0",
"jstransformer-markdown-it": "^3.0.0", "jstransformer-markdown-it": "^3.0.0",
"keyv": "^4.5.0", "keyv": "^4.5.0",
"libravatar": "^3.0.0", "libravatar": "^3.0.0",
"nanoid": "^5.0.1",
"openpgp": "^5.5.0", "openpgp": "^5.5.0",
"pug": "^3.0.0", "pug": "^3.0.0",
"qrcode": "^1.4.4", "qrcode": "^1.4.4",

View file

@ -28,6 +28,8 @@ if any, to sign a "copyright disclaimer" for the program, if necessary. For
more information on this, and how to apply and follow the GNU AGPL, see <https://www.gnu.org/licenses/>. more information on this, and how to apply and follow the GNU AGPL, see <https://www.gnu.org/licenses/>.
*/ */
import express from 'express' import express from 'express'
import * as httpContext from 'express-http-context2'
import { nanoid } from 'nanoid'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import { stringReplace } from 'string-replace-middleware' import { stringReplace } from 'string-replace-middleware'
import * as pug from 'pug' import * as pug from 'pug'
@ -53,8 +55,16 @@ app.set('keyoxide_version', packageData.version)
app.set('onion_url', process.env.ONION_URL) app.set('onion_url', process.env.ONION_URL)
// Middlewares // Middlewares
app.use(httpContext.middleware)
app.use((req, res, next) => { app.use((req, res, next) => {
res.setHeader('Permissions-Policy', 'interest-cohort=()') res.setHeader('Permissions-Policy', 'interest-cohort=()')
httpContext.set('requestId', nanoid())
httpContext.set('requestPath', req.path)
httpContext.set('requestIp', req.ip)
logger.info(`Handle a request`,
{ component: 'http_server', action: 'request' })
next() next()
}) })

View file

@ -28,6 +28,7 @@ if any, to sign a "copyright disclaimer" for the program, if necessary. For
more information on this, and how to apply and follow the GNU AGPL, see <https://www.gnu.org/licenses/>. more information on this, and how to apply and follow the GNU AGPL, see <https://www.gnu.org/licenses/>.
*/ */
import { createLogger, format, transports } from 'winston' import { createLogger, format, transports } from 'winston'
import * as httpContext from 'express-http-context2'
import * as dotenv from 'dotenv' import * as dotenv from 'dotenv'
dotenv.config() dotenv.config()
@ -37,13 +38,23 @@ const anonymize = format((info, opts) => {
info.keyserver_domain = undefined info.keyserver_domain = undefined
info.username = undefined info.username = undefined
info.fingerprint = undefined info.fingerprint = undefined
info.request_path = undefined
info.request_ip = undefined
} }
return info return info
}) })
const addRequestData = format((info, opts) => {
if (httpContext.get('requestId')) info.request_id = httpContext.get('requestId')
if (httpContext.get('requestPath')) info.request_path = httpContext.get('requestPath')
if (httpContext.get('requestIp')) info.request_ip = httpContext.get('requestIp')
return info
})
const logger = createLogger({ const logger = createLogger({
level: process.env.LOG_LEVEL || 'info', level: process.env.LOG_LEVEL || 'info',
format: format.combine( format: format.combine(
addRequestData(),
anonymize(), anonymize(),
format.timestamp(), format.timestamp(),
format.json() format.json()

View file

@ -2264,6 +2264,11 @@ events@^3.2.0, events@^3.3.0:
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400" resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q== integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==
express-http-context2@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/express-http-context2/-/express-http-context2-1.0.0.tgz#58cd9fb0d233739e0dcd7aabb766d1dc74522d77"
integrity sha512-xdukoNNpWcuMn5ZJcjDe/tA+2A96rQ1MyAB/oWUU7qP15Tkz3txQyFsw/QG8YgRzTJ1sNAA8Bdq0o5b/1Y4zLA==
express-validator@^6.10.0, express-validator@^6.13.0: express-validator@^6.10.0, express-validator@^6.13.0:
version "6.15.0" version "6.15.0"
resolved "https://registry.yarnpkg.com/express-validator/-/express-validator-6.15.0.tgz#5e4601428960b0d66f5f4ae09cb32ed2077374a4" resolved "https://registry.yarnpkg.com/express-validator/-/express-validator-6.15.0.tgz#5e4601428960b0d66f5f4ae09cb32ed2077374a4"
@ -3539,6 +3544,11 @@ nanoid@^3.3.6:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.6.tgz#443380c856d6e9f9824267d960b4236ad583ea4c" resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.6.tgz#443380c856d6e9f9824267d960b4236ad583ea4c"
integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA== integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==
nanoid@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-5.0.1.tgz#3e95d775a8bc8a98afbf0a237e2bbc6a71b0662e"
integrity sha512-vWeVtV5Cw68aML/QaZvqN/3QQXc6fBfIieAlu05m7FZW2Dgb+3f0xc0TTxuJW+7u30t7iSDTV/j3kVI0oJqIfQ==
natural-compare@^1.4.0: natural-compare@^1.4.0:
version "1.4.0" version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"