From 4be22465fcb48e9fa7bc728103e4b0bfa1b8d12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Fri, 22 Sep 2023 15:26:55 +0100 Subject: [PATCH] chore(formatter): beautify request formatter --- package-lock.json | 132 +++++++++--------- package.json | 4 +- src/formatters/RequestFormatter.ts | 16 +-- tests/unit/formatters/RequestFormatterTest.ts | 3 +- 4 files changed, 75 insertions(+), 80 deletions(-) diff --git a/package-lock.json b/package-lock.json index 80d10c5..7adec8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,19 @@ { "name": "@athenna/logger", - "version": "4.4.0", + "version": "4.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/logger", - "version": "4.4.0", + "version": "4.5.0", "license": "MIT", "dependencies": { "cls-rtracer": "^2.6.3", "telegraf": "^4.12.2" }, "devDependencies": { - "@athenna/common": "^4.10.1", + "@athenna/common": "^4.13.1", "@athenna/config": "^4.4.0", "@athenna/ioc": "^4.3.0", "@athenna/test": "^4.7.0", @@ -67,9 +67,9 @@ "dev": true }, "node_modules/@athenna/common": { - "version": "4.10.1", - "resolved": "https://registry.npmjs.org/@athenna/common/-/common-4.10.1.tgz", - "integrity": "sha512-EVzdlfxP8dQVV5dSIQzUHXMYhby+TCxkGRtyz99nXovlkTOx//KOKE3NT6WXIZUPpAfARwQSQNfD1wEgUAmZMg==", + "version": "4.13.1", + "resolved": "https://registry.npmjs.org/@athenna/common/-/common-4.13.1.tgz", + "integrity": "sha512-9Dq7azVqW2qAjxO/T6RARaz8VXZ4U4bXAiaNVkxtpgB0+Gr8pbKmNnRqJzMgAYhD/Bxx2WWqgcYjZEfRKgnYqA==", "dev": true, "dependencies": { "@fastify/formbody": "^7.4.0", @@ -78,7 +78,7 @@ "chalk": "^5.3.0", "change-case": "^4.1.2", "collect.js": "^4.36.1", - "fastify": "^4.21.0", + "fastify": "^4.22.1", "got": "^12.6.1", "http-status-codes": "^2.2.0", "is-wsl": "^2.2.0", @@ -4221,9 +4221,9 @@ } }, "node_modules/fast-content-type-parse": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fast-content-type-parse/-/fast-content-type-parse-1.0.0.tgz", - "integrity": "sha512-Xbc4XcysUXcsP5aHUU7Nq3OwvHq97C+WnbkeIefpeYLX+ryzFJlU6OStFJhs6Ol0LkUGpcK+wL0JwfM+FCU5IA==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fast-content-type-parse/-/fast-content-type-parse-1.1.0.tgz", + "integrity": "sha512-fBHHqSTFLVnR61C+gltJuE5GkVQMV0S2nqUO8TJ+5Z3qAKG8vAx4FKai1s5jq/inV1+sREynIWSuQ6HgoSXpDQ==", "dev": true }, "node_modules/fast-decode-uri-component": { @@ -4345,9 +4345,9 @@ "dev": true }, "node_modules/fastify": { - "version": "4.21.0", - "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.21.0.tgz", - "integrity": "sha512-tsu4bcwE4HetxqW8prA5fbC9bKHMYDp7jGEDWyzK1l90a3uOaLoIcQbdGcWeODNLVJviQnzh1wvIjTZE3MJFEg==", + "version": "4.23.2", + "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.23.2.tgz", + "integrity": "sha512-WFSxsHES115svC7NrerNqZwwM0UOxbC/P6toT9LRHgAAFvG7o2AN5W+H4ihCtOGuYXjZf4z+2jXC89rVEoPWOA==", "dev": true, "dependencies": { "@fastify/ajv-compiler": "^3.5.0", @@ -4365,7 +4365,7 @@ "rfdc": "^1.3.0", "secure-json-parse": "^2.5.0", "semver": "^7.5.0", - "tiny-lru": "^11.0.1" + "toad-cache": "^3.2.0" } }, "node_modules/fastify-plugin": { @@ -6358,9 +6358,9 @@ } }, "node_modules/light-my-request": { - "version": "5.10.0", - "resolved": "https://registry.npmjs.org/light-my-request/-/light-my-request-5.10.0.tgz", - "integrity": "sha512-ZU2D9GmAcOUculTTdH9/zryej6n8TzT+fNGdNtm6SDp5MMMpHrJJkvAdE3c6d8d2chE9i+a//dS9CWZtisknqA==", + "version": "5.11.0", + "resolved": "https://registry.npmjs.org/light-my-request/-/light-my-request-5.11.0.tgz", + "integrity": "sha512-qkFCeloXCOMpmEdZ/MV91P8AT4fjwFXWaAFz3lUeStM8RcoM1ks4J/F8r1b3r6y/H4u3ACEJ1T+Gv5bopj7oDA==", "dev": true, "dependencies": { "cookie": "^0.5.0", @@ -7659,15 +7659,15 @@ } }, "node_modules/pino": { - "version": "8.15.0", - "resolved": "https://registry.npmjs.org/pino/-/pino-8.15.0.tgz", - "integrity": "sha512-olUADJByk4twxccmAxb1RiGKOSvddHugCV3wkqjyv+3Sooa2KLrmXrKEWOKi0XPCLasRR5jBXxioE1jxUa4KzQ==", + "version": "8.15.1", + "resolved": "https://registry.npmjs.org/pino/-/pino-8.15.1.tgz", + "integrity": "sha512-Cp4QzUQrvWCRJaQ8Lzv0mJzXVk4z2jlq8JNKMGaixC2Pz5L4l2p95TkuRvYbrEbe85NQsDKrAd4zalf7Ml6WiA==", "dev": true, "dependencies": { "atomic-sleep": "^1.0.0", "fast-redact": "^3.1.1", "on-exit-leak-free": "^2.1.0", - "pino-abstract-transport": "v1.0.0", + "pino-abstract-transport": "v1.1.0", "pino-std-serializers": "^6.0.0", "process-warning": "^2.0.0", "quick-format-unescaped": "^4.0.3", @@ -7681,9 +7681,9 @@ } }, "node_modules/pino-abstract-transport": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/pino-abstract-transport/-/pino-abstract-transport-1.0.0.tgz", - "integrity": "sha512-c7vo5OpW4wIS42hUVcT5REsL8ZljsUfBjqV/e2sFxmFEFZiq1XLUp5EYLtuDH6PEHq9W1egWqRbnLUP5FuZmOA==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pino-abstract-transport/-/pino-abstract-transport-1.1.0.tgz", + "integrity": "sha512-lsleG3/2a/JIWUtf9Q5gUNErBqwIu1tUKTT3dUzaf5DySw9ra1wcqKjJjLX1VTY64Wk1eEOYsVGSaGfCK85ekA==", "dev": true, "dependencies": { "readable-stream": "^4.0.0", @@ -9043,9 +9043,9 @@ "dev": true }, "node_modules/thread-stream": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-2.3.0.tgz", - "integrity": "sha512-kaDqm1DET9pp3NXwR8382WHbnpXnRkN9xGN9dQt3B2+dmXiW8X1SOwmFOxAErEQ47ObhZ96J6yhZNXuyCOL7KA==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-2.4.0.tgz", + "integrity": "sha512-xZYtOtmnA63zj04Q+F9bdEay5r47bvpo1CaNqsKi7TpoJHcotUez8Fkfo2RJWpW91lnnaApdpRbVwCWsy+ifcw==", "dev": true, "dependencies": { "real-require": "^0.2.0" @@ -9072,15 +9072,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/tiny-lru": { - "version": "11.0.1", - "resolved": "https://registry.npmjs.org/tiny-lru/-/tiny-lru-11.0.1.tgz", - "integrity": "sha512-iNgFugVuQgBKrqeO/mpiTTgmBsTP0WL6yeuLfLs/Ctf0pI/ixGqIRm8sDCwMcXGe9WWvt2sGXI5mNqZbValmJg==", - "dev": true, - "engines": { - "node": ">=12" - } - }, "node_modules/tmp": { "version": "0.0.33", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", @@ -9114,6 +9105,15 @@ "node": ">=8.0" } }, + "node_modules/toad-cache": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/toad-cache/-/toad-cache-3.2.0.tgz", + "integrity": "sha512-Hj5zSqBS6OHbZoQk9IU8VqIr+0JUpwzunnwSlFJhG8aJSInYUMEuzItl3kJsGteTPd1qtflafdRHlRtUazYeqg==", + "dev": true, + "engines": { + "node": ">=12" + } + }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", @@ -9733,9 +9733,9 @@ "dev": true }, "@athenna/common": { - "version": "4.10.1", - "resolved": "https://registry.npmjs.org/@athenna/common/-/common-4.10.1.tgz", - "integrity": "sha512-EVzdlfxP8dQVV5dSIQzUHXMYhby+TCxkGRtyz99nXovlkTOx//KOKE3NT6WXIZUPpAfARwQSQNfD1wEgUAmZMg==", + "version": "4.13.1", + "resolved": "https://registry.npmjs.org/@athenna/common/-/common-4.13.1.tgz", + "integrity": "sha512-9Dq7azVqW2qAjxO/T6RARaz8VXZ4U4bXAiaNVkxtpgB0+Gr8pbKmNnRqJzMgAYhD/Bxx2WWqgcYjZEfRKgnYqA==", "dev": true, "requires": { "@fastify/formbody": "^7.4.0", @@ -9744,7 +9744,7 @@ "chalk": "^5.3.0", "change-case": "^4.1.2", "collect.js": "^4.36.1", - "fastify": "^4.21.0", + "fastify": "^4.22.1", "got": "^12.6.1", "http-status-codes": "^2.2.0", "is-wsl": "^2.2.0", @@ -12861,9 +12861,9 @@ } }, "fast-content-type-parse": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fast-content-type-parse/-/fast-content-type-parse-1.0.0.tgz", - "integrity": "sha512-Xbc4XcysUXcsP5aHUU7Nq3OwvHq97C+WnbkeIefpeYLX+ryzFJlU6OStFJhs6Ol0LkUGpcK+wL0JwfM+FCU5IA==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fast-content-type-parse/-/fast-content-type-parse-1.1.0.tgz", + "integrity": "sha512-fBHHqSTFLVnR61C+gltJuE5GkVQMV0S2nqUO8TJ+5Z3qAKG8vAx4FKai1s5jq/inV1+sREynIWSuQ6HgoSXpDQ==", "dev": true }, "fast-decode-uri-component": { @@ -12976,9 +12976,9 @@ "dev": true }, "fastify": { - "version": "4.21.0", - "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.21.0.tgz", - "integrity": "sha512-tsu4bcwE4HetxqW8prA5fbC9bKHMYDp7jGEDWyzK1l90a3uOaLoIcQbdGcWeODNLVJviQnzh1wvIjTZE3MJFEg==", + "version": "4.23.2", + "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.23.2.tgz", + "integrity": "sha512-WFSxsHES115svC7NrerNqZwwM0UOxbC/P6toT9LRHgAAFvG7o2AN5W+H4ihCtOGuYXjZf4z+2jXC89rVEoPWOA==", "dev": true, "requires": { "@fastify/ajv-compiler": "^3.5.0", @@ -12996,7 +12996,7 @@ "rfdc": "^1.3.0", "secure-json-parse": "^2.5.0", "semver": "^7.5.0", - "tiny-lru": "^11.0.1" + "toad-cache": "^3.2.0" }, "dependencies": { "semver": { @@ -14480,9 +14480,9 @@ } }, "light-my-request": { - "version": "5.10.0", - "resolved": "https://registry.npmjs.org/light-my-request/-/light-my-request-5.10.0.tgz", - "integrity": "sha512-ZU2D9GmAcOUculTTdH9/zryej6n8TzT+fNGdNtm6SDp5MMMpHrJJkvAdE3c6d8d2chE9i+a//dS9CWZtisknqA==", + "version": "5.11.0", + "resolved": "https://registry.npmjs.org/light-my-request/-/light-my-request-5.11.0.tgz", + "integrity": "sha512-qkFCeloXCOMpmEdZ/MV91P8AT4fjwFXWaAFz3lUeStM8RcoM1ks4J/F8r1b3r6y/H4u3ACEJ1T+Gv5bopj7oDA==", "dev": true, "requires": { "cookie": "^0.5.0", @@ -15449,15 +15449,15 @@ "dev": true }, "pino": { - "version": "8.15.0", - "resolved": "https://registry.npmjs.org/pino/-/pino-8.15.0.tgz", - "integrity": "sha512-olUADJByk4twxccmAxb1RiGKOSvddHugCV3wkqjyv+3Sooa2KLrmXrKEWOKi0XPCLasRR5jBXxioE1jxUa4KzQ==", + "version": "8.15.1", + "resolved": "https://registry.npmjs.org/pino/-/pino-8.15.1.tgz", + "integrity": "sha512-Cp4QzUQrvWCRJaQ8Lzv0mJzXVk4z2jlq8JNKMGaixC2Pz5L4l2p95TkuRvYbrEbe85NQsDKrAd4zalf7Ml6WiA==", "dev": true, "requires": { "atomic-sleep": "^1.0.0", "fast-redact": "^3.1.1", "on-exit-leak-free": "^2.1.0", - "pino-abstract-transport": "v1.0.0", + "pino-abstract-transport": "v1.1.0", "pino-std-serializers": "^6.0.0", "process-warning": "^2.0.0", "quick-format-unescaped": "^4.0.3", @@ -15468,9 +15468,9 @@ } }, "pino-abstract-transport": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/pino-abstract-transport/-/pino-abstract-transport-1.0.0.tgz", - "integrity": "sha512-c7vo5OpW4wIS42hUVcT5REsL8ZljsUfBjqV/e2sFxmFEFZiq1XLUp5EYLtuDH6PEHq9W1egWqRbnLUP5FuZmOA==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pino-abstract-transport/-/pino-abstract-transport-1.1.0.tgz", + "integrity": "sha512-lsleG3/2a/JIWUtf9Q5gUNErBqwIu1tUKTT3dUzaf5DySw9ra1wcqKjJjLX1VTY64Wk1eEOYsVGSaGfCK85ekA==", "dev": true, "requires": { "readable-stream": "^4.0.0", @@ -16481,9 +16481,9 @@ "dev": true }, "thread-stream": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-2.3.0.tgz", - "integrity": "sha512-kaDqm1DET9pp3NXwR8382WHbnpXnRkN9xGN9dQt3B2+dmXiW8X1SOwmFOxAErEQ47ObhZ96J6yhZNXuyCOL7KA==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-2.4.0.tgz", + "integrity": "sha512-xZYtOtmnA63zj04Q+F9bdEay5r47bvpo1CaNqsKi7TpoJHcotUez8Fkfo2RJWpW91lnnaApdpRbVwCWsy+ifcw==", "dev": true, "requires": { "real-require": "^0.2.0" @@ -16504,12 +16504,6 @@ "convert-hrtime": "^3.0.0" } }, - "tiny-lru": { - "version": "11.0.1", - "resolved": "https://registry.npmjs.org/tiny-lru/-/tiny-lru-11.0.1.tgz", - "integrity": "sha512-iNgFugVuQgBKrqeO/mpiTTgmBsTP0WL6yeuLfLs/Ctf0pI/ixGqIRm8sDCwMcXGe9WWvt2sGXI5mNqZbValmJg==", - "dev": true - }, "tmp": { "version": "0.0.33", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", @@ -16534,6 +16528,12 @@ "is-number": "^7.0.0" } }, + "toad-cache": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/toad-cache/-/toad-cache-3.2.0.tgz", + "integrity": "sha512-Hj5zSqBS6OHbZoQk9IU8VqIr+0JUpwzunnwSlFJhG8aJSInYUMEuzItl3kJsGteTPd1qtflafdRHlRtUazYeqg==", + "dev": true + }, "tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", diff --git a/package.json b/package.json index b214b23..be8d172 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/logger", - "version": "4.4.0", + "version": "4.5.0", "description": "The Athenna logging solution. Log in stdout, files and buckets.", "license": "MIT", "author": "João Lenon ", @@ -59,7 +59,7 @@ "cls-rtracer": "^2.6.3" }, "devDependencies": { - "@athenna/common": "^4.10.1", + "@athenna/common": "^4.13.1", "@athenna/config": "^4.4.0", "@athenna/ioc": "^4.3.0", "@athenna/test": "^4.7.0", diff --git a/src/formatters/RequestFormatter.ts b/src/formatters/RequestFormatter.ts index ebba3a9..b0b5987 100644 --- a/src/formatters/RequestFormatter.ts +++ b/src/formatters/RequestFormatter.ts @@ -16,26 +16,22 @@ export class RequestFormatter extends Formatter { return ctx } - const ip = ctx.request.ip - const status = ctx.status const responseTimeMs = `${Math.round(ctx.responseTime)}ms` - const methodAndStatus = Color[ctx.request.method]( - `[${ctx.request.method}::${ctx.status}]` - ) + const status = Color.statusCode(ctx.status) + const method = Color.httpMethod(ctx.request.method) + const date = new Date().toISOString() if (!this.configs.asJson) { return this.clean( - `${methodAndStatus} - [${ip}] - ${new Date().toISOString()} - ${ - ctx.request.baseUrl - } ${responseTimeMs}` + `${method}${status} ${ctx.request.baseUrl} - ${date} - ${responseTimeMs}` ) } const metadata = { method: ctx.request.method, duration: responseTimeMs, - status: status <= 399 ? 'SUCCESS' : 'ERROR', - statusCode: status, + status: ctx.status <= 399 ? 'SUCCESS' : 'ERROR', + statusCode: ctx.status, url: ctx.request.hostUrl, path: ctx.request.baseUrl, createdAt: Date.now(), diff --git a/tests/unit/formatters/RequestFormatterTest.ts b/tests/unit/formatters/RequestFormatterTest.ts index 03db586..a0b2fee 100644 --- a/tests/unit/formatters/RequestFormatterTest.ts +++ b/tests/unit/formatters/RequestFormatterTest.ts @@ -41,8 +41,7 @@ export default class RequestFormatterTest { const message = formatter.format(ctx) - assert.isTrue(message.includes('127.0.0.1')) - assert.isTrue(message.includes('[GET::200]')) + assert.isTrue(message.includes('GET')) assert.isTrue(message.includes('http://localhost:1335/:id')) assert.isTrue(message.includes('ms')) }