nextrans-logger 0.1.15 → 0.2.4

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 ADDED
@@ -0,0 +1,10 @@
1
+ root = true
2
+
3
+ [*.{ts, js}]
4
+ indent_size = 2
5
+
6
+ [*.md]
7
+ trim_trailing_whitespace = false
8
+
9
+ [*]
10
+ quote_type = single
@@ -0,0 +1,6 @@
1
+ {
2
+ "editor.tabSize": 2,
3
+ "prettier.singleQuote": true,
4
+ "javascript.preferences.quoteStyle": "single",
5
+ "typescript.preferences.quoteStyle": "single"
6
+ }
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
- return data.replace(/[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
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;
@@ -46,12 +47,14 @@ function formatMessage(log, env) {
46
47
  message.push(log === null || log === void 0 ? void 0 : log.options);
47
48
  }
48
49
  const output = message
49
- .map((data) => (typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `${data}`))
50
+ .map((data) => typeof data === 'object'
51
+ ? '\n```' + JSON.stringify(data, null, '\t') + '```'
52
+ : `${data}`)
50
53
  .join(' ');
51
54
  return output;
52
55
  }
53
56
  class Logging {
54
- constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
57
+ constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT', }) {
55
58
  this.env = 'DEVELOPMENT';
56
59
  this.enable = false;
57
60
  this.env = env;
@@ -60,18 +63,22 @@ class Logging {
60
63
  this.slackWebhook = slackWebhook;
61
64
  if (cloudwatchOption && enable)
62
65
  this.cloudwatchOption = cloudwatchOption;
66
+ this.setLog();
63
67
  }
64
68
  lineFormat(log) {
65
69
  return formatMessage(log, this.env).replace(/`/g, '');
66
70
  }
67
- formatPrint() {
68
- return winston_1.format.combine(winston_1.format.colorize({ all: true, colors: { info: 'green', 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)));
71
+ formatPrint(...formats) {
72
+ 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
73
  }
70
- log() {
74
+ setLog() {
71
75
  var _a, _b, _c, _d;
72
76
  const transportsData = [
73
77
  new winston_1.transports.Console({
74
- format: winston_1.format.combine(winston_1.format.colorize(), this.formatPrint()),
78
+ format: this.formatPrint(winston_1.format.colorize({
79
+ all: true,
80
+ colors: { info: 'green', error: 'red', warn: 'yellow' },
81
+ })),
75
82
  handleExceptions: true,
76
83
  level: 'debug',
77
84
  }),
@@ -81,10 +88,14 @@ class Logging {
81
88
  level,
82
89
  webhookUrl: this.slackWebhook,
83
90
  formatter: (log) => ({
84
- text: `:sos: ${replaceAnsi(formatMessage(log, this.env))}`,
91
+ text: `:sos: ${formatMessage(log, this.env)}`,
85
92
  }),
93
+ emitAxiosErrors: true,
94
+ handleRejections: false,
86
95
  });
87
- transportsData.push(slack('error'));
96
+ transportsData.push(slack('error').on('error', () => {
97
+ console.log('Error when send slack message');
98
+ }));
88
99
  }
89
100
  const log = (0, winston_1.createLogger)({
90
101
  format: this.formatPrint(),
@@ -112,7 +123,82 @@ class Logging {
112
123
  };
113
124
  log.add(cloudwatch());
114
125
  }
115
- return log;
126
+ this.log = log;
127
+ }
128
+ format(args) {
129
+ return args
130
+ .map((arg) => {
131
+ try {
132
+ if (arg instanceof Error) {
133
+ let logString = arg.message;
134
+ logString += '\n';
135
+ logString += '```' + (arg === null || arg === void 0 ? void 0 : arg.stack) + '```';
136
+ return logString;
137
+ }
138
+ // @ts-ignore
139
+ if (typeof (arg === null || arg === void 0 ? void 0 : arg.status) === 'number') {
140
+ return JSON.stringify({
141
+ // @ts-ignore
142
+ status: arg.status,
143
+ // @ts-ignore
144
+ data: arg.data,
145
+ }, null, '\t');
146
+ }
147
+ if (typeof arg === 'object') {
148
+ return JSON.stringify(arg, null, '\t');
149
+ }
150
+ if (typeof arg === 'number') {
151
+ return arg.toString();
152
+ }
153
+ if (typeof arg === 'string') {
154
+ return arg;
155
+ }
156
+ return undefined;
157
+ }
158
+ catch (err) {
159
+ return undefined;
160
+ }
161
+ })
162
+ .filter((d) => typeof d === 'string')
163
+ .join(': ');
164
+ }
165
+ error(...args) {
166
+ if (!this.log)
167
+ this.setLog();
168
+ if (process.env.NODE_ENV !== 'test') {
169
+ this.log.error(this.format(args));
170
+ }
171
+ }
172
+ info(...args) {
173
+ if (!this.log)
174
+ this.setLog();
175
+ if (process.env.NODE_ENV !== 'test') {
176
+ this.log.info(this.format(args));
177
+ }
178
+ }
179
+ warning(...args) {
180
+ if (!this.log)
181
+ this.setLog();
182
+ if (process.env.NODE_ENV !== 'test') {
183
+ this.log.warning(this.format(args));
184
+ }
185
+ }
186
+ warn(...args) {
187
+ if (!this.log)
188
+ this.setLog();
189
+ if (process.env.NODE_ENV !== 'test') {
190
+ this.log.warn(this.format(args));
191
+ }
192
+ }
193
+ debug(...args) {
194
+ if (!this.log)
195
+ this.setLog();
196
+ if (process.env.NODE_ENV !== 'test') {
197
+ this.log.debug(this.format(args));
198
+ }
199
+ else {
200
+ return this.format(args);
201
+ }
116
202
  }
117
203
  }
118
204
  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
- constructor({ enable, slackWebhook, cloudwatchOption, env }: LoggingOptions);
25
+ protected log: winston.Logger;
26
+ constructor({ enable, slackWebhook, cloudwatchOption, env, }: LoggingOptions);
24
27
  protected lineFormat(log: LogObject): string;
25
- protected formatPrint(): import("logform").Format;
26
- log(): import("winston").Logger;
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
@@ -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
- return data.replace(/[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
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 = [];
@@ -40,7 +41,9 @@ function formatMessage(log, env) {
40
41
  message.push(log?.options);
41
42
  }
42
43
  const output = message
43
- .map((data) => (typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `${data}`))
44
+ .map((data) => typeof data === 'object'
45
+ ? '\n```' + JSON.stringify(data, null, '\t') + '```'
46
+ : `${data}`)
44
47
  .join(' ');
45
48
  return output;
46
49
  }
@@ -49,24 +52,29 @@ export default class Logging {
49
52
  enable = false;
50
53
  slackWebhook;
51
54
  cloudwatchOption;
52
- constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
55
+ log;
56
+ constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT', }) {
53
57
  this.env = env;
54
58
  this.enable = enable;
55
59
  if (slackWebhook && enable)
56
60
  this.slackWebhook = slackWebhook;
57
61
  if (cloudwatchOption && enable)
58
62
  this.cloudwatchOption = cloudwatchOption;
63
+ this.setLog();
59
64
  }
60
65
  lineFormat(log) {
61
66
  return formatMessage(log, this.env).replace(/`/g, '');
62
67
  }
63
- formatPrint() {
64
- return format.combine(format.colorize({ all: true, colors: { info: 'green', error: 'red', warn: 'yellow' } }), format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf((log) => this.lineFormat(log)));
68
+ formatPrint(...formats) {
69
+ return format.combine(format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf((log) => this.lineFormat(log)), ...formats);
65
70
  }
66
- log() {
71
+ setLog() {
67
72
  const transportsData = [
68
73
  new transports.Console({
69
- format: format.combine(format.colorize(), this.formatPrint()),
74
+ format: this.formatPrint(format.colorize({
75
+ all: true,
76
+ colors: { info: 'green', error: 'red', warn: 'yellow' },
77
+ })),
70
78
  handleExceptions: true,
71
79
  level: 'debug',
72
80
  }),
@@ -76,10 +84,14 @@ export default class Logging {
76
84
  level,
77
85
  webhookUrl: this.slackWebhook,
78
86
  formatter: (log) => ({
79
- text: `:sos: ${replaceAnsi(formatMessage(log, this.env))}`,
87
+ text: `:sos: ${formatMessage(log, this.env)}`,
80
88
  }),
89
+ emitAxiosErrors: true,
90
+ handleRejections: false,
81
91
  });
82
- transportsData.push(slack('error'));
92
+ transportsData.push(slack('error').on('error', () => {
93
+ console.log('Error when send slack message');
94
+ }));
83
95
  }
84
96
  const log = createLogger({
85
97
  format: this.formatPrint(),
@@ -104,6 +116,81 @@ export default class Logging {
104
116
  });
105
117
  log.add(cloudwatch());
106
118
  }
107
- return log;
119
+ this.log = log;
120
+ }
121
+ format(args) {
122
+ return args
123
+ .map((arg) => {
124
+ try {
125
+ if (arg instanceof Error) {
126
+ let logString = arg.message;
127
+ logString += '\n';
128
+ logString += '```' + arg?.stack + '```';
129
+ return logString;
130
+ }
131
+ // @ts-ignore
132
+ if (typeof arg?.status === 'number') {
133
+ return JSON.stringify({
134
+ // @ts-ignore
135
+ status: arg.status,
136
+ // @ts-ignore
137
+ data: arg.data,
138
+ }, null, '\t');
139
+ }
140
+ if (typeof arg === 'object') {
141
+ return JSON.stringify(arg, null, '\t');
142
+ }
143
+ if (typeof arg === 'number') {
144
+ return arg.toString();
145
+ }
146
+ if (typeof arg === 'string') {
147
+ return arg;
148
+ }
149
+ return undefined;
150
+ }
151
+ catch (err) {
152
+ return undefined;
153
+ }
154
+ })
155
+ .filter((d) => typeof d === 'string')
156
+ .join(': ');
157
+ }
158
+ error(...args) {
159
+ if (!this.log)
160
+ this.setLog();
161
+ if (process.env.NODE_ENV !== 'test') {
162
+ this.log.error(this.format(args));
163
+ }
164
+ }
165
+ info(...args) {
166
+ if (!this.log)
167
+ this.setLog();
168
+ if (process.env.NODE_ENV !== 'test') {
169
+ this.log.info(this.format(args));
170
+ }
171
+ }
172
+ warning(...args) {
173
+ if (!this.log)
174
+ this.setLog();
175
+ if (process.env.NODE_ENV !== 'test') {
176
+ this.log.warning(this.format(args));
177
+ }
178
+ }
179
+ warn(...args) {
180
+ if (!this.log)
181
+ this.setLog();
182
+ if (process.env.NODE_ENV !== 'test') {
183
+ this.log.warn(this.format(args));
184
+ }
185
+ }
186
+ debug(...args) {
187
+ if (!this.log)
188
+ this.setLog();
189
+ if (process.env.NODE_ENV !== 'test') {
190
+ this.log.debug(this.format(args));
191
+ }
192
+ else {
193
+ return this.format(args);
194
+ }
108
195
  }
109
196
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextrans-logger",
3
- "version": "0.1.15",
3
+ "version": "0.2.4",
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.290.0",
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",