@odg/json-log 0.0.1
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/README.md +181 -0
- package/dist/Interfaces/LoggerObjectInterface.d.ts +27 -0
- package/dist/Interfaces/LoggerObjectInterface.js +3 -0
- package/dist/Interfaces/LoggerObjectInterface.js.map +1 -0
- package/dist/Interfaces/index.d.ts +1 -0
- package/dist/Interfaces/index.js +18 -0
- package/dist/Interfaces/index.js.map +1 -0
- package/dist/example.d.ts +1 -0
- package/dist/example.js +8 -0
- package/dist/example.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/JSONLoggerPlugin.d.ts +113 -0
- package/dist/lib/JSONLoggerPlugin.js +215 -0
- package/dist/lib/JSONLoggerPlugin.js.map +1 -0
- package/dist/lib/ParserException.d.ts +3 -0
- package/dist/lib/ParserException.js +8 -0
- package/dist/lib/ParserException.js.map +1 -0
- package/dist/logs/JSONLoggerPlugin.d.ts +113 -0
- package/dist/logs/JSONLoggerPlugin.js +215 -0
- package/dist/logs/JSONLoggerPlugin.js.map +1 -0
- package/dist/logs/ParserException.d.ts +3 -0
- package/dist/logs/ParserException.js +8 -0
- package/dist/logs/ParserException.js.map +1 -0
- package/dist/package/JSONLoggerPlugin.d.ts +113 -0
- package/dist/package/JSONLoggerPlugin.js +221 -0
- package/dist/package/JSONLoggerPlugin.js.map +1 -0
- package/dist/package/JSONParserException.d.ts +3 -0
- package/dist/package/JSONParserException.js +8 -0
- package/dist/package/JSONParserException.js.map +1 -0
- package/dist/package/ParserException.d.ts +3 -0
- package/dist/package/ParserException.js +8 -0
- package/dist/package/ParserException.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +74 -0
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<a href="https://github.com/ODGodinho">
|
|
3
|
+
<img
|
|
4
|
+
src="https://raw.githubusercontent.com/ODGodinho/Stanley-TheTemplate/main/public/images/Stanley.jpg"
|
|
5
|
+
alt="Stanley Imagem" width="500"
|
|
6
|
+
/>
|
|
7
|
+
</a>
|
|
8
|
+
<br />
|
|
9
|
+
Parse your log in JSON formatter
|
|
10
|
+
<br />
|
|
11
|
+
</h1>
|
|
12
|
+
|
|
13
|
+
<h4 align="center">Using @ODG/Log parser logs message ๐ฆ!</h4>
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
|
|
17
|
+
[](https://codecov.io/gh/ODGodinho/ODG-JSONLog)
|
|
18
|
+
[](https://github.com/ODGodinho/ODG-JSONLog/stargazers)
|
|
19
|
+
[](https://www.linkedin.com/in/victor-alves-odgodinho/)
|
|
20
|
+
[](https://github.com/ODGodinho/ODG-JSONLog/network/members)
|
|
21
|
+

|
|
22
|
+
[](https://github.com/ODGodinho/ODG-JSONLog/commits/master)
|
|
23
|
+
[](https://opensource.org/licenses/MIT)
|
|
24
|
+
[](https://github.styleci.io/repos/562306382?branch=main)
|
|
25
|
+
|
|
26
|
+
</p>
|
|
27
|
+
|
|
28
|
+
# Table of Contents
|
|
29
|
+
|
|
30
|
+
- [๐ Benefits](#-benefits)
|
|
31
|
+
- [๐ Libraries](#-libraries)
|
|
32
|
+
- [๐ Dependencies](#-dependencies)
|
|
33
|
+
- [โฉ Get Started](#-get-started)
|
|
34
|
+
- [๐ Use Plugin](#-use-plugin)
|
|
35
|
+
- [๐ฒ Implementation](#-implementation)
|
|
36
|
+
- [๐ SendLog](#-send-log)
|
|
37
|
+
- [๐ป Prepare to develop](#-prepare-to-develop)
|
|
38
|
+
- [๐ Start Project](#-start-project)
|
|
39
|
+
- [๐จ Build and Run](#-build-and-run)
|
|
40
|
+
- [๐งช Teste Code](#-teste-code)
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## ๐ Benefits
|
|
45
|
+
|
|
46
|
+
- ๐ Speed start new project or package using typescript
|
|
47
|
+
- ๐จ Over 800 rules for pattern, possible errors and errors in Linter
|
|
48
|
+
- ๐ Log Pattern default format
|
|
49
|
+
|
|
50
|
+
## ๐ Libraries
|
|
51
|
+
|
|
52
|
+
- [Node.js 16](https://nodejs.org/?n=dragonsgamers)
|
|
53
|
+
- [Typescript](https://www.typescriptlang.org/?n=dragonsgamers)
|
|
54
|
+
- [Eslint](https://eslint.org/?n=dragonsgamers)
|
|
55
|
+
- [ODG-Linter-JS](https://github.com/ODGodinho/ODG-Linter-Js?n=dragonsgamers)
|
|
56
|
+
- [EditorConfig](https://editorconfig.org/?n=dragonsgamers)
|
|
57
|
+
- [ReviewDog](https://github.com/reviewdog/action-eslint)
|
|
58
|
+
|
|
59
|
+
## ๐ Dependencies
|
|
60
|
+
|
|
61
|
+
- [Node.js](https://nodejs.org) 16 or later
|
|
62
|
+
- [Yarn](https://yarnpkg.com/) Optional/Recommended
|
|
63
|
+
- [ODG TsConfig](https://github.com/ODGodinho/tsconfig) Last Version
|
|
64
|
+
- [error-stack-parser](https://www.npmjs.com/package/error-stack-parser) Last Version
|
|
65
|
+
|
|
66
|
+
## โฉ Get Started
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
### ๐ Use Plugin
|
|
71
|
+
|
|
72
|
+
install this plugin with
|
|
73
|
+
|
|
74
|
+
```powershell
|
|
75
|
+
yarn add @odg/json-log
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### ๐ฒ Implementation
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const logger = new ConsoleLogger(); // Or Other Class Logger
|
|
82
|
+
const plugin = new JSONLoggerPlugin(
|
|
83
|
+
"appName",
|
|
84
|
+
);
|
|
85
|
+
logger.use(plugin);
|
|
86
|
+
|
|
87
|
+
plugin.setIdentifier(randomUUID());
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### ๐ Send Log
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
try {
|
|
94
|
+
throw new Exception("Example");
|
|
95
|
+
} catch (error) {
|
|
96
|
+
await logger.error(error);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
LOG JSON Return
|
|
101
|
+
|
|
102
|
+
```jsonc
|
|
103
|
+
{
|
|
104
|
+
"type": "error",
|
|
105
|
+
"index": "appName",
|
|
106
|
+
"instance": "ContainerID",
|
|
107
|
+
"identifier": "00000000-0000-0000-0000-00000", // use setIdentifier in plugin to change log group
|
|
108
|
+
"gitRelease": "v1.0.0",
|
|
109
|
+
"gitBranch": "main",
|
|
110
|
+
"exception": {
|
|
111
|
+
"type": "string", // ExceptionClassName
|
|
112
|
+
"message": "string", // Example
|
|
113
|
+
"fileException": "string", // index.js
|
|
114
|
+
"functionName": null, // OPTIONAL:
|
|
115
|
+
"fileLine": "number", // 1
|
|
116
|
+
"fileColumn": "number", // 1
|
|
117
|
+
"stack": "stack",
|
|
118
|
+
},
|
|
119
|
+
"exceptionPreview": null, // OPTIONAL: This is preview second property on new Exceptions("Example", error);
|
|
120
|
+
"request": {
|
|
121
|
+
"url": "string",
|
|
122
|
+
"baseURL": "string",
|
|
123
|
+
"method": "Methods | string",
|
|
124
|
+
"headers": "HttpHeadersInterface",
|
|
125
|
+
"params": "ParametersInterface",
|
|
126
|
+
"data": "RequestData",
|
|
127
|
+
"timeout": "number",
|
|
128
|
+
"responseType": "ResponseType",
|
|
129
|
+
"maxContentLength": "number",
|
|
130
|
+
"validateStatus": "((status: number) => boolean) | null",
|
|
131
|
+
"maxBodyLength": "number",
|
|
132
|
+
"maxRedirects": "number",
|
|
133
|
+
"socketPath": "string | null",
|
|
134
|
+
"proxy": "ProxyConfigInterface | false",
|
|
135
|
+
"response": { // OPTIONAL: This is response from axios
|
|
136
|
+
"data": "any",
|
|
137
|
+
"status": "number",
|
|
138
|
+
"headers": {
|
|
139
|
+
"string": "string"
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
"createdAt": "Date",
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### ๐ป Prepare To Develop
|
|
148
|
+
|
|
149
|
+
Copy `.env.example` to `.env` and add the values according to your needs.
|
|
150
|
+
|
|
151
|
+
### ๐ Start Project
|
|
152
|
+
|
|
153
|
+
First install dependencies with the following command
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
yarn install
|
|
157
|
+
# or
|
|
158
|
+
npm install
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## ๐จ Build and Run
|
|
162
|
+
|
|
163
|
+
To build the project, you can use the following command
|
|
164
|
+
|
|
165
|
+
> if you change files, you need to run `yarn build` and `yarn start` again
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
yarn build && yarn start
|
|
169
|
+
# or
|
|
170
|
+
yarn dev
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## ๐งช Teste Code
|
|
174
|
+
|
|
175
|
+
To Test execute this command
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
yarn test
|
|
179
|
+
# or
|
|
180
|
+
yarn test:watch
|
|
181
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type LogLevel } from "@odg/log";
|
|
2
|
+
import { type RequestInterface } from "@odg/message";
|
|
3
|
+
import { type ResponseInterface } from "@odg/message/dist/interfaces/response";
|
|
4
|
+
export interface ExceptionObjectLoggerInterface {
|
|
5
|
+
"type": string;
|
|
6
|
+
"message": string;
|
|
7
|
+
"fileException"?: string;
|
|
8
|
+
"functionName"?: string;
|
|
9
|
+
"fileLine"?: number;
|
|
10
|
+
"fileColumn"?: number;
|
|
11
|
+
"tracker"?: string;
|
|
12
|
+
}
|
|
13
|
+
export type LoggerObjectRequestInterface = RequestInterface<unknown> & {
|
|
14
|
+
response?: Omit<ResponseInterface<unknown, unknown>, "request">;
|
|
15
|
+
};
|
|
16
|
+
export interface LoggerObjectInterface {
|
|
17
|
+
"type": LogLevel;
|
|
18
|
+
"index": string;
|
|
19
|
+
"instance": string;
|
|
20
|
+
"identifier"?: string;
|
|
21
|
+
"gitRelease"?: string;
|
|
22
|
+
"gitBranch"?: string;
|
|
23
|
+
"exception"?: ExceptionObjectLoggerInterface;
|
|
24
|
+
"exceptionPreview"?: ExceptionObjectLoggerInterface[];
|
|
25
|
+
"request"?: LoggerObjectRequestInterface;
|
|
26
|
+
"createdAt": Date;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LoggerObjectInterface.js","sourceRoot":"","sources":["../../src/Interfaces/LoggerObjectInterface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./LoggerObjectInterface";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./LoggerObjectInterface"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/Interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0DAAwC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function exampleStanleyTemplate(): string;
|
package/dist/example.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exampleStanleyTemplate = void 0;
|
|
4
|
+
function exampleStanleyTemplate() {
|
|
5
|
+
return "Stanley The Template";
|
|
6
|
+
}
|
|
7
|
+
exports.exampleStanleyTemplate = exampleStanleyTemplate;
|
|
8
|
+
//# sourceMappingURL=example.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"example.js","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":";;;AAAA,SAAgB,sBAAsB;IAClC,OAAO,sBAAsB,CAAC;AAClC,CAAC;AAFD,wDAEC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Interfaces";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./Interfaces"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type LogLevel, type LoggerParserInterface, type LoggerPluginInterface } from "@odg/log";
|
|
2
|
+
import { type ResponseInterface, type RequestInterface } from "@odg/message";
|
|
3
|
+
import { type LoggerObjectRequestInterface, type ExceptionObjectLoggerInterface, type LoggerObjectInterface } from "../Interfaces";
|
|
4
|
+
export declare class JSONLoggerPlugin implements LoggerPluginInterface {
|
|
5
|
+
protected readonly appName: string;
|
|
6
|
+
protected readonly instanceId?: string | undefined;
|
|
7
|
+
protected readonly maxExceptionPreview: number;
|
|
8
|
+
/**
|
|
9
|
+
* Identifier for the current log
|
|
10
|
+
*
|
|
11
|
+
* @type {string}
|
|
12
|
+
*/
|
|
13
|
+
protected identifier?: string;
|
|
14
|
+
constructor(appName: string, instanceId?: string | undefined, maxExceptionPreview?: number);
|
|
15
|
+
/**
|
|
16
|
+
* Plugin parser Function return new message with JSON format
|
|
17
|
+
*
|
|
18
|
+
* @param {LoggerParserInterface} data Received Data params
|
|
19
|
+
* @returns {Promise<LoggerParserInterface>}
|
|
20
|
+
*/
|
|
21
|
+
parser(data: LoggerParserInterface): Promise<LoggerParserInterface>;
|
|
22
|
+
logJSON(level: LogLevel, message: unknown): Promise<LoggerObjectInterface>;
|
|
23
|
+
/**
|
|
24
|
+
* Define a unique identifier for the current request, for
|
|
25
|
+
* Example: Request ID, Transaction ID, Crawler Process, etc
|
|
26
|
+
*
|
|
27
|
+
* @param {string} identifier Unique identifier
|
|
28
|
+
* @memberof JSONLoggerPlugin
|
|
29
|
+
*/
|
|
30
|
+
setIdentifier(identifier: string): void;
|
|
31
|
+
/**
|
|
32
|
+
* Return instance HostName, identifier
|
|
33
|
+
*
|
|
34
|
+
* @returns {string}
|
|
35
|
+
*/
|
|
36
|
+
protected getInstance(): string;
|
|
37
|
+
/**
|
|
38
|
+
* Return git tag name
|
|
39
|
+
*
|
|
40
|
+
* @returns {Promise<string>}
|
|
41
|
+
*/
|
|
42
|
+
protected getGitRelease(): Promise<string>;
|
|
43
|
+
/**
|
|
44
|
+
* Return git tag name
|
|
45
|
+
*
|
|
46
|
+
* @returns {Promise<string>}
|
|
47
|
+
*/
|
|
48
|
+
protected getGitBranch(): Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* If Message is a Exception, parse to ExceptionObjectLoggerInterface
|
|
51
|
+
*
|
|
52
|
+
* @param {unknown} exception Possible Exception
|
|
53
|
+
* @returns {Promise<ExceptionObjectLoggerInterface | undefined>}
|
|
54
|
+
*/
|
|
55
|
+
protected parseException(exception: unknown): Promise<ExceptionObjectLoggerInterface | undefined>;
|
|
56
|
+
/**
|
|
57
|
+
* If Message is a Exception, get All Exception Preview and parse to ExceptionObjectLoggerInterface
|
|
58
|
+
*
|
|
59
|
+
* @param {unknown} exception Possible Exception
|
|
60
|
+
* @returns {Promise<ExceptionObjectLoggerInterface | undefined>}
|
|
61
|
+
*/
|
|
62
|
+
protected parseExceptionPreview(exception: unknown): Promise<ExceptionObjectLoggerInterface[] | undefined>;
|
|
63
|
+
/**
|
|
64
|
+
* Parser Request and Response
|
|
65
|
+
*
|
|
66
|
+
* @protected
|
|
67
|
+
* @param {unknown} message Possible Message/Request
|
|
68
|
+
* @returns {Promise<LoggerObjectRequestInterface | undefined>}
|
|
69
|
+
* @memberof JSONLoggerPlugin
|
|
70
|
+
*/
|
|
71
|
+
protected parseRequest(message: unknown): Promise<LoggerObjectRequestInterface | undefined>;
|
|
72
|
+
/**
|
|
73
|
+
* Get Response
|
|
74
|
+
*
|
|
75
|
+
* @protected
|
|
76
|
+
* @param {unknown} message Possible Request/Message
|
|
77
|
+
* @returns {Promise<ResponseInterface<unknown, unknown> | undefined>}
|
|
78
|
+
* @memberof JSONLoggerPlugin
|
|
79
|
+
*/
|
|
80
|
+
protected getResponseMessage(message: unknown): Promise<ResponseInterface<unknown, unknown> | undefined>;
|
|
81
|
+
/**
|
|
82
|
+
* Get Request
|
|
83
|
+
*
|
|
84
|
+
* @protected
|
|
85
|
+
* @param {unknown} message Possible Request/Message
|
|
86
|
+
* @returns {Promise<ResponseInterface<unknown, unknown> | undefined>}
|
|
87
|
+
* @memberof JSONLoggerPlugin
|
|
88
|
+
*/
|
|
89
|
+
protected getRequestMessage(message: unknown): Promise<RequestInterface<unknown> | undefined>;
|
|
90
|
+
/**
|
|
91
|
+
* Check Is Message Response or Request or Exception Message
|
|
92
|
+
*
|
|
93
|
+
* @protected
|
|
94
|
+
* @param {unknown} message Possible Message/Request
|
|
95
|
+
* @returns {Promise<boolean>}
|
|
96
|
+
* @memberof JSONLoggerPlugin
|
|
97
|
+
*/
|
|
98
|
+
protected isRequestOrResponseMessage(message: unknown): Promise<boolean>;
|
|
99
|
+
/**
|
|
100
|
+
* Check is Response Message
|
|
101
|
+
*
|
|
102
|
+
* @param {unknown} message Possible Response/Message
|
|
103
|
+
* @returns {boolean}
|
|
104
|
+
*/
|
|
105
|
+
protected isResponseMessage(message: unknown): message is ResponseInterface<unknown, unknown>;
|
|
106
|
+
/**
|
|
107
|
+
* Check is Request Message
|
|
108
|
+
*
|
|
109
|
+
* @param {unknown} message Possible Request/Message
|
|
110
|
+
* @returns {boolean}
|
|
111
|
+
*/
|
|
112
|
+
protected isRequestMessage(message: unknown): message is RequestInterface<unknown>;
|
|
113
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.JSONLoggerPlugin = void 0;
|
|
7
|
+
const node_child_process_1 = require("node:child_process");
|
|
8
|
+
const node_util_1 = require("node:util");
|
|
9
|
+
const exception_1 = require("@odg/exception");
|
|
10
|
+
const message_1 = require("@odg/message");
|
|
11
|
+
const error_stack_parser_1 = __importDefault(require("error-stack-parser"));
|
|
12
|
+
class JSONLoggerPlugin {
|
|
13
|
+
constructor(appName, instanceId, maxExceptionPreview = 10) {
|
|
14
|
+
this.appName = appName;
|
|
15
|
+
this.instanceId = instanceId;
|
|
16
|
+
this.maxExceptionPreview = maxExceptionPreview;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Plugin parser Function return new message with JSON format
|
|
20
|
+
*
|
|
21
|
+
* @param {LoggerParserInterface} data Received Data params
|
|
22
|
+
* @returns {Promise<LoggerParserInterface>}
|
|
23
|
+
*/
|
|
24
|
+
async parser(data) {
|
|
25
|
+
return {
|
|
26
|
+
...data,
|
|
27
|
+
message: await this.logJSON(data.level, data.message),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async logJSON(level, message) {
|
|
31
|
+
return {
|
|
32
|
+
"type": level,
|
|
33
|
+
"index": this.appName,
|
|
34
|
+
"instance": this.getInstance(),
|
|
35
|
+
"identifier": this.identifier,
|
|
36
|
+
"gitRelease": await this.getGitRelease().catch(() => void 0),
|
|
37
|
+
"gitBranch": await this.getGitBranch().catch(() => void 0),
|
|
38
|
+
"exception": await this.parseException(message),
|
|
39
|
+
"exceptionPreview": await this.parseExceptionPreview(message),
|
|
40
|
+
"request": await this.parseRequest(message),
|
|
41
|
+
"createdAt": new Date(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Define a unique identifier for the current request, for
|
|
46
|
+
* Example: Request ID, Transaction ID, Crawler Process, etc
|
|
47
|
+
*
|
|
48
|
+
* @param {string} identifier Unique identifier
|
|
49
|
+
* @memberof JSONLoggerPlugin
|
|
50
|
+
*/
|
|
51
|
+
setIdentifier(identifier) {
|
|
52
|
+
this.identifier = identifier;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Return instance HostName, identifier
|
|
56
|
+
*
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
getInstance() {
|
|
60
|
+
return this.instanceId
|
|
61
|
+
|| process.env.HOSTNAME
|
|
62
|
+
|| process.env.CONTAINER_ID
|
|
63
|
+
|| "unknown";
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Return git tag name
|
|
67
|
+
*
|
|
68
|
+
* @returns {Promise<string>}
|
|
69
|
+
*/
|
|
70
|
+
async getGitRelease() {
|
|
71
|
+
const command = (0, node_util_1.promisify)(node_child_process_1.exec);
|
|
72
|
+
const gitVersion = await command("git describe --tags --abbrev=41");
|
|
73
|
+
return gitVersion.stdout.trim();
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Return git tag name
|
|
77
|
+
*
|
|
78
|
+
* @returns {Promise<string>}
|
|
79
|
+
*/
|
|
80
|
+
async getGitBranch() {
|
|
81
|
+
const command = (0, node_util_1.promisify)(node_child_process_1.exec);
|
|
82
|
+
const gitVersion = await command("git rev-parse --abbrev-ref HEAD");
|
|
83
|
+
return gitVersion.stdout.trim();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* If Message is a Exception, parse to ExceptionObjectLoggerInterface
|
|
87
|
+
*
|
|
88
|
+
* @param {unknown} exception Possible Exception
|
|
89
|
+
* @returns {Promise<ExceptionObjectLoggerInterface | undefined>}
|
|
90
|
+
*/
|
|
91
|
+
async parseException(exception) {
|
|
92
|
+
var _a, _b, _c, _d;
|
|
93
|
+
if (!(exception instanceof Error))
|
|
94
|
+
return;
|
|
95
|
+
const trace = exception.stack ? error_stack_parser_1.default.parse(exception) : undefined;
|
|
96
|
+
return {
|
|
97
|
+
"type": exception.name,
|
|
98
|
+
"message": exception.message,
|
|
99
|
+
"fileException": (_a = trace === null || trace === void 0 ? void 0 : trace[0]) === null || _a === void 0 ? void 0 : _a.getFileName(),
|
|
100
|
+
"functionName": (_b = trace === null || trace === void 0 ? void 0 : trace[0]) === null || _b === void 0 ? void 0 : _b.getFunctionName(),
|
|
101
|
+
"fileLine": (_c = trace === null || trace === void 0 ? void 0 : trace[0]) === null || _c === void 0 ? void 0 : _c.getLineNumber(),
|
|
102
|
+
"fileColumn": (_d = trace === null || trace === void 0 ? void 0 : trace[0]) === null || _d === void 0 ? void 0 : _d.getColumnNumber(),
|
|
103
|
+
"tracker": exception.stack,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* If Message is a Exception, get All Exception Preview and parse to ExceptionObjectLoggerInterface
|
|
108
|
+
*
|
|
109
|
+
* @param {unknown} exception Possible Exception
|
|
110
|
+
* @returns {Promise<ExceptionObjectLoggerInterface | undefined>}
|
|
111
|
+
*/
|
|
112
|
+
async parseExceptionPreview(exception) {
|
|
113
|
+
if (!(exception instanceof exception_1.Exception))
|
|
114
|
+
return;
|
|
115
|
+
const exceptionCollection = [];
|
|
116
|
+
let exceptionBase = exception.preview;
|
|
117
|
+
let exceptionCount = 0;
|
|
118
|
+
do {
|
|
119
|
+
const parsedException = await this.parseException(exceptionBase);
|
|
120
|
+
if (parsedException)
|
|
121
|
+
exceptionCollection.push(parsedException);
|
|
122
|
+
exceptionBase = exceptionBase === null || exceptionBase === void 0 ? void 0 : exceptionBase.preview;
|
|
123
|
+
} while (exceptionBase && ++exceptionCount < this.maxExceptionPreview);
|
|
124
|
+
return exceptionCollection;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Parser Request and Response
|
|
128
|
+
*
|
|
129
|
+
* @protected
|
|
130
|
+
* @param {unknown} message Possible Message/Request
|
|
131
|
+
* @returns {Promise<LoggerObjectRequestInterface | undefined>}
|
|
132
|
+
* @memberof JSONLoggerPlugin
|
|
133
|
+
*/
|
|
134
|
+
async parseRequest(message) {
|
|
135
|
+
if (!await this.isRequestOrResponseMessage(message))
|
|
136
|
+
return;
|
|
137
|
+
const request = await this.getRequestMessage(message);
|
|
138
|
+
const response = await this.getResponseMessage(message);
|
|
139
|
+
return {
|
|
140
|
+
...request,
|
|
141
|
+
response: response && {
|
|
142
|
+
...Object.fromEntries(Object.entries(response).filter(([key]) => key !== "request")),
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get Response
|
|
148
|
+
*
|
|
149
|
+
* @protected
|
|
150
|
+
* @param {unknown} message Possible Request/Message
|
|
151
|
+
* @returns {Promise<ResponseInterface<unknown, unknown> | undefined>}
|
|
152
|
+
* @memberof JSONLoggerPlugin
|
|
153
|
+
*/
|
|
154
|
+
async getResponseMessage(message) {
|
|
155
|
+
if (message instanceof message_1.MessageException)
|
|
156
|
+
return message.response;
|
|
157
|
+
if (this.isResponseMessage(message))
|
|
158
|
+
return message;
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Get Request
|
|
163
|
+
*
|
|
164
|
+
* @protected
|
|
165
|
+
* @param {unknown} message Possible Request/Message
|
|
166
|
+
* @returns {Promise<ResponseInterface<unknown, unknown> | undefined>}
|
|
167
|
+
* @memberof JSONLoggerPlugin
|
|
168
|
+
*/
|
|
169
|
+
async getRequestMessage(message) {
|
|
170
|
+
if (message instanceof message_1.MessageException)
|
|
171
|
+
return message.request;
|
|
172
|
+
if (this.isRequestMessage(message))
|
|
173
|
+
return message;
|
|
174
|
+
if (this.isResponseMessage(message))
|
|
175
|
+
return message.request;
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Check Is Message Response or Request or Exception Message
|
|
180
|
+
*
|
|
181
|
+
* @protected
|
|
182
|
+
* @param {unknown} message Possible Message/Request
|
|
183
|
+
* @returns {Promise<boolean>}
|
|
184
|
+
* @memberof JSONLoggerPlugin
|
|
185
|
+
*/
|
|
186
|
+
async isRequestOrResponseMessage(message) {
|
|
187
|
+
return message instanceof message_1.MessageException
|
|
188
|
+
|| this.isResponseMessage(message)
|
|
189
|
+
|| this.isRequestMessage(message);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Check is Response Message
|
|
193
|
+
*
|
|
194
|
+
* @param {unknown} message Possible Response/Message
|
|
195
|
+
* @returns {boolean}
|
|
196
|
+
*/
|
|
197
|
+
isResponseMessage(message) {
|
|
198
|
+
return Object.prototype.hasOwnProperty.call(message, "data")
|
|
199
|
+
&& Object.prototype.hasOwnProperty.call(message, "status")
|
|
200
|
+
&& Object.prototype.hasOwnProperty.call(message, "headers")
|
|
201
|
+
&& Object.prototype.hasOwnProperty.call(message, "request");
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Check is Request Message
|
|
205
|
+
*
|
|
206
|
+
* @param {unknown} message Possible Request/Message
|
|
207
|
+
* @returns {boolean}
|
|
208
|
+
*/
|
|
209
|
+
isRequestMessage(message) {
|
|
210
|
+
return Object.prototype.hasOwnProperty.call(message, "url")
|
|
211
|
+
&& Object.prototype.hasOwnProperty.call(message, "method");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
exports.JSONLoggerPlugin = JSONLoggerPlugin;
|
|
215
|
+
//# sourceMappingURL=JSONLoggerPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JSONLoggerPlugin.js","sourceRoot":"","sources":["../../src/lib/JSONLoggerPlugin.ts"],"names":[],"mappings":";;;;;;AAAA,2DAA0C;AAC1C,yCAAsC;AAEtC,8CAA2C;AAM3C,0CAA+F;AAC/F,4EAAkD;AAQlD,MAAa,gBAAgB;IASzB,YACuB,OAAe,EACf,UAAmB,EACnB,sBAA8B,EAAE;QAFhC,YAAO,GAAP,OAAO,CAAQ;QACf,eAAU,GAAV,UAAU,CAAS;QACnB,wBAAmB,GAAnB,mBAAmB,CAAa;IACnD,CAAC;IAEL;;;;;OAKG;IACI,KAAK,CAAC,MAAM,CAAC,IAA2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("@odg/exception");
|
|
5
|
+
class ExceptionPluginLogger extends exception_1.Exception {
|
|
6
|
+
}
|
|
7
|
+
exports.ExceptionPluginLogger = ExceptionPluginLogger;
|
|
8
|
+
//# sourceMappingURL=ParserException.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ParserException.js","sourceRoot":"","sources":["../../src/lib/ParserException.ts"],"names":[],"mappings":";;;AAAA,8CAA2C;AAE3C,MAAa,qBAAsB,SAAQ,qBAAS;CAEnD;AAFD,sDAEC"}
|