nextrans-logger 0.1.15 → 0.2.5
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/.editorconfig +10 -0
- package/.vscode/settings.json +6 -0
- package/lib/index.cjs +100 -11
- package/lib/index.d.ts +12 -3
- package/lib/index.js +101 -11
- package/package.json +4 -3
package/.editorconfig
ADDED
package/lib/index.cjs
CHANGED
|
@@ -7,8 +7,12 @@ require('dotenv').config();
|
|
|
7
7
|
const winston_1 = require("winston");
|
|
8
8
|
const winston_cloudwatch_1 = __importDefault(require("winston-cloudwatch"));
|
|
9
9
|
const winston_slack_webhook_transport_1 = __importDefault(require("winston-slack-webhook-transport"));
|
|
10
|
+
function sanitizePolicyData(string) {
|
|
11
|
+
return string.replace(/("amount|name|beneficiary_name")(.*?)(\,)/g, '$1: "******",');
|
|
12
|
+
}
|
|
10
13
|
function replaceAnsi(data) {
|
|
11
|
-
|
|
14
|
+
const ansiiCodePattern = /[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
|
|
15
|
+
return data.replace(ansiiCodePattern, '');
|
|
12
16
|
}
|
|
13
17
|
function formatMessage(log, env) {
|
|
14
18
|
var _a, _b, _c;
|
|
@@ -46,12 +50,14 @@ function formatMessage(log, env) {
|
|
|
46
50
|
message.push(log === null || log === void 0 ? void 0 : log.options);
|
|
47
51
|
}
|
|
48
52
|
const output = message
|
|
49
|
-
.map((data) =>
|
|
53
|
+
.map((data) => typeof data === 'object'
|
|
54
|
+
? '\n```' + JSON.stringify(data, null, '\t') + '```'
|
|
55
|
+
: `${data}`)
|
|
50
56
|
.join(' ');
|
|
51
|
-
return output;
|
|
57
|
+
return sanitizePolicyData(output);
|
|
52
58
|
}
|
|
53
59
|
class Logging {
|
|
54
|
-
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
|
|
60
|
+
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT', }) {
|
|
55
61
|
this.env = 'DEVELOPMENT';
|
|
56
62
|
this.enable = false;
|
|
57
63
|
this.env = env;
|
|
@@ -60,18 +66,22 @@ class Logging {
|
|
|
60
66
|
this.slackWebhook = slackWebhook;
|
|
61
67
|
if (cloudwatchOption && enable)
|
|
62
68
|
this.cloudwatchOption = cloudwatchOption;
|
|
69
|
+
this.setLog();
|
|
63
70
|
}
|
|
64
71
|
lineFormat(log) {
|
|
65
72
|
return formatMessage(log, this.env).replace(/`/g, '');
|
|
66
73
|
}
|
|
67
|
-
formatPrint() {
|
|
68
|
-
return winston_1.format.combine(winston_1.format.
|
|
74
|
+
formatPrint(...formats) {
|
|
75
|
+
return winston_1.format.combine(winston_1.format.errors({ stack: true }), winston_1.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston_1.format.printf((log) => this.lineFormat(log)), ...formats);
|
|
69
76
|
}
|
|
70
|
-
|
|
77
|
+
setLog() {
|
|
71
78
|
var _a, _b, _c, _d;
|
|
72
79
|
const transportsData = [
|
|
73
80
|
new winston_1.transports.Console({
|
|
74
|
-
format:
|
|
81
|
+
format: this.formatPrint(winston_1.format.colorize({
|
|
82
|
+
all: true,
|
|
83
|
+
colors: { info: 'green', error: 'red', warn: 'yellow' },
|
|
84
|
+
})),
|
|
75
85
|
handleExceptions: true,
|
|
76
86
|
level: 'debug',
|
|
77
87
|
}),
|
|
@@ -81,10 +91,14 @@ class Logging {
|
|
|
81
91
|
level,
|
|
82
92
|
webhookUrl: this.slackWebhook,
|
|
83
93
|
formatter: (log) => ({
|
|
84
|
-
text: `:sos: ${
|
|
94
|
+
text: `:sos: ${formatMessage(log, this.env)}`,
|
|
85
95
|
}),
|
|
96
|
+
emitAxiosErrors: true,
|
|
97
|
+
handleRejections: false,
|
|
86
98
|
});
|
|
87
|
-
transportsData.push(slack('error'))
|
|
99
|
+
transportsData.push(slack('error').on('error', () => {
|
|
100
|
+
console.log('Error when send slack message');
|
|
101
|
+
}));
|
|
88
102
|
}
|
|
89
103
|
const log = (0, winston_1.createLogger)({
|
|
90
104
|
format: this.formatPrint(),
|
|
@@ -112,7 +126,82 @@ class Logging {
|
|
|
112
126
|
};
|
|
113
127
|
log.add(cloudwatch());
|
|
114
128
|
}
|
|
115
|
-
|
|
129
|
+
this.log = log;
|
|
130
|
+
}
|
|
131
|
+
format(args) {
|
|
132
|
+
return args
|
|
133
|
+
.map((arg) => {
|
|
134
|
+
try {
|
|
135
|
+
if (arg instanceof Error) {
|
|
136
|
+
let logString = arg.message;
|
|
137
|
+
logString += '\n';
|
|
138
|
+
logString += '```' + (arg === null || arg === void 0 ? void 0 : arg.stack) + '```';
|
|
139
|
+
return logString;
|
|
140
|
+
}
|
|
141
|
+
// @ts-ignore
|
|
142
|
+
if (typeof (arg === null || arg === void 0 ? void 0 : arg.status) === 'number') {
|
|
143
|
+
return JSON.stringify({
|
|
144
|
+
// @ts-ignore
|
|
145
|
+
status: arg.status,
|
|
146
|
+
// @ts-ignore
|
|
147
|
+
data: arg.data,
|
|
148
|
+
}, null, '\t');
|
|
149
|
+
}
|
|
150
|
+
if (typeof arg === 'object') {
|
|
151
|
+
return JSON.stringify(arg, null, '\t');
|
|
152
|
+
}
|
|
153
|
+
if (typeof arg === 'number') {
|
|
154
|
+
return arg.toString();
|
|
155
|
+
}
|
|
156
|
+
if (typeof arg === 'string') {
|
|
157
|
+
return arg;
|
|
158
|
+
}
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
.filter((d) => typeof d === 'string')
|
|
166
|
+
.join(': ');
|
|
167
|
+
}
|
|
168
|
+
error(...args) {
|
|
169
|
+
if (!this.log)
|
|
170
|
+
this.setLog();
|
|
171
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
172
|
+
this.log.error(this.format(args));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
info(...args) {
|
|
176
|
+
if (!this.log)
|
|
177
|
+
this.setLog();
|
|
178
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
179
|
+
this.log.info(this.format(args));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
warning(...args) {
|
|
183
|
+
if (!this.log)
|
|
184
|
+
this.setLog();
|
|
185
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
186
|
+
this.log.warning(this.format(args));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
warn(...args) {
|
|
190
|
+
if (!this.log)
|
|
191
|
+
this.setLog();
|
|
192
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
193
|
+
this.log.warn(this.format(args));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
debug(...args) {
|
|
197
|
+
if (!this.log)
|
|
198
|
+
this.setLog();
|
|
199
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
200
|
+
this.log.debug(this.format(args));
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
return this.format(args);
|
|
204
|
+
}
|
|
116
205
|
}
|
|
117
206
|
}
|
|
118
207
|
exports.default = Logging;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as logform from 'logform';
|
|
2
|
+
import winston from 'winston';
|
|
1
3
|
import { LogObject } from 'winston-cloudwatch';
|
|
2
4
|
export type ErrorType = 'error' | 'info' | 'warn' | 'debug';
|
|
3
5
|
export interface CloudwatchOptions {
|
|
@@ -20,8 +22,15 @@ export default class Logging {
|
|
|
20
22
|
protected enable: boolean;
|
|
21
23
|
protected slackWebhook?: string;
|
|
22
24
|
protected cloudwatchOption?: CloudwatchOptions;
|
|
23
|
-
|
|
25
|
+
protected log: winston.Logger;
|
|
26
|
+
constructor({ enable, slackWebhook, cloudwatchOption, env, }: LoggingOptions);
|
|
24
27
|
protected lineFormat(log: LogObject): string;
|
|
25
|
-
protected formatPrint():
|
|
26
|
-
|
|
28
|
+
protected formatPrint(...formats: logform.Format[]): logform.Format;
|
|
29
|
+
protected setLog(): void;
|
|
30
|
+
protected format(args: unknown[]): string;
|
|
31
|
+
error(...args: unknown[]): void;
|
|
32
|
+
info(...args: unknown[]): void;
|
|
33
|
+
warning(...args: unknown[]): void;
|
|
34
|
+
warn(...args: unknown[]): void;
|
|
35
|
+
debug(...args: unknown[]): string | undefined;
|
|
27
36
|
}
|
package/lib/index.js
CHANGED
|
@@ -2,8 +2,12 @@ require('dotenv').config();
|
|
|
2
2
|
import { createLogger, format, transports } from 'winston';
|
|
3
3
|
import WinstonCloudwatch from 'winston-cloudwatch';
|
|
4
4
|
import SlackHook from 'winston-slack-webhook-transport';
|
|
5
|
+
function sanitizePolicyData(string) {
|
|
6
|
+
return string.replace(/("amount|name|beneficiary_name")(.*?)(\,)/g, '$1: "******",');
|
|
7
|
+
}
|
|
5
8
|
function replaceAnsi(data) {
|
|
6
|
-
|
|
9
|
+
const ansiiCodePattern = /[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
|
|
10
|
+
return data.replace(ansiiCodePattern, '');
|
|
7
11
|
}
|
|
8
12
|
function formatMessage(log, env) {
|
|
9
13
|
const message = [];
|
|
@@ -40,33 +44,40 @@ function formatMessage(log, env) {
|
|
|
40
44
|
message.push(log?.options);
|
|
41
45
|
}
|
|
42
46
|
const output = message
|
|
43
|
-
.map((data) =>
|
|
47
|
+
.map((data) => typeof data === 'object'
|
|
48
|
+
? '\n```' + JSON.stringify(data, null, '\t') + '```'
|
|
49
|
+
: `${data}`)
|
|
44
50
|
.join(' ');
|
|
45
|
-
return output;
|
|
51
|
+
return sanitizePolicyData(output);
|
|
46
52
|
}
|
|
47
53
|
export default class Logging {
|
|
48
54
|
env = 'DEVELOPMENT';
|
|
49
55
|
enable = false;
|
|
50
56
|
slackWebhook;
|
|
51
57
|
cloudwatchOption;
|
|
52
|
-
|
|
58
|
+
log;
|
|
59
|
+
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT', }) {
|
|
53
60
|
this.env = env;
|
|
54
61
|
this.enable = enable;
|
|
55
62
|
if (slackWebhook && enable)
|
|
56
63
|
this.slackWebhook = slackWebhook;
|
|
57
64
|
if (cloudwatchOption && enable)
|
|
58
65
|
this.cloudwatchOption = cloudwatchOption;
|
|
66
|
+
this.setLog();
|
|
59
67
|
}
|
|
60
68
|
lineFormat(log) {
|
|
61
69
|
return formatMessage(log, this.env).replace(/`/g, '');
|
|
62
70
|
}
|
|
63
|
-
formatPrint() {
|
|
64
|
-
return format.combine(format.
|
|
71
|
+
formatPrint(...formats) {
|
|
72
|
+
return format.combine(format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf((log) => this.lineFormat(log)), ...formats);
|
|
65
73
|
}
|
|
66
|
-
|
|
74
|
+
setLog() {
|
|
67
75
|
const transportsData = [
|
|
68
76
|
new transports.Console({
|
|
69
|
-
format:
|
|
77
|
+
format: this.formatPrint(format.colorize({
|
|
78
|
+
all: true,
|
|
79
|
+
colors: { info: 'green', error: 'red', warn: 'yellow' },
|
|
80
|
+
})),
|
|
70
81
|
handleExceptions: true,
|
|
71
82
|
level: 'debug',
|
|
72
83
|
}),
|
|
@@ -76,10 +87,14 @@ export default class Logging {
|
|
|
76
87
|
level,
|
|
77
88
|
webhookUrl: this.slackWebhook,
|
|
78
89
|
formatter: (log) => ({
|
|
79
|
-
text: `:sos: ${
|
|
90
|
+
text: `:sos: ${formatMessage(log, this.env)}`,
|
|
80
91
|
}),
|
|
92
|
+
emitAxiosErrors: true,
|
|
93
|
+
handleRejections: false,
|
|
81
94
|
});
|
|
82
|
-
transportsData.push(slack('error'))
|
|
95
|
+
transportsData.push(slack('error').on('error', () => {
|
|
96
|
+
console.log('Error when send slack message');
|
|
97
|
+
}));
|
|
83
98
|
}
|
|
84
99
|
const log = createLogger({
|
|
85
100
|
format: this.formatPrint(),
|
|
@@ -104,6 +119,81 @@ export default class Logging {
|
|
|
104
119
|
});
|
|
105
120
|
log.add(cloudwatch());
|
|
106
121
|
}
|
|
107
|
-
|
|
122
|
+
this.log = log;
|
|
123
|
+
}
|
|
124
|
+
format(args) {
|
|
125
|
+
return args
|
|
126
|
+
.map((arg) => {
|
|
127
|
+
try {
|
|
128
|
+
if (arg instanceof Error) {
|
|
129
|
+
let logString = arg.message;
|
|
130
|
+
logString += '\n';
|
|
131
|
+
logString += '```' + arg?.stack + '```';
|
|
132
|
+
return logString;
|
|
133
|
+
}
|
|
134
|
+
// @ts-ignore
|
|
135
|
+
if (typeof arg?.status === 'number') {
|
|
136
|
+
return JSON.stringify({
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
status: arg.status,
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
data: arg.data,
|
|
141
|
+
}, null, '\t');
|
|
142
|
+
}
|
|
143
|
+
if (typeof arg === 'object') {
|
|
144
|
+
return JSON.stringify(arg, null, '\t');
|
|
145
|
+
}
|
|
146
|
+
if (typeof arg === 'number') {
|
|
147
|
+
return arg.toString();
|
|
148
|
+
}
|
|
149
|
+
if (typeof arg === 'string') {
|
|
150
|
+
return arg;
|
|
151
|
+
}
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
.filter((d) => typeof d === 'string')
|
|
159
|
+
.join(': ');
|
|
160
|
+
}
|
|
161
|
+
error(...args) {
|
|
162
|
+
if (!this.log)
|
|
163
|
+
this.setLog();
|
|
164
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
165
|
+
this.log.error(this.format(args));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
info(...args) {
|
|
169
|
+
if (!this.log)
|
|
170
|
+
this.setLog();
|
|
171
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
172
|
+
this.log.info(this.format(args));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
warning(...args) {
|
|
176
|
+
if (!this.log)
|
|
177
|
+
this.setLog();
|
|
178
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
179
|
+
this.log.warning(this.format(args));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
warn(...args) {
|
|
183
|
+
if (!this.log)
|
|
184
|
+
this.setLog();
|
|
185
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
186
|
+
this.log.warn(this.format(args));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
debug(...args) {
|
|
190
|
+
if (!this.log)
|
|
191
|
+
this.setLog();
|
|
192
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
193
|
+
this.log.debug(this.format(args));
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
return this.format(args);
|
|
197
|
+
}
|
|
108
198
|
}
|
|
109
199
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nextrans-logger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "Logging for nextrans app service",
|
|
5
5
|
"main": "lib/index.cjs",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -14,11 +14,12 @@
|
|
|
14
14
|
"author": "nextrans-team",
|
|
15
15
|
"license": "ISC",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@aws-sdk/client-cloudwatch-logs": "^3.
|
|
17
|
+
"@aws-sdk/client-cloudwatch-logs": "^3.347.0",
|
|
18
18
|
"moment": "^2.29.4",
|
|
19
19
|
"winston": "^3.8.2",
|
|
20
20
|
"winston-cloudwatch": "^6.1.1",
|
|
21
|
-
"winston-slack-webhook-transport": "^2.2.3"
|
|
21
|
+
"winston-slack-webhook-transport": "^2.2.3",
|
|
22
|
+
"axios": "^1.5.0"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
25
|
"@types/jest": "^29.4.0",
|