@athenna/logger 1.1.1 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/logger",
3
- "version": "1.1.1",
3
+ "version": "1.1.4",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -153,8 +153,8 @@
153
153
  }
154
154
  },
155
155
  "dependencies": {
156
- "@athenna/ioc": "1.0.9",
157
- "@secjs/utils": "1.8.0",
156
+ "@athenna/ioc": "1.1.2",
157
+ "@secjs/utils": "1.8.3",
158
158
  "chalk": "4.1.1",
159
159
  "reflect-metadata": "0.1.13",
160
160
  "tscpaths": "0.0.9"
@@ -9,12 +9,13 @@
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.FormatterFactory = void 0;
12
+ const CliFormatter_1 = require("../Formatters/CliFormatter");
12
13
  const JsonFormatter_1 = require("../Formatters/JsonFormatter");
13
14
  const NestFormatter_1 = require("../Formatters/NestFormatter");
14
15
  const SimpleFormatter_1 = require("../Formatters/SimpleFormatter");
16
+ const RequestFormatter_1 = require("../Formatters/RequestFormatter");
15
17
  const NotFoundFormatterException_1 = require("../Exceptions/NotFoundFormatterException");
16
18
  const FormatterAlreadyExistException_1 = require("../Exceptions/FormatterAlreadyExistException");
17
- const CliFormatter_1 = require("../Formatters/CliFormatter");
18
19
  class FormatterFactory {
19
20
  static availableFormatters() {
20
21
  const availableFormatters = [];
@@ -42,4 +43,5 @@ FormatterFactory.formatters = new Map()
42
43
  .set('cli', { Formatter: CliFormatter_1.CliFormatter })
43
44
  .set('nest', { Formatter: NestFormatter_1.NestFormatter })
44
45
  .set('json', { Formatter: JsonFormatter_1.JsonFormatter })
45
- .set('simple', { Formatter: SimpleFormatter_1.SimpleFormatter });
46
+ .set('simple', { Formatter: SimpleFormatter_1.SimpleFormatter })
47
+ .set('request', { Formatter: RequestFormatter_1.RequestFormatter });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @athenna/logger
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { Chalk } from 'chalk';
10
+ import { FormatterContract } from '../Contracts/FormatterContract';
11
+ export interface RequestFormatterOptions {
12
+ chalk: Chalk;
13
+ asJson: boolean;
14
+ }
15
+ export declare class RequestFormatter implements FormatterContract {
16
+ format(ctx: any, options: RequestFormatterOptions): string;
17
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ /**
3
+ * @athenna/logger
4
+ *
5
+ * (c) João Lenon <lenon@athenna.io>
6
+ *
7
+ * For the full copyright and license information, please view the LICENSE
8
+ * file that was distributed with this source code.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.RequestFormatter = void 0;
12
+ const Color_1 = require("../Utils/Color");
13
+ class RequestFormatter {
14
+ format(ctx, options) {
15
+ const ip = ctx.request.ip;
16
+ const status = ctx.status;
17
+ const responseTimeMs = `${Math.round(ctx.responseTime)}ms`;
18
+ const methodAndUrl = Color_1.Color[ctx.request.method](`${ctx.request.method}::${ctx.request.baseUrl}`);
19
+ if (!options.asJson) {
20
+ return `(${ip}) - [${status}] ${methodAndUrl} ${responseTimeMs}`;
21
+ }
22
+ const metadata = {
23
+ method: ctx.request.method,
24
+ duration: responseTimeMs,
25
+ status: status <= 399 ? 'SUCCESS' : 'ERROR',
26
+ statusCode: status,
27
+ url: ctx.request.hostUrl,
28
+ path: ctx.request.baseUrl,
29
+ createdAt: Date.now(),
30
+ data: ctx.data,
31
+ };
32
+ const request = {
33
+ url: ctx.request.hostUrl,
34
+ method: ctx.request.method,
35
+ body: ctx.request.body,
36
+ params: ctx.request.params,
37
+ queries: ctx.request.queries,
38
+ headers: ctx.request.headers,
39
+ };
40
+ const response = {
41
+ body: ctx.body,
42
+ headers: ctx.headers,
43
+ };
44
+ return JSON.stringify({ request, response, metadata });
45
+ }
46
+ }
47
+ exports.RequestFormatter = RequestFormatter;
package/src/Logger.js CHANGED
@@ -5,13 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Logger = void 0;
7
7
  const chalk_1 = __importDefault(require("chalk"));
8
- const Color_1 = require("./Utils/Color");
9
8
  const utils_1 = require("@secjs/utils");
9
+ const Color_1 = require("./Utils/Color");
10
10
  const DriverFactory_1 = require("./Factories/DriverFactory");
11
11
  const FormatterFactory_1 = require("./Factories/FormatterFactory");
12
12
  class Logger {
13
13
  constructor(runtimeConfig = {}) {
14
- new utils_1.Config().safeLoad(utils_1.Path.config('logging'));
15
14
  this.runtimeConfig = runtimeConfig;
16
15
  this.channelName = 'default';
17
16
  this.driver = DriverFactory_1.DriverFactory.fabricate(this.channelName, this.runtimeConfig);
@@ -30,16 +30,19 @@ export declare class Color {
30
30
  static get green(): Chalk;
31
31
  static get darkGreen(): Chalk;
32
32
  static get red(): Chalk;
33
- static removeColors(string: string): any;
34
33
  static get info(): any;
35
34
  static get log(): any;
36
35
  static get debug(): any;
37
36
  static get error(): any;
38
37
  static get warning(): any;
39
- static httpMethod(method: Methods): any;
40
38
  static get GET(): any;
39
+ static get HEAD(): any;
41
40
  static get PUT(): any;
41
+ static get PATCH(): any;
42
42
  static get POST(): any;
43
43
  static get DELETE(): any;
44
+ static get OPTIONS(): any;
45
+ static removeColors(string: string): any;
46
+ static httpMethod(method: Methods): any;
44
47
  }
45
48
  export {};
@@ -44,11 +44,6 @@ class Color {
44
44
  static get red() {
45
45
  return Color.chalk.hex('#f10e0e');
46
46
  }
47
- static removeColors(string) {
48
- return Color.chalk.reset(string).replace(
49
- // eslint-disable-next-line no-control-regex
50
- /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
51
- }
52
47
  static get info() {
53
48
  return this.cyan.bold;
54
49
  }
@@ -64,20 +59,34 @@ class Color {
64
59
  static get warning() {
65
60
  return this.orange.bold;
66
61
  }
67
- static httpMethod(method) {
68
- return this[method];
69
- }
70
62
  static get GET() {
71
- return this.purple.bold('GET 🔍');
63
+ return this.purple.bold;
64
+ }
65
+ static get HEAD() {
66
+ return this.cyan.bold;
72
67
  }
73
68
  static get PUT() {
74
- return this.yellow.bold('PUT 🛠');
69
+ return this.orange.bold;
70
+ }
71
+ static get PATCH() {
72
+ return this.yellow.bold;
75
73
  }
76
74
  static get POST() {
77
- return this.green.bold('POST 🧱');
75
+ return this.green.bold;
78
76
  }
79
77
  static get DELETE() {
80
- return this.red.bold('DELETE ❌');
78
+ return this.red.bold;
79
+ }
80
+ static get OPTIONS() {
81
+ return this.cyan.bold;
82
+ }
83
+ static removeColors(string) {
84
+ return Color.chalk.reset(string).replace(
85
+ // eslint-disable-next-line no-control-regex
86
+ /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
87
+ }
88
+ static httpMethod(method) {
89
+ return this[method];
81
90
  }
82
91
  }
83
92
  exports.Color = Color;