From a812bb0866337784c68323beb963221df188ec64 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Thu, 21 Sep 2023 22:54:37 +0200 Subject: [PATCH] feat: add request data to logs --- nodemon.json | 3 ++- package.json | 2 ++ src/index.js | 10 ++++++++++ src/log.js | 11 +++++++++++ yarn.lock | 10 ++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/nodemon.json b/nodemon.json index 099d21a..fa42808 100644 --- a/nodemon.json +++ b/nodemon.json @@ -1,6 +1,7 @@ { "env": { - "NODE_ENV": "development" + "NODE_ENV": "development", + "LOG_LEVEL": "debug" }, "ext": "js,json,css,pug,md" } \ No newline at end of file diff --git a/package.json b/package.json index 6b61824..6d1ca37 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,14 @@ "doipjs": "^1.1.0", "dotenv": "^16.0.3", "express": "^4.17.1", + "express-http-context2": "^1.0.0", "express-validator": "^6.13.0", "got": "^11.8.2", "hash-wasm": "^4.9.0", "jstransformer-markdown-it": "^3.0.0", "keyv": "^4.5.0", "libravatar": "^3.0.0", + "nanoid": "^5.0.1", "openpgp": "^5.5.0", "pug": "^3.0.0", "qrcode": "^1.4.4", diff --git a/src/index.js b/src/index.js index 691e462..f99b1d4 100644 --- a/src/index.js +++ b/src/index.js @@ -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 . */ import express from 'express' +import * as httpContext from 'express-http-context2' +import { nanoid } from 'nanoid' import { readFileSync } from 'fs' import { stringReplace } from 'string-replace-middleware' import * as pug from 'pug' @@ -53,8 +55,16 @@ app.set('keyoxide_version', packageData.version) app.set('onion_url', process.env.ONION_URL) // Middlewares +app.use(httpContext.middleware) app.use((req, res, next) => { 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() }) diff --git a/src/log.js b/src/log.js index 3a4b0b5..afff79e 100644 --- a/src/log.js +++ b/src/log.js @@ -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 . */ import { createLogger, format, transports } from 'winston' +import * as httpContext from 'express-http-context2' import * as dotenv from 'dotenv' dotenv.config() @@ -37,13 +38,23 @@ const anonymize = format((info, opts) => { info.keyserver_domain = undefined info.username = undefined info.fingerprint = undefined + info.request_path = undefined + info.request_ip = undefined } 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({ level: process.env.LOG_LEVEL || 'info', format: format.combine( + addRequestData(), anonymize(), format.timestamp(), format.json() diff --git a/yarn.lock b/yarn.lock index 546903d..cc01d7a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2264,6 +2264,11 @@ events@^3.2.0, events@^3.3.0: resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400" 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: version "6.15.0" 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" 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: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"