nextrans-logger 0.1.15 → 0.2.10
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 +53 -7
- package/lib/index.d.ts +11 -2
- package/lib/index.js +54 -7
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -8,7 +8,8 @@ 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
10
|
function replaceAnsi(data) {
|
|
11
|
-
|
|
11
|
+
const ansiiCodePattern = /[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
|
|
12
|
+
return data.replace(ansiiCodePattern, '');
|
|
12
13
|
}
|
|
13
14
|
function formatMessage(log, env) {
|
|
14
15
|
var _a, _b, _c;
|
|
@@ -60,18 +61,19 @@ class Logging {
|
|
|
60
61
|
this.slackWebhook = slackWebhook;
|
|
61
62
|
if (cloudwatchOption && enable)
|
|
62
63
|
this.cloudwatchOption = cloudwatchOption;
|
|
64
|
+
this.setLog();
|
|
63
65
|
}
|
|
64
66
|
lineFormat(log) {
|
|
65
67
|
return formatMessage(log, this.env).replace(/`/g, '');
|
|
66
68
|
}
|
|
67
|
-
formatPrint() {
|
|
68
|
-
return winston_1.format.combine(winston_1.format.
|
|
69
|
+
formatPrint(...formats) {
|
|
70
|
+
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
71
|
}
|
|
70
|
-
|
|
72
|
+
setLog() {
|
|
71
73
|
var _a, _b, _c, _d;
|
|
72
74
|
const transportsData = [
|
|
73
75
|
new winston_1.transports.Console({
|
|
74
|
-
format:
|
|
76
|
+
format: this.formatPrint(winston_1.format.colorize({ all: true, colors: { info: 'green', error: 'red', warn: 'yellow' } })),
|
|
75
77
|
handleExceptions: true,
|
|
76
78
|
level: 'debug',
|
|
77
79
|
}),
|
|
@@ -81,7 +83,7 @@ class Logging {
|
|
|
81
83
|
level,
|
|
82
84
|
webhookUrl: this.slackWebhook,
|
|
83
85
|
formatter: (log) => ({
|
|
84
|
-
text: `:sos: ${
|
|
86
|
+
text: `:sos: ${formatMessage(log, this.env)}`,
|
|
85
87
|
}),
|
|
86
88
|
});
|
|
87
89
|
transportsData.push(slack('error'));
|
|
@@ -112,7 +114,51 @@ class Logging {
|
|
|
112
114
|
};
|
|
113
115
|
log.add(cloudwatch());
|
|
114
116
|
}
|
|
115
|
-
|
|
117
|
+
this.log = log;
|
|
118
|
+
}
|
|
119
|
+
format(arg) {
|
|
120
|
+
if (arg instanceof Error || typeof arg !== 'object') {
|
|
121
|
+
return arg;
|
|
122
|
+
}
|
|
123
|
+
if (typeof arg === 'object') {
|
|
124
|
+
return JSON.stringify(arg, null, '\t');
|
|
125
|
+
}
|
|
126
|
+
return arg;
|
|
127
|
+
}
|
|
128
|
+
error(...args) {
|
|
129
|
+
if (!this.log)
|
|
130
|
+
this.setLog();
|
|
131
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
132
|
+
this.log.error(args.map((arg) => this.format(arg)).join(': '));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
info(...args) {
|
|
136
|
+
if (!this.log)
|
|
137
|
+
this.setLog();
|
|
138
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
139
|
+
this.log.info(args.map((arg) => this.format(arg)).join(': '));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
warning(...args) {
|
|
143
|
+
if (!this.log)
|
|
144
|
+
this.setLog();
|
|
145
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
146
|
+
this.log.warning(args.map((arg) => this.format(arg)).join(': '));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
warn(...args) {
|
|
150
|
+
if (!this.log)
|
|
151
|
+
this.setLog();
|
|
152
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
153
|
+
this.log.warn(args.map((arg) => this.format(arg)).join(': '));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
debug(...args) {
|
|
157
|
+
if (!this.log)
|
|
158
|
+
this.setLog();
|
|
159
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
160
|
+
this.log.debug(args.map((arg) => this.format(arg)).join(': '));
|
|
161
|
+
}
|
|
116
162
|
}
|
|
117
163
|
}
|
|
118
164
|
exports.default = Logging;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import winston from 'winston';
|
|
1
2
|
import { LogObject } from 'winston-cloudwatch';
|
|
3
|
+
import * as logform from 'logform';
|
|
2
4
|
export type ErrorType = 'error' | 'info' | 'warn' | 'debug';
|
|
3
5
|
export interface CloudwatchOptions {
|
|
4
6
|
logGroupName: string;
|
|
@@ -20,8 +22,15 @@ export default class Logging {
|
|
|
20
22
|
protected enable: boolean;
|
|
21
23
|
protected slackWebhook?: string;
|
|
22
24
|
protected cloudwatchOption?: CloudwatchOptions;
|
|
25
|
+
protected log: winston.Logger;
|
|
23
26
|
constructor({ enable, slackWebhook, cloudwatchOption, env }: LoggingOptions);
|
|
24
27
|
protected lineFormat(log: LogObject): string;
|
|
25
|
-
protected formatPrint(
|
|
26
|
-
|
|
28
|
+
protected formatPrint(...formats: logform.Format[]): winston.Logform.Format;
|
|
29
|
+
protected setLog(): void;
|
|
30
|
+
protected format(arg: unknown): unknown;
|
|
31
|
+
error(...args: unknown[]): void;
|
|
32
|
+
info(...args: unknown[]): void;
|
|
33
|
+
warning(...args: unknown[]): void;
|
|
34
|
+
warn(...args: unknown[]): void;
|
|
35
|
+
debug(...args: unknown[]): void;
|
|
27
36
|
}
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,8 @@ import { createLogger, format, transports } from 'winston';
|
|
|
3
3
|
import WinstonCloudwatch from 'winston-cloudwatch';
|
|
4
4
|
import SlackHook from 'winston-slack-webhook-transport';
|
|
5
5
|
function replaceAnsi(data) {
|
|
6
|
-
|
|
6
|
+
const ansiiCodePattern = /[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
|
|
7
|
+
return data.replace(ansiiCodePattern, '');
|
|
7
8
|
}
|
|
8
9
|
function formatMessage(log, env) {
|
|
9
10
|
const message = [];
|
|
@@ -49,6 +50,7 @@ export default class Logging {
|
|
|
49
50
|
enable = false;
|
|
50
51
|
slackWebhook;
|
|
51
52
|
cloudwatchOption;
|
|
53
|
+
log;
|
|
52
54
|
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
|
|
53
55
|
this.env = env;
|
|
54
56
|
this.enable = enable;
|
|
@@ -56,17 +58,18 @@ export default class Logging {
|
|
|
56
58
|
this.slackWebhook = slackWebhook;
|
|
57
59
|
if (cloudwatchOption && enable)
|
|
58
60
|
this.cloudwatchOption = cloudwatchOption;
|
|
61
|
+
this.setLog();
|
|
59
62
|
}
|
|
60
63
|
lineFormat(log) {
|
|
61
64
|
return formatMessage(log, this.env).replace(/`/g, '');
|
|
62
65
|
}
|
|
63
|
-
formatPrint() {
|
|
64
|
-
return format.combine(format.
|
|
66
|
+
formatPrint(...formats) {
|
|
67
|
+
return format.combine(format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf((log) => this.lineFormat(log)), ...formats);
|
|
65
68
|
}
|
|
66
|
-
|
|
69
|
+
setLog() {
|
|
67
70
|
const transportsData = [
|
|
68
71
|
new transports.Console({
|
|
69
|
-
format:
|
|
72
|
+
format: this.formatPrint(format.colorize({ all: true, colors: { info: 'green', error: 'red', warn: 'yellow' } })),
|
|
70
73
|
handleExceptions: true,
|
|
71
74
|
level: 'debug',
|
|
72
75
|
}),
|
|
@@ -76,7 +79,7 @@ export default class Logging {
|
|
|
76
79
|
level,
|
|
77
80
|
webhookUrl: this.slackWebhook,
|
|
78
81
|
formatter: (log) => ({
|
|
79
|
-
text: `:sos: ${
|
|
82
|
+
text: `:sos: ${formatMessage(log, this.env)}`,
|
|
80
83
|
}),
|
|
81
84
|
});
|
|
82
85
|
transportsData.push(slack('error'));
|
|
@@ -104,6 +107,50 @@ export default class Logging {
|
|
|
104
107
|
});
|
|
105
108
|
log.add(cloudwatch());
|
|
106
109
|
}
|
|
107
|
-
|
|
110
|
+
this.log = log;
|
|
111
|
+
}
|
|
112
|
+
format(arg) {
|
|
113
|
+
if (arg instanceof Error || typeof arg !== 'object') {
|
|
114
|
+
return arg;
|
|
115
|
+
}
|
|
116
|
+
if (typeof arg === 'object') {
|
|
117
|
+
return JSON.stringify(arg, null, '\t');
|
|
118
|
+
}
|
|
119
|
+
return arg;
|
|
120
|
+
}
|
|
121
|
+
error(...args) {
|
|
122
|
+
if (!this.log)
|
|
123
|
+
this.setLog();
|
|
124
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
125
|
+
this.log.error(args.map((arg) => this.format(arg)).join(': '));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
info(...args) {
|
|
129
|
+
if (!this.log)
|
|
130
|
+
this.setLog();
|
|
131
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
132
|
+
this.log.info(args.map((arg) => this.format(arg)).join(': '));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
warning(...args) {
|
|
136
|
+
if (!this.log)
|
|
137
|
+
this.setLog();
|
|
138
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
139
|
+
this.log.warning(args.map((arg) => this.format(arg)).join(': '));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
warn(...args) {
|
|
143
|
+
if (!this.log)
|
|
144
|
+
this.setLog();
|
|
145
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
146
|
+
this.log.warn(args.map((arg) => this.format(arg)).join(': '));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
debug(...args) {
|
|
150
|
+
if (!this.log)
|
|
151
|
+
this.setLog();
|
|
152
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
153
|
+
this.log.debug(args.map((arg) => this.format(arg)).join(': '));
|
|
154
|
+
}
|
|
108
155
|
}
|
|
109
156
|
}
|