nextrans-logger 0.1.12 → 0.1.14
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/lib/index.cjs +12 -6
- package/lib/index.js +12 -6
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -7,6 +7,9 @@ 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 replaceAnsi(data) {
|
|
11
|
+
return data.replace(/[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
|
|
12
|
+
}
|
|
10
13
|
function formatMessage(log, env) {
|
|
11
14
|
var _a, _b, _c;
|
|
12
15
|
const message = [];
|
|
@@ -36,15 +39,16 @@ function formatMessage(log, env) {
|
|
|
36
39
|
}
|
|
37
40
|
if (typeof (log === null || log === void 0 ? void 0 : log.stack) !== 'undefined') {
|
|
38
41
|
message.push('\n');
|
|
39
|
-
message.push(log === null || log === void 0 ? void 0 : log.stack);
|
|
42
|
+
message.push('```' + (log === null || log === void 0 ? void 0 : log.stack) + '```');
|
|
40
43
|
}
|
|
41
44
|
else if (typeof (log === null || log === void 0 ? void 0 : log.options) !== 'undefined') {
|
|
42
45
|
message.push('\n');
|
|
43
46
|
message.push(log === null || log === void 0 ? void 0 : log.options);
|
|
44
47
|
}
|
|
45
|
-
|
|
46
|
-
.map((data) => (typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' :
|
|
48
|
+
const output = message
|
|
49
|
+
.map((data) => (typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `${data}`))
|
|
47
50
|
.join(' ');
|
|
51
|
+
return output;
|
|
48
52
|
}
|
|
49
53
|
class Logging {
|
|
50
54
|
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
|
|
@@ -58,7 +62,7 @@ class Logging {
|
|
|
58
62
|
this.cloudwatchOption = cloudwatchOption;
|
|
59
63
|
}
|
|
60
64
|
lineFormat(log) {
|
|
61
|
-
return formatMessage(log, this.env).replace(/`/g, '')
|
|
65
|
+
return formatMessage(log, this.env).replace(/`/g, '');
|
|
62
66
|
}
|
|
63
67
|
formatPrint() {
|
|
64
68
|
return winston_1.format.combine(winston_1.format.colorize({ all: true, colors: { info: 'blue', error: 'red', warn: 'yellow' } }), 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)));
|
|
@@ -76,7 +80,9 @@ class Logging {
|
|
|
76
80
|
const slack = (level) => new winston_slack_webhook_transport_1.default({
|
|
77
81
|
level,
|
|
78
82
|
webhookUrl: this.slackWebhook,
|
|
79
|
-
formatter: (log) => ({
|
|
83
|
+
formatter: (log) => ({
|
|
84
|
+
text: `:sos: ${replaceAnsi(formatMessage(log, this.env))}`,
|
|
85
|
+
}),
|
|
80
86
|
});
|
|
81
87
|
transportsData.push(slack('error'));
|
|
82
88
|
}
|
|
@@ -101,7 +107,7 @@ class Logging {
|
|
|
101
107
|
region: (_c = this.cloudwatchOption) === null || _c === void 0 ? void 0 : _c.region,
|
|
102
108
|
credentials: (_d = this === null || this === void 0 ? void 0 : this.cloudwatchOption) === null || _d === void 0 ? void 0 : _d.credentials,
|
|
103
109
|
},
|
|
104
|
-
messageFormatter: (log) => this.lineFormat(log)
|
|
110
|
+
messageFormatter: (log) => replaceAnsi(this.lineFormat(log)),
|
|
105
111
|
});
|
|
106
112
|
};
|
|
107
113
|
log.add(cloudwatch());
|
package/lib/index.js
CHANGED
|
@@ -2,6 +2,9 @@ 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 replaceAnsi(data) {
|
|
6
|
+
return data.replace(/[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
|
|
7
|
+
}
|
|
5
8
|
function formatMessage(log, env) {
|
|
6
9
|
const message = [];
|
|
7
10
|
const newDate = new Date();
|
|
@@ -30,15 +33,16 @@ function formatMessage(log, env) {
|
|
|
30
33
|
}
|
|
31
34
|
if (typeof log?.stack !== 'undefined') {
|
|
32
35
|
message.push('\n');
|
|
33
|
-
message.push(log?.stack);
|
|
36
|
+
message.push('```' + log?.stack + '```');
|
|
34
37
|
}
|
|
35
38
|
else if (typeof log?.options !== 'undefined') {
|
|
36
39
|
message.push('\n');
|
|
37
40
|
message.push(log?.options);
|
|
38
41
|
}
|
|
39
|
-
|
|
40
|
-
.map((data) => (typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' :
|
|
42
|
+
const output = message
|
|
43
|
+
.map((data) => (typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `${data}`))
|
|
41
44
|
.join(' ');
|
|
45
|
+
return output;
|
|
42
46
|
}
|
|
43
47
|
export default class Logging {
|
|
44
48
|
env = 'DEVELOPMENT';
|
|
@@ -54,7 +58,7 @@ export default class Logging {
|
|
|
54
58
|
this.cloudwatchOption = cloudwatchOption;
|
|
55
59
|
}
|
|
56
60
|
lineFormat(log) {
|
|
57
|
-
return formatMessage(log, this.env).replace(/`/g, '')
|
|
61
|
+
return formatMessage(log, this.env).replace(/`/g, '');
|
|
58
62
|
}
|
|
59
63
|
formatPrint() {
|
|
60
64
|
return format.combine(format.colorize({ all: true, colors: { info: 'blue', error: 'red', warn: 'yellow' } }), format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf((log) => this.lineFormat(log)));
|
|
@@ -71,7 +75,9 @@ export default class Logging {
|
|
|
71
75
|
const slack = (level) => new SlackHook({
|
|
72
76
|
level,
|
|
73
77
|
webhookUrl: this.slackWebhook,
|
|
74
|
-
formatter: (log) => ({
|
|
78
|
+
formatter: (log) => ({
|
|
79
|
+
text: `:sos: ${replaceAnsi(formatMessage(log, this.env))}`,
|
|
80
|
+
}),
|
|
75
81
|
});
|
|
76
82
|
transportsData.push(slack('error'));
|
|
77
83
|
}
|
|
@@ -94,7 +100,7 @@ export default class Logging {
|
|
|
94
100
|
region: this.cloudwatchOption?.region,
|
|
95
101
|
credentials: this?.cloudwatchOption?.credentials,
|
|
96
102
|
},
|
|
97
|
-
messageFormatter: (log) => this.lineFormat(log)
|
|
103
|
+
messageFormatter: (log) => replaceAnsi(this.lineFormat(log)),
|
|
98
104
|
});
|
|
99
105
|
log.add(cloudwatch());
|
|
100
106
|
}
|