nextrans-logger 0.1.15 → 0.2.20

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,7 +88,7 @@ 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
  }),
86
93
  });
87
94
  transportsData.push(slack('error'));
@@ -112,7 +119,58 @@ class Logging {
112
119
  };
113
120
  log.add(cloudwatch());
114
121
  }
115
- return log;
122
+ this.log = log;
123
+ }
124
+ format(args) {
125
+ return args
126
+ .map((arg) => {
127
+ if (arg instanceof Error) {
128
+ let logString = arg.message;
129
+ logString += '\n';
130
+ logString += '```' + (arg === null || arg === void 0 ? void 0 : arg.stack) + '```';
131
+ return logString;
132
+ }
133
+ if (typeof arg === 'object') {
134
+ return JSON.stringify(arg, null, '\t');
135
+ }
136
+ return arg;
137
+ })
138
+ .join(': ');
139
+ }
140
+ error(...args) {
141
+ if (!this.log)
142
+ this.setLog();
143
+ if (process.env.NODE_ENV !== 'test') {
144
+ this.log.error(this.format(args));
145
+ }
146
+ }
147
+ info(...args) {
148
+ if (!this.log)
149
+ this.setLog();
150
+ if (process.env.NODE_ENV !== 'test') {
151
+ this.log.info(this.format(args));
152
+ }
153
+ }
154
+ warning(...args) {
155
+ if (!this.log)
156
+ this.setLog();
157
+ if (process.env.NODE_ENV !== 'test') {
158
+ this.log.warning(this.format(args));
159
+ }
160
+ }
161
+ warn(...args) {
162
+ if (!this.log)
163
+ this.setLog();
164
+ if (process.env.NODE_ENV !== 'test') {
165
+ this.log.warn(this.format(args));
166
+ }
167
+ }
168
+ debug(...args) {
169
+ if (!this.log)
170
+ this.setLog();
171
+ if (process.env.NODE_ENV !== 'test') {
172
+ this.log.debug(this.format(args));
173
+ }
116
174
  }
117
175
  }
118
176
  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[]): 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
- 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,7 +84,7 @@ 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
  }),
81
89
  });
82
90
  transportsData.push(slack('error'));
@@ -104,6 +112,57 @@ export default class Logging {
104
112
  });
105
113
  log.add(cloudwatch());
106
114
  }
107
- return log;
115
+ this.log = log;
116
+ }
117
+ format(args) {
118
+ return args
119
+ .map((arg) => {
120
+ if (arg instanceof Error) {
121
+ let logString = arg.message;
122
+ logString += '\n';
123
+ logString += '```' + arg?.stack + '```';
124
+ return logString;
125
+ }
126
+ if (typeof arg === 'object') {
127
+ return JSON.stringify(arg, null, '\t');
128
+ }
129
+ return arg;
130
+ })
131
+ .join(': ');
132
+ }
133
+ error(...args) {
134
+ if (!this.log)
135
+ this.setLog();
136
+ if (process.env.NODE_ENV !== 'test') {
137
+ this.log.error(this.format(args));
138
+ }
139
+ }
140
+ info(...args) {
141
+ if (!this.log)
142
+ this.setLog();
143
+ if (process.env.NODE_ENV !== 'test') {
144
+ this.log.info(this.format(args));
145
+ }
146
+ }
147
+ warning(...args) {
148
+ if (!this.log)
149
+ this.setLog();
150
+ if (process.env.NODE_ENV !== 'test') {
151
+ this.log.warning(this.format(args));
152
+ }
153
+ }
154
+ warn(...args) {
155
+ if (!this.log)
156
+ this.setLog();
157
+ if (process.env.NODE_ENV !== 'test') {
158
+ this.log.warn(this.format(args));
159
+ }
160
+ }
161
+ debug(...args) {
162
+ if (!this.log)
163
+ this.setLog();
164
+ if (process.env.NODE_ENV !== 'test') {
165
+ this.log.debug(this.format(args));
166
+ }
108
167
  }
109
168
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextrans-logger",
3
- "version": "0.1.15",
3
+ "version": "0.2.20",
4
4
  "description": "Logging for nextrans app service",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.js",
@@ -14,7 +14,7 @@
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",