nextrans-logger 0.1.6 → 0.1.8
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 +26 -17
- package/lib/index.js +26 -17
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -7,9 +7,6 @@ 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 prettyFormat(data) {
|
|
11
|
-
return typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `\`${data}\``;
|
|
12
|
-
}
|
|
13
10
|
function formatMessage(log, env) {
|
|
14
11
|
var _a, _b, _c;
|
|
15
12
|
const message = [];
|
|
@@ -19,25 +16,33 @@ function formatMessage(log, env) {
|
|
|
19
16
|
const date = newDate.getDate();
|
|
20
17
|
const time = newDate.toTimeString();
|
|
21
18
|
const timestamp = `${year}-${month}-${date} ${time}`;
|
|
22
|
-
message.push(
|
|
23
|
-
message.push(
|
|
24
|
-
message.push(
|
|
19
|
+
message.push(`${(log === null || log === void 0 ? void 0 : log.timestamp) || timestamp} `);
|
|
20
|
+
message.push(`${log.level.toUpperCase()}`);
|
|
21
|
+
message.push(`${(_a = (env || 'Staging')) === null || _a === void 0 ? void 0 : _a.toUpperCase()}: `);
|
|
25
22
|
if (typeof log === 'string') {
|
|
26
|
-
message.push(
|
|
23
|
+
message.push(log);
|
|
27
24
|
}
|
|
28
25
|
else if (typeof ((_b = log === null || log === void 0 ? void 0 : log.errorInfo) === null || _b === void 0 ? void 0 : _b.message) !== 'undefined') {
|
|
29
|
-
message.push(
|
|
26
|
+
message.push((_c = log.errorInfo) === null || _c === void 0 ? void 0 : _c.message);
|
|
30
27
|
}
|
|
31
28
|
else if (typeof (log === null || log === void 0 ? void 0 : log.message) !== 'undefined') {
|
|
32
|
-
|
|
29
|
+
if (Array.isArray(log.message)) {
|
|
30
|
+
for (const msg of log.message)
|
|
31
|
+
message.push(msg);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
message.push(log.message);
|
|
35
|
+
}
|
|
33
36
|
}
|
|
34
37
|
if (typeof (log === null || log === void 0 ? void 0 : log.stack) !== 'undefined') {
|
|
35
|
-
message.push(
|
|
38
|
+
message.push(log === null || log === void 0 ? void 0 : log.stack);
|
|
36
39
|
}
|
|
37
40
|
else if (typeof (log === null || log === void 0 ? void 0 : log.options) !== 'undefined') {
|
|
38
|
-
message.push(
|
|
41
|
+
message.push(log === null || log === void 0 ? void 0 : log.options);
|
|
39
42
|
}
|
|
40
|
-
return message
|
|
43
|
+
return message
|
|
44
|
+
.map((data) => (typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `\`${data}\``))
|
|
45
|
+
.join(' ');
|
|
41
46
|
}
|
|
42
47
|
class Logging {
|
|
43
48
|
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
|
|
@@ -54,17 +59,21 @@ class Logging {
|
|
|
54
59
|
return formatMessage(log, this.env).replace(/`/g, '').replace(/\[\*/g, '[').replace(/\*\]/g, ']');
|
|
55
60
|
}
|
|
56
61
|
formatPrint() {
|
|
57
|
-
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)));
|
|
62
|
+
return winston_1.format.combine(winston_1.format.colorize({ all: true, colors: { info: 'blue', error: 'red', warn: 'yellow' } }), winston_1.format.label({ label: '[LOGGER]' }), 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)));
|
|
58
63
|
}
|
|
59
64
|
log() {
|
|
60
65
|
var _a, _b, _c, _d;
|
|
61
66
|
const transportsData = [
|
|
62
|
-
new winston_1.transports.Console({
|
|
67
|
+
new winston_1.transports.Console({
|
|
68
|
+
format: winston_1.format.combine(winston_1.format.colorize(), this.formatPrint()),
|
|
69
|
+
handleExceptions: true,
|
|
70
|
+
level: 'debug',
|
|
71
|
+
}),
|
|
63
72
|
];
|
|
64
73
|
if (this.enable && (this === null || this === void 0 ? void 0 : this.slackWebhook)) {
|
|
65
74
|
const slack = (level) => new winston_slack_webhook_transport_1.default({
|
|
66
75
|
level,
|
|
67
|
-
webhookUrl: this
|
|
76
|
+
webhookUrl: this.slackWebhook,
|
|
68
77
|
formatter: (log) => ({ text: `:sos: ${formatMessage(log, this.env)}` }),
|
|
69
78
|
});
|
|
70
79
|
transportsData.push(slack('error'));
|
|
@@ -76,8 +85,8 @@ class Logging {
|
|
|
76
85
|
});
|
|
77
86
|
if (this.enable &&
|
|
78
87
|
this.cloudwatchOption &&
|
|
79
|
-
this.cloudwatchOption.logGroupName &&
|
|
80
|
-
this.cloudwatchOption.logStreamName &&
|
|
88
|
+
this.cloudwatchOption.logGroupName != undefined &&
|
|
89
|
+
this.cloudwatchOption.logStreamName != undefined &&
|
|
81
90
|
((_b = (_a = this === null || this === void 0 ? void 0 : this.cloudwatchOption) === null || _a === void 0 ? void 0 : _a.credentials) === null || _b === void 0 ? void 0 : _b.secretAccessKey) != undefined &&
|
|
82
91
|
((_d = (_c = this === null || this === void 0 ? void 0 : this.cloudwatchOption) === null || _c === void 0 ? void 0 : _c.credentials) === null || _d === void 0 ? void 0 : _d.accessKeyId) != undefined &&
|
|
83
92
|
this.cloudwatchOption.region) {
|
package/lib/index.js
CHANGED
|
@@ -2,9 +2,6 @@ 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 prettyFormat(data) {
|
|
6
|
-
return typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `\`${data}\``;
|
|
7
|
-
}
|
|
8
5
|
function formatMessage(log, env) {
|
|
9
6
|
const message = [];
|
|
10
7
|
const newDate = new Date();
|
|
@@ -13,25 +10,33 @@ function formatMessage(log, env) {
|
|
|
13
10
|
const date = newDate.getDate();
|
|
14
11
|
const time = newDate.toTimeString();
|
|
15
12
|
const timestamp = `${year}-${month}-${date} ${time}`;
|
|
16
|
-
message.push(
|
|
17
|
-
message.push(
|
|
18
|
-
message.push(
|
|
13
|
+
message.push(`${log?.timestamp || timestamp} `);
|
|
14
|
+
message.push(`${log.level.toUpperCase()}`);
|
|
15
|
+
message.push(`${(env || 'Staging')?.toUpperCase()}: `);
|
|
19
16
|
if (typeof log === 'string') {
|
|
20
|
-
message.push(
|
|
17
|
+
message.push(log);
|
|
21
18
|
}
|
|
22
19
|
else if (typeof log?.errorInfo?.message !== 'undefined') {
|
|
23
|
-
message.push(
|
|
20
|
+
message.push(log.errorInfo?.message);
|
|
24
21
|
}
|
|
25
22
|
else if (typeof log?.message !== 'undefined') {
|
|
26
|
-
|
|
23
|
+
if (Array.isArray(log.message)) {
|
|
24
|
+
for (const msg of log.message)
|
|
25
|
+
message.push(msg);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
message.push(log.message);
|
|
29
|
+
}
|
|
27
30
|
}
|
|
28
31
|
if (typeof log?.stack !== 'undefined') {
|
|
29
|
-
message.push(
|
|
32
|
+
message.push(log?.stack);
|
|
30
33
|
}
|
|
31
34
|
else if (typeof log?.options !== 'undefined') {
|
|
32
|
-
message.push(
|
|
35
|
+
message.push(log?.options);
|
|
33
36
|
}
|
|
34
|
-
return message
|
|
37
|
+
return message
|
|
38
|
+
.map((data) => (typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `\`${data}\``))
|
|
39
|
+
.join(' ');
|
|
35
40
|
}
|
|
36
41
|
export default class Logging {
|
|
37
42
|
env = 'DEVELOPMENT';
|
|
@@ -50,16 +55,20 @@ export default class Logging {
|
|
|
50
55
|
return formatMessage(log, this.env).replace(/`/g, '').replace(/\[\*/g, '[').replace(/\*\]/g, ']');
|
|
51
56
|
}
|
|
52
57
|
formatPrint() {
|
|
53
|
-
return format.combine(format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf((log) => this.lineFormat(log)));
|
|
58
|
+
return format.combine(format.colorize({ all: true, colors: { info: 'blue', error: 'red', warn: 'yellow' } }), format.label({ label: '[LOGGER]' }), format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf((log) => this.lineFormat(log)));
|
|
54
59
|
}
|
|
55
60
|
log() {
|
|
56
61
|
const transportsData = [
|
|
57
|
-
new transports.Console({
|
|
62
|
+
new transports.Console({
|
|
63
|
+
format: format.combine(format.colorize(), this.formatPrint()),
|
|
64
|
+
handleExceptions: true,
|
|
65
|
+
level: 'debug',
|
|
66
|
+
}),
|
|
58
67
|
];
|
|
59
68
|
if (this.enable && this?.slackWebhook) {
|
|
60
69
|
const slack = (level) => new SlackHook({
|
|
61
70
|
level,
|
|
62
|
-
webhookUrl: this
|
|
71
|
+
webhookUrl: this.slackWebhook,
|
|
63
72
|
formatter: (log) => ({ text: `:sos: ${formatMessage(log, this.env)}` }),
|
|
64
73
|
});
|
|
65
74
|
transportsData.push(slack('error'));
|
|
@@ -71,8 +80,8 @@ export default class Logging {
|
|
|
71
80
|
});
|
|
72
81
|
if (this.enable &&
|
|
73
82
|
this.cloudwatchOption &&
|
|
74
|
-
this.cloudwatchOption.logGroupName &&
|
|
75
|
-
this.cloudwatchOption.logStreamName &&
|
|
83
|
+
this.cloudwatchOption.logGroupName != undefined &&
|
|
84
|
+
this.cloudwatchOption.logStreamName != undefined &&
|
|
76
85
|
this?.cloudwatchOption?.credentials?.secretAccessKey != undefined &&
|
|
77
86
|
this?.cloudwatchOption?.credentials?.accessKeyId != undefined &&
|
|
78
87
|
this.cloudwatchOption.region) {
|