diff --git a/package.json b/package.json index d67fad3..dda60f6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/logger", - "version": "1.1.2", + "version": "1.1.3", "description": "", "license": "MIT", "author": "João Lenon ", @@ -153,8 +153,8 @@ } }, "dependencies": { - "@athenna/ioc": "1.1.1", - "@secjs/utils": "1.8.2", + "@athenna/ioc": "1.1.2", + "@secjs/utils": "1.8.3", "chalk": "4.1.1", "reflect-metadata": "0.1.13", "tscpaths": "0.0.9" diff --git a/src/Formatters/RequestFormatter.ts b/src/Formatters/RequestFormatter.ts new file mode 100644 index 0000000..6a544b0 --- /dev/null +++ b/src/Formatters/RequestFormatter.ts @@ -0,0 +1,47 @@ +/** + * @athenna/logger + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { Chalk } from 'chalk' +import { Color } from 'src/Utils/Color' +import { FormatterContract } from 'src/Contracts/FormatterContract' + +export interface RequestFormatterOptions { + chalk: Chalk + asJson: boolean +} + +export class RequestFormatter implements FormatterContract { + format(ctx: any, options: RequestFormatterOptions): string { + const ip = ctx.request.ip + const status = ctx.status + const baseUrl = ctx.request.baseUrl + const method = Color[ctx.request.method] + const responseTimeMs = `${ctx.responseTime}ms` + + if (!options.asJson) { + return `(${ip}) - [${status}] ${method}::${baseUrl} ${responseTimeMs}` + } + + const metadata = { + method: ctx.request.method, + duration: responseTimeMs, + status: status <= 399 ? 'SUCCESS' : 'ERROR', + statusCode: status, + url: ctx.request.hostUrl, + path: ctx.request.baseUrl, + createdAt: Date.now(), + } + + return JSON.stringify({ + request: JSON.stringify(ctx.request), + response: JSON.stringify(ctx.response), + metadata, + }) + } +} diff --git a/src/Utils/Color.ts b/src/Utils/Color.ts index b9e2cc9..3948a66 100644 --- a/src/Utils/Color.ts +++ b/src/Utils/Color.ts @@ -54,14 +54,6 @@ export class Color { return Color.chalk.hex('#f10e0e') } - static removeColors(string: string): any { - return Color.chalk.reset(string).replace( - // eslint-disable-next-line no-control-regex - /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, - '', - ) - } - static get info(): any { return this.cyan.bold } @@ -82,23 +74,43 @@ export class Color { return this.orange.bold } - static httpMethod(method: Methods): any { - return this[method] as Chalk + static get GET(): any { + return this.purple.bold('GET') } - static get GET(): any { - return this.purple.bold('GET 🔍') + static get HEAD(): any { + return this.cyan.bold('HEAD') } static get PUT(): any { - return this.yellow.bold('PUT 🛠') + return this.orange.bold('PUT') + } + + static get PATCH(): any { + return this.yellow.bold('PATCH') } static get POST(): any { - return this.green.bold('POST 🧱') + return this.green.bold('POST') } static get DELETE(): any { - return this.red.bold('DELETE ❌') + return this.red.bold('DELETE') + } + + static get OPTIONS(): any { + return this.cyan.bold('OPTIONS') + } + + static removeColors(string: string): any { + return Color.chalk.reset(string).replace( + // eslint-disable-next-line no-control-regex + /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, + '', + ) + } + + static httpMethod(method: Methods): any { + return this[method] as Chalk } } diff --git a/tests/Stubs/config/logging.ts b/tests/Stubs/config/logging.ts index 1741916..2ca49f0 100644 --- a/tests/Stubs/config/logging.ts +++ b/tests/Stubs/config/logging.ts @@ -23,7 +23,7 @@ export default { | Here you may configure the log channels for your application. | | Available Drivers: "console", "debug", "file". - | Available Formatters: "cli", "simple", "nest", "json". + | Available Formatters: "cli", "simple", "nest", "json", "request". | */ @@ -38,6 +38,14 @@ export default { context: 'Logger', }, }, + request: { + driver: 'console', + formatter: 'request', + streamType: 'stdout', + formatterConfig: { + method: 'GET', + }, + }, debug: { driver: 'debug', formatter: 'nest',