From e5535776047b51611dabb376c128a3b8b8268748 Mon Sep 17 00:00:00 2001 From: Fionn Date: Thu, 19 Oct 2023 23:27:03 +0200 Subject: [PATCH] refactor logger --- package-lock.json | 302 ++++++++++++++++++++- package.json | 9 +- src/backend/app.ts | 17 +- src/backend/config.ts | 10 + src/backend/controllers/auth.controller.ts | 7 +- src/backend/logger.ts | 42 +++ src/backend/router.ts | 5 +- src/backend/services/bookings.service.ts | 8 +- src/backend/services/cdm.service.ts | 15 +- src/backend/utils/requestlogger.utils.ts | 22 -- 10 files changed, 374 insertions(+), 63 deletions(-) create mode 100644 src/backend/logger.ts delete mode 100644 src/backend/utils/requestlogger.utils.ts diff --git a/package-lock.json b/package-lock.json index 4fad5a4..75d08c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "1.1.0", "license": "GPL3", "dependencies": { - "@dotfionn/logger": "^1.0.1", "axios": "^0.27.2", "body-parser": "^1.20.0", "cookie-parser": "^1.4.6", @@ -20,7 +19,9 @@ "jsonwebtoken": "^8.5.1", "module-alias": "^2.2.2", "mongoose": "^6.5.0", - "point-in-polygon": "^1.1.0" + "morgan": "1.10.0", + "point-in-polygon": "^1.1.0", + "winston": "3.11.0" }, "devDependencies": { "@headlessui/react": "^1.7.15", @@ -31,6 +32,7 @@ "@types/express": "^4.17.13", "@types/jest": "^27.5.2", "@types/jsonwebtoken": "^8.5.8", + "@types/morgan": "1.9.7", "@types/node": "^18.6.2", "@types/react": "^18.0.15", "@types/react-dom": "^18.0.6", @@ -2644,11 +2646,22 @@ "node": ">=6.9.0" } }, - "node_modules/@dotfionn/logger": { - "version": "1.0.1", - "license": "ISC", + "node_modules/@colors/colors": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.6.0.tgz", + "integrity": "sha512-Ir+AOibqzrIsL6ajt3Rz3LskB7OiMVHqltZmspbW/TJuTVuyOMirVqAkjfY6JISiLHgyNqicAC8AyHHGzNd/dA==", + "engines": { + "node": ">=0.1.90" + } + }, + "node_modules/@dabh/diagnostics": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@dabh/diagnostics/-/diagnostics-2.0.3.tgz", + "integrity": "sha512-hrlQOIi7hAfzsMqlGSFyVucrx38O+j6wiGOf//H2ecvIEqYN4ADBSS2iLMh5UFyDunCNniUIPk/q3riFv45xRA==", "dependencies": { - "typescript": "^4.6.3" + "colorspace": "1.1.x", + "enabled": "2.0.x", + "kuler": "^2.0.0" } }, "node_modules/@emotion/is-prop-valid": { @@ -3624,6 +3637,15 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/morgan": { + "version": "1.9.7", + "resolved": "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.7.tgz", + "integrity": "sha512-4sJFBUBrIZkP5EvMm1L6VCXp3SQe8dnXqlVpe1jsmTjS1JQVmSjnpMNs8DosQd6omBi/K7BSKJ6z/Mc3ki0K9g==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/node": { "version": "18.16.19", "license": "MIT" @@ -3712,6 +3734,11 @@ "@types/jest": "*" } }, + "node_modules/@types/triple-beam": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@types/triple-beam/-/triple-beam-1.3.4.tgz", + "integrity": "sha512-HlJjF3wxV4R2VQkFpKe0YqJLilYNgtRtsqqZtby7RkVsSs+i+vbyzjtUwpFEdUCKcrGzCiEJE7F/0mKjh0sunA==" + }, "node_modules/@types/webidl-conversions": { "version": "7.0.0", "license": "MIT" @@ -4209,6 +4236,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/async": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz", + "integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==" + }, "node_modules/asynckit": { "version": "0.4.0", "license": "MIT" @@ -4326,6 +4358,22 @@ ], "license": "MIT" }, + "node_modules/basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "dependencies": { + "safe-buffer": "5.1.2" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/basic-auth/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, "node_modules/binary-extensions": { "version": "2.2.0", "dev": true, @@ -4615,6 +4663,15 @@ "dev": true, "license": "MIT" }, + "node_modules/color": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/color/-/color-3.2.1.tgz", + "integrity": "sha512-aBl7dZI9ENN6fUGC7mWpMTPNHmWUSNan9tuWN6ahh5ZLNk9baLJOnSMlrQkHcrfFgz2/RigjUVAjdx36VcemKA==", + "dependencies": { + "color-convert": "^1.9.3", + "color-string": "^1.6.0" + } + }, "node_modules/color-convert": { "version": "2.0.1", "dev": true, @@ -4628,9 +4685,39 @@ }, "node_modules/color-name": { "version": "1.1.4", - "dev": true, "license": "MIT" }, + "node_modules/color-string": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.9.1.tgz", + "integrity": "sha512-shrVawQFojnZv6xM40anx4CkoDP+fZsw/ZerEMsW/pyzsRbElpsL/DBVW7q3ExxwusdNXI3lXpuhEZkzs8p5Eg==", + "dependencies": { + "color-name": "^1.0.0", + "simple-swizzle": "^0.2.2" + } + }, + "node_modules/color/node_modules/color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dependencies": { + "color-name": "1.1.3" + } + }, + "node_modules/color/node_modules/color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==" + }, + "node_modules/colorspace": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/colorspace/-/colorspace-1.1.4.tgz", + "integrity": "sha512-BgvKJiuVu1igBUF2kEjRCZXol6wiiGbY5ipL/oVPwm0BL9sIpMIzM8IK7vwuxIIzOXMV3Ey5w+vxhm0rR/TN8w==", + "dependencies": { + "color": "^3.1.3", + "text-hex": "1.0.x" + } + }, "node_modules/combined-stream": { "version": "1.0.8", "license": "MIT", @@ -4963,6 +5050,11 @@ "dev": true, "license": "ISC" }, + "node_modules/enabled": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/enabled/-/enabled-2.0.0.tgz", + "integrity": "sha512-AKrN98kuwOzMIdAizXGI86UFBoo26CL21UM763y1h/GMSJ4/OHU9k2YlsmBpyScFo/wbLzWQJBMCW4+IO3/+OQ==" + }, "node_modules/encodeurl": { "version": "1.0.2", "license": "MIT", @@ -5781,6 +5873,11 @@ "reusify": "^1.0.4" } }, + "node_modules/fecha": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/fecha/-/fecha-4.2.3.tgz", + "integrity": "sha512-OP2IUU6HeYKJi3i0z4A19kHMQoLVs4Hc+DPqqxI2h/DPZHTm/vjsfC6P0b4jCMy14XizLBqvndQ+UilD7707Jw==" + }, "node_modules/file-entry-cache": { "version": "6.0.1", "dev": true, @@ -5888,6 +5985,11 @@ "tailwindcss": "^3" } }, + "node_modules/fn.name": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz", + "integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==" + }, "node_modules/follow-redirects": { "version": "1.15.2", "funding": [ @@ -6396,6 +6498,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-arrayish": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", + "integrity": "sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==" + }, "node_modules/is-bigint": { "version": "1.0.4", "dev": true, @@ -6582,6 +6689,17 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-stream": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", + "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==", + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-string": { "version": "1.0.7", "dev": true, @@ -6805,6 +6923,11 @@ "node": ">=12.0.0" } }, + "node_modules/kuler": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/kuler/-/kuler-2.0.0.tgz", + "integrity": "sha512-Xq9nH7KlWZmXAtodXDDRE7vs6DU1gTU8zYDHDiWLSip45Egwq3plLHzPn27NgvzL2r1LMPC1vdqh98sQxtqj4A==" + }, "node_modules/levn": { "version": "0.4.1", "dev": true, @@ -6888,6 +7011,22 @@ "version": "4.1.1", "license": "MIT" }, + "node_modules/logform": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/logform/-/logform-2.6.0.tgz", + "integrity": "sha512-1ulHeNPp6k/LD8H91o7VYFBng5i1BDE7HoKxVbZiGFidS1Rj65qcywLxX+pVfAPoQJEjRdvKcusKwOupHCVOVQ==", + "dependencies": { + "@colors/colors": "1.6.0", + "@types/triple-beam": "^1.3.2", + "fecha": "^4.2.0", + "ms": "^2.1.1", + "safe-stable-stringify": "^2.3.1", + "triple-beam": "^1.3.0" + }, + "engines": { + "node": ">= 12.0.0" + } + }, "node_modules/loose-envify": { "version": "1.4.0", "dev": true, @@ -7089,6 +7228,45 @@ "version": "2.1.3", "license": "MIT" }, + "node_modules/morgan": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", + "integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==", + "dependencies": { + "basic-auth": "~2.0.1", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-finished": "~2.3.0", + "on-headers": "~1.0.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/morgan/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/morgan/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" + }, + "node_modules/morgan/node_modules/on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/mpath": { "version": "0.9.0", "license": "MIT", @@ -7281,6 +7459,14 @@ "node": ">= 0.8" } }, + "node_modules/on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/once": { "version": "1.4.0", "dev": true, @@ -7289,6 +7475,14 @@ "wrappy": "1" } }, + "node_modules/one-time": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/one-time/-/one-time-1.0.0.tgz", + "integrity": "sha512-5DXOiRKwuSEcQ/l0kGCF6Q3jcADFv5tSmRaJck/OqkVFcOzutB134KRSfF0xDrL39MNnqxbHBbUUcjZIhTgb2g==", + "dependencies": { + "fn.name": "1.x.x" + } + }, "node_modules/optionator": { "version": "0.9.3", "dev": true, @@ -7869,6 +8063,19 @@ "node": ">=0.10.0" } }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/readdirp": { "version": "3.6.0", "dev": true, @@ -8134,6 +8341,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/safe-stable-stringify": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.4.3.tgz", + "integrity": "sha512-e2bDA2WJT0wxseVd4lsDP4+3ONX6HpMXQa1ZhFQ7SU+GjvORCmShbCMltrtIDfkYhVHrOcPtj+KhmDBdPdZD1g==", + "engines": { + "node": ">=10" + } + }, "node_modules/safer-buffer": { "version": "2.1.2", "license": "MIT" @@ -8259,6 +8474,14 @@ "version": "16.0.1", "license": "MIT" }, + "node_modules/simple-swizzle": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", + "integrity": "sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==", + "dependencies": { + "is-arrayish": "^0.3.1" + } + }, "node_modules/slash": { "version": "2.0.0", "dev": true, @@ -8315,6 +8538,14 @@ "node": "*" } }, + "node_modules/stack-trace": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", + "integrity": "sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==", + "engines": { + "node": "*" + } + }, "node_modules/statuses": { "version": "2.0.1", "license": "MIT", @@ -8341,6 +8572,14 @@ "duplexer": "~0.1.1" } }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/string-argv": { "version": "0.1.2", "dev": true, @@ -8611,6 +8850,11 @@ "node": ">=10.13.0" } }, + "node_modules/text-hex": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz", + "integrity": "sha512-uuVGNWzgJ4yhRaNSiubPY7OjISw4sw4E5Uv0wbjp+OzcbmVU/rsT8ujgcXJhn9ypzsgr5vlzpPqP+MBBKcGvbg==" + }, "node_modules/text-table": { "version": "0.2.0", "dev": true, @@ -8676,6 +8920,14 @@ "node": ">=12" } }, + "node_modules/triple-beam": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/triple-beam/-/triple-beam-1.4.1.tgz", + "integrity": "sha512-aZbgViZrg1QNcG+LULa7nhZpJTZSLm/mXnHXnbAbjmN5aSa0y7V+wvv6+4WaBtpISJzThKy+PIPxc1Nq1EJ9mg==", + "engines": { + "node": ">= 14.0.0" + } + }, "node_modules/ts-interface-checker": { "version": "0.1.13", "dev": true, @@ -8801,6 +9053,7 @@ }, "node_modules/typescript": { "version": "4.9.5", + "dev": true, "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", @@ -8910,7 +9163,6 @@ }, "node_modules/util-deprecate": { "version": "1.0.2", - "dev": true, "license": "MIT" }, "node_modules/utils-merge": { @@ -9074,6 +9326,40 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/winston": { + "version": "3.11.0", + "resolved": "https://registry.npmjs.org/winston/-/winston-3.11.0.tgz", + "integrity": "sha512-L3yR6/MzZAOl0DsysUXHVjOwv8mKZ71TrA/41EIduGpOOV5LQVodqN+QdQ6BS6PJ/RdIshZhq84P/fStEZkk7g==", + "dependencies": { + "@colors/colors": "^1.6.0", + "@dabh/diagnostics": "^2.0.2", + "async": "^3.2.3", + "is-stream": "^2.0.0", + "logform": "^2.4.0", + "one-time": "^1.0.0", + "readable-stream": "^3.4.0", + "safe-stable-stringify": "^2.3.1", + "stack-trace": "0.0.x", + "triple-beam": "^1.3.0", + "winston-transport": "^4.5.0" + }, + "engines": { + "node": ">= 12.0.0" + } + }, + "node_modules/winston-transport": { + "version": "4.6.0", + "resolved": "https://registry.npmjs.org/winston-transport/-/winston-transport-4.6.0.tgz", + "integrity": "sha512-wbBA9PbPAHxKiygo7ub7BYRiKxms0tpfU2ljtWzb3SjRjv5yl6Ozuy/TkXf00HTAt+Uylo3gSkNwzc4ME0wiIg==", + "dependencies": { + "logform": "^2.3.2", + "readable-stream": "^3.6.0", + "triple-beam": "^1.3.0" + }, + "engines": { + "node": ">= 12.0.0" + } + }, "node_modules/wrappy": { "version": "1.0.2", "dev": true, diff --git a/package.json b/package.json index 7dd65f7..a784bc8 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,9 @@ "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "paths": "resolve-tspaths --out \"dist\"", - "start": "resolve-tspaths --out \"dist\" && node --inspect=0.0.0.0:9229 dist/backend/app.js", "dev": "tsc-watch -p ./tsconfig.node.json --onSuccess \"npm run start\" --onFailure \"echo WHOOPS! Server compilation failed\"", - "spa-dev": "vite --config=src/frontend/vite.config.ts", "spa-build": "vite --config=src/frontend/vite.config.ts build", "spa-preview": "vite --config=src/frontend/vite.config.ts preview" @@ -23,7 +20,6 @@ "author": "Fionn Sperath / vACDM Team", "license": "GPL3", "dependencies": { - "@dotfionn/logger": "^1.0.1", "axios": "^0.27.2", "body-parser": "^1.20.0", "cookie-parser": "^1.4.6", @@ -34,7 +30,9 @@ "jsonwebtoken": "^8.5.1", "module-alias": "^2.2.2", "mongoose": "^6.5.0", - "point-in-polygon": "^1.1.0" + "morgan": "1.10.0", + "point-in-polygon": "^1.1.0", + "winston": "3.11.0" }, "devDependencies": { "@headlessui/react": "^1.7.15", @@ -45,6 +43,7 @@ "@types/express": "^4.17.13", "@types/jest": "^27.5.2", "@types/jsonwebtoken": "^8.5.8", + "@types/morgan": "1.9.7", "@types/node": "^18.6.2", "@types/react": "^18.0.15", "@types/react-dom": "^18.0.6", diff --git a/src/backend/app.ts b/src/backend/app.ts index cd5d3f1..9aec25e 100644 --- a/src/backend/app.ts +++ b/src/backend/app.ts @@ -1,18 +1,16 @@ -import Logger from '@dotfionn/logger'; import bodyparser from 'body-parser'; import cookieParser from 'cookie-parser'; import express from 'express'; import mongoose from 'mongoose'; import config from './config'; +import logger from './logger'; import router from './router'; import cdmService from './services/cdm.service'; import ecfmpService from './services/ecfmp.service'; import errors from '@/shared/errors'; -const logger = new Logger('vACDM:app'); - (async () => { logger.info('starting up'); @@ -20,6 +18,8 @@ const logger = new Logger('vACDM:app'); throw new Error('MONGO_URI has to be set!'); } + // TODO: default from mongoose7 will be false, needs to be checked + mongoose.set('strictQuery', true); await mongoose.connect(config().mongoUri); if (config().role == 'WORKER') { @@ -30,7 +30,7 @@ const logger = new Logger('vACDM:app'); try { await cdmService.cleanupPilots(); } catch (error) { - logger.error('error occurred when cleaning up pilots', error); + logger.error('error occurred when cleaning up pilots %o', error); } }, 10000); @@ -49,10 +49,9 @@ const logger = new Logger('vACDM:app'); try { await cdmService.cleanupUsers(); } catch (error) { - logger.error('error occurred when cleaning up users', error); + logger.error('error occurred when cleaning up users %o', error); } }, 6 * 60 * 60 * 1000); /* 6h */ - console.log(6 * 60 * 60 * 1000); // eslint-disable-next-line no-constant-condition while (true) { @@ -62,7 +61,7 @@ const logger = new Logger('vACDM:app'); try { await cdmService.optimizeBlockAssignments(); } catch (error) { - logger.error('error occurred when optimizing block assignments', error); + logger.error('error occurred when optimizing block assignments %o', error); } } @@ -89,7 +88,7 @@ const logger = new Logger('vACDM:app'); res: express.Response, next: express.NextFunction, ) => { - console.log(err); + logger.warn(err); if (err instanceof errors.APIError) { return res.status(err.responseCode).json(err); @@ -105,6 +104,6 @@ const logger = new Logger('vACDM:app'); const port = config().port; app.listen(port, () => { - logger.info('listening on port', port); + logger.info('listening on port %d', port); }); })(); diff --git a/src/backend/config.ts b/src/backend/config.ts index 7d668ef..cec9031 100644 --- a/src/backend/config.ts +++ b/src/backend/config.ts @@ -14,6 +14,11 @@ interface VacdmConfig { timeSinceLastLogin: number; }; + logging: { + levelConsole: string; + levelFile: string; + }; + eventUrl: string; eventPrio: number; eventPullInterval: number; @@ -62,6 +67,11 @@ export default function config(): VacdmConfig { timeSinceLastLogin: Number(process.env.TIME_LAST_LOGIN || 48) * 60 * 60 * 1000, }, + logging: { + levelConsole: process.env.LOG_LEVEL_CONSOLE || 'http', + levelFile: process.env.LOG_LEVEL_FILE || 'info', + }, + eventUrl: process.env.EVENT_URL || 'https://slots.vatsim-germany.org/api/events/', eventPrio: Number(process.env.EVENT_PRIO) || 5, eventPullInterval: Number(process.env.EVENT_PULL_INTERVAL || 5), diff --git a/src/backend/controllers/auth.controller.ts b/src/backend/controllers/auth.controller.ts index 5aaed08..749a15e 100644 --- a/src/backend/controllers/auth.controller.ts +++ b/src/backend/controllers/auth.controller.ts @@ -1,5 +1,6 @@ import { NextFunction, Request, Response } from 'express'; +import logger from '../logger'; import authService from '../services/auth.service'; import { UserDocument } from './../models/user.model'; @@ -23,7 +24,7 @@ export async function authUser( }); const user: UserDocument = await authService.getUserFromToken(response); - console.log('User is: ', user); + logger.debug('User is: %o', user); if (user.vacdm.atc || user.vacdm.admin) { return res.redirect('/atc'); @@ -42,7 +43,7 @@ export async function authUser( export async function getProfile( req: Request, res: Response, - next: NextFunction, + // next: NextFunction, ) { if (req.user) { req.user.access_token = ''; @@ -54,7 +55,7 @@ export async function getProfile( export async function logoutUser( req: Request, res: Response, - next: NextFunction, + // next: NextFunction, ) { res.clearCookie('vacdm_token'); res.json({ success: true }); diff --git a/src/backend/logger.ts b/src/backend/logger.ts new file mode 100644 index 0000000..8fe38e2 --- /dev/null +++ b/src/backend/logger.ts @@ -0,0 +1,42 @@ +import winston from 'winston'; + +import getConfig from './config'; + +const { logging } = getConfig(); + +const transports = [ + new winston.transports.File({ + filename: 'app.log', + level: logging.levelFile, + }), + new winston.transports.Console({ + level: logging.levelConsole, + format: winston.format.colorize({ all: true }), + }), +]; + +const logger = winston.createLogger({ + exitOnError: false, + format: winston.format.combine( + winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), + winston.format.splat(), + winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`), + ), + transports, +}); + +winston.addColors({ + error: 'brightRed', + warn: 'yellow', + info: 'green', + http: 'magenta', + verbose: 'brightBlue', + debug: 'brightCyan', + silly: 'gray', +}); + +process.on('warning', e => logger.warn('%o', e)); +process.on('uncaughtException', e => logger.error('%o', e)); +process.on('unhandledRejection', e => logger.error('%o', e)); + +export default logger; diff --git a/src/backend/router.ts b/src/backend/router.ts index 5ee78f8..66f8495 100644 --- a/src/backend/router.ts +++ b/src/backend/router.ts @@ -1,4 +1,5 @@ import { NextFunction, Request, Response, Router } from 'express'; +import morgan from 'morgan'; import airportController from './controllers/airport.controller'; import authController from './controllers/auth.controller'; @@ -6,14 +7,14 @@ import flowController from './controllers/flow.controller'; import metaController from './controllers/meta.controller'; import miscController from './controllers/misc.controller'; import pilotController from './controllers/pilot.controller'; +import logger from './logger'; import authMiddleware from './middleware/auth.middleware'; -import requestloggerUtils from './utils/requestlogger.utils'; import { APIError } from '@/shared/errors'; const router = Router(); -router.use(requestloggerUtils); +router.use(morgan('short', { stream: { write: m => logger.http(m.trim()) } })); router.get('/version', metaController.getVersion); router.get('/config', metaController.getPluginConfig); diff --git a/src/backend/services/bookings.service.ts b/src/backend/services/bookings.service.ts index ec169fc..1b68f3c 100644 --- a/src/backend/services/bookings.service.ts +++ b/src/backend/services/bookings.service.ts @@ -1,10 +1,8 @@ -import Logger from '@dotfionn/logger'; import axios from 'axios'; import dayjs from 'dayjs'; import config from '../config'; - -const logger = new Logger('vACDM:services:booking'); +import logger from '../logger'; let lastPull: Date | null = null; let relevantBookings: any[] | null = null; @@ -40,7 +38,7 @@ export async function getAllBookings() { return relevantBookings; } catch (e) { - console.log('error getting all bookings', e); + logger.warn('error getting all bookings %o', e); throw e; } } @@ -51,7 +49,7 @@ export async function pilotHasBooking(cid: number): Promise { return bookings.findIndex((b) => b.user === cid) != -1; } catch (e) { - console.log('error checking if pilot has booking', cid, e); + logger.warn('error checking if pilot has booking %s %o', cid, e); throw e; } } diff --git a/src/backend/services/cdm.service.ts b/src/backend/services/cdm.service.ts index 11f4979..f2c017b 100644 --- a/src/backend/services/cdm.service.ts +++ b/src/backend/services/cdm.service.ts @@ -1,7 +1,7 @@ -import Logger from '@dotfionn/logger'; import dayjs from 'dayjs'; import config from '../config'; +import logger from '../logger'; import pilotModel, { PilotDocument } from '../models/pilot.model'; import userModel from '../models/user.model'; import blockUtils from '../utils/block.utils'; @@ -12,10 +12,7 @@ import bookingsService from './bookings.service'; import datafeedService from './datafeed.service'; import pilotService from './pilot.service'; - - import { AirportCapacity } from '@/shared/interfaces/airport.interface'; -const logger = new Logger('vACDM:services:cdm'); export function determineInitialBlock(pilot: PilotDocument): { initialBlock: number; @@ -183,11 +180,11 @@ export async function cleanupPilots() { }) .exec(); - logger.debug('pilotsToBeDeleted', pilotsToBeDeleted); + logger.debug('pilotsToBeDeleted %o', pilotsToBeDeleted); for (const pilot of pilotsToBeDeleted) { pilotService.deletePilot(pilot.callsign); - logger.debug('deleted inactive pilot', pilot.callsign); + logger.debug('deleted inactive pilot %o', pilot.callsign); } // deactivate long not seen pilots @@ -202,7 +199,7 @@ export async function cleanupPilots() { }) .exec(); - logger.debug('pilotsToBeDeactivated', pilotsToBeDeactivated); + logger.debug('pilotsToBeDeactivated %o', pilotsToBeDeactivated); for (const pilot of pilotsToBeDeactivated) { pilot.inactive = true; @@ -216,7 +213,7 @@ export async function cleanupPilots() { }, }); - logger.debug('deactivating pilot', pilot.callsign); + logger.debug('deactivating pilot %o', pilot.callsign); await pilot.save(); } @@ -325,7 +322,7 @@ export async function optimizeBlockAssignments() { pilot.vacdm.delay -= (144 + pilot.vacdm.blockId - firstBlockId) % 144; pilot.vacdm.blockId = firstBlockId; - console.log('==========>> setting pilot times', pilot.callsign); + logger.debug('==========>> setting pilot times %o', pilot.callsign); await setTime(pilot); } diff --git a/src/backend/utils/requestlogger.utils.ts b/src/backend/utils/requestlogger.utils.ts deleted file mode 100644 index 73d3c72..0000000 --- a/src/backend/utils/requestlogger.utils.ts +++ /dev/null @@ -1,22 +0,0 @@ -import Logger from '@dotfionn/logger'; -import { NextFunction, Request, Response } from 'express'; - -const logger = new Logger('vACDM:api:request'); - -function loggerMiddleware(req: Request, res: Response, next: NextFunction) { - logger.log(`request: ${new Date().toISOString()} | ${req.ip} | ${req.method} ${req.originalUrl || req.url}`); - - const stringifiedBody = JSON.stringify(req.body, undefined, 2); - - if (stringifiedBody != '{}') { - const bodyLines = stringifiedBody.split('\n'); - - for (const line of bodyLines) { - logger.debug(`body: ${line}`); - } - } - - next(); -} - -export default loggerMiddleware;