Skip to content

Commit

Permalink
Merge pull request #12 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat: add request channel/formatter
  • Loading branch information
jlenon7 authored Apr 7, 2022
2 parents cdcf259 + 7bb207f commit 49bc7bc
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "1.1.2",
"version": "1.1.3",
"description": "",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -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"
Expand Down
47 changes: 47 additions & 0 deletions src/Formatters/RequestFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @athenna/logger
*
* (c) João Lenon <lenon@athenna.io>
*
* 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,
})
}
}
42 changes: 27 additions & 15 deletions src/Utils/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}
10 changes: 9 additions & 1 deletion tests/Stubs/config/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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".
|
*/

Expand All @@ -38,6 +38,14 @@ export default {
context: 'Logger',
},
},
request: {
driver: 'console',
formatter: 'request',
streamType: 'stdout',
formatterConfig: {
method: 'GET',
},
},
debug: {
driver: 'debug',
formatter: 'nest',
Expand Down

0 comments on commit 49bc7bc

Please sign in to comment.