nextrans-logger 0.1.4 → 0.1.5
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 +14 -11
- package/lib/index.d.ts +7 -6
- package/lib/index.js +11 -10
- package/package.json +2 -1
package/lib/index.cjs
CHANGED
|
@@ -3,14 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
require('dotenv').config();
|
|
6
7
|
const winston_1 = require("winston");
|
|
7
8
|
const winston_cloudwatch_1 = __importDefault(require("winston-cloudwatch"));
|
|
8
9
|
const winston_slack_webhook_transport_1 = __importDefault(require("winston-slack-webhook-transport"));
|
|
9
10
|
function prettyFormat(data) {
|
|
10
|
-
return typeof data === 'object' ? '\n```' + JSON.stringify(data, null,
|
|
11
|
+
return typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `\`${data}\``;
|
|
11
12
|
}
|
|
12
|
-
function formatMessage(log, env
|
|
13
|
-
var _a, _b;
|
|
13
|
+
function formatMessage(log, env) {
|
|
14
|
+
var _a, _b, _c;
|
|
14
15
|
const message = [];
|
|
15
16
|
const newDate = new Date();
|
|
16
17
|
const year = newDate.getFullYear();
|
|
@@ -20,18 +21,18 @@ function formatMessage(log, env = 'Staging') {
|
|
|
20
21
|
const timestamp = `${year}-${month}-${date} ${time}`;
|
|
21
22
|
message.push(`[${(log === null || log === void 0 ? void 0 : log.timestamp) || timestamp}]`);
|
|
22
23
|
message.push(`[*${log.level.toUpperCase()}*]`);
|
|
23
|
-
message.push(`[${env === null ||
|
|
24
|
+
message.push(`[${(_a = (env || 'Staging')) === null || _a === void 0 ? void 0 : _a.toUpperCase()}]: `);
|
|
24
25
|
if (typeof log === 'string') {
|
|
25
26
|
message.push(prettyFormat(log));
|
|
26
27
|
}
|
|
27
|
-
else if (typeof ((
|
|
28
|
-
message.push(prettyFormat((
|
|
28
|
+
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(prettyFormat((_c = log.errorInfo) === null || _c === void 0 ? void 0 : _c.message));
|
|
29
30
|
}
|
|
30
31
|
else if (typeof (log === null || log === void 0 ? void 0 : log.message) !== 'undefined') {
|
|
31
32
|
message.push(prettyFormat(log.message));
|
|
32
33
|
}
|
|
33
34
|
if (typeof (log === null || log === void 0 ? void 0 : log.stack) !== 'undefined') {
|
|
34
|
-
message.push(
|
|
35
|
+
message.push('\n```' + JSON.stringify(log === null || log === void 0 ? void 0 : log.stack, null, '\t').replace(/\\n\ {4}/g, '\n ') + '```');
|
|
35
36
|
}
|
|
36
37
|
else if (typeof (log === null || log === void 0 ? void 0 : log.options) !== 'undefined') {
|
|
37
38
|
message.push(prettyFormat(log === null || log === void 0 ? void 0 : log.options));
|
|
@@ -40,18 +41,20 @@ function formatMessage(log, env = 'Staging') {
|
|
|
40
41
|
}
|
|
41
42
|
class Logging {
|
|
42
43
|
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
|
|
44
|
+
this.env = 'DEVELOPMENT';
|
|
45
|
+
this.enable = false;
|
|
43
46
|
this.env = env;
|
|
44
47
|
this.enable = enable;
|
|
45
|
-
if (slackWebhook)
|
|
48
|
+
if (slackWebhook && enable)
|
|
46
49
|
this.slackWebhook = slackWebhook;
|
|
47
|
-
if (cloudwatchOption)
|
|
50
|
+
if (cloudwatchOption && enable)
|
|
48
51
|
this.cloudwatchOption = cloudwatchOption;
|
|
49
52
|
}
|
|
50
53
|
lineFormat(log) {
|
|
51
54
|
return formatMessage(log, this.env).replace(/`/g, '').replace(/\[\*/g, '[').replace(/\*\]/g, ']');
|
|
52
55
|
}
|
|
53
56
|
formatPrint() {
|
|
54
|
-
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(this.lineFormat));
|
|
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)));
|
|
55
58
|
}
|
|
56
59
|
log() {
|
|
57
60
|
var _a, _b, _c, _d;
|
|
@@ -85,7 +88,7 @@ class Logging {
|
|
|
85
88
|
logStreamName: (_b = this.cloudwatchOption) === null || _b === void 0 ? void 0 : _b.logStreamName,
|
|
86
89
|
awsOptions: {
|
|
87
90
|
region: (_c = this.cloudwatchOption) === null || _c === void 0 ? void 0 : _c.region,
|
|
88
|
-
credentials: (_d = this === null || this === void 0 ? void 0 : this.cloudwatchOption) === null || _d === void 0 ? void 0 : _d.credentials
|
|
91
|
+
credentials: (_d = this === null || this === void 0 ? void 0 : this.cloudwatchOption) === null || _d === void 0 ? void 0 : _d.credentials,
|
|
89
92
|
},
|
|
90
93
|
messageFormatter: this.lineFormat,
|
|
91
94
|
});
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LogObject } from 'winston-cloudwatch';
|
|
1
2
|
export type ErrorType = 'error' | 'info' | 'warn' | 'debug';
|
|
2
3
|
export interface CloudwatchOptions {
|
|
3
4
|
logGroupName: string;
|
|
@@ -15,12 +16,12 @@ export interface LoggingOptions {
|
|
|
15
16
|
cloudwatchOption?: CloudwatchOptions;
|
|
16
17
|
}
|
|
17
18
|
export default class Logging {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
protected env: string;
|
|
20
|
+
protected enable: boolean;
|
|
21
|
+
protected slackWebhook?: string;
|
|
22
|
+
protected cloudwatchOption?: CloudwatchOptions;
|
|
22
23
|
constructor({ enable, slackWebhook, cloudwatchOption, env }: LoggingOptions);
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
protected lineFormat(log: LogObject): string;
|
|
25
|
+
protected formatPrint(): import("logform").Format;
|
|
25
26
|
log(): import("winston").Logger;
|
|
26
27
|
}
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
require('dotenv').config();
|
|
1
2
|
import { createLogger, format, transports } from 'winston';
|
|
2
3
|
import WinstonCloudwatch from 'winston-cloudwatch';
|
|
3
4
|
import SlackHook from 'winston-slack-webhook-transport';
|
|
4
5
|
function prettyFormat(data) {
|
|
5
|
-
return typeof data === 'object' ? '\n```' + JSON.stringify(data, null,
|
|
6
|
+
return typeof data === 'object' ? '\n```' + JSON.stringify(data, null, '\t') + '```' : `\`${data}\``;
|
|
6
7
|
}
|
|
7
|
-
function formatMessage(log, env
|
|
8
|
+
function formatMessage(log, env) {
|
|
8
9
|
const message = [];
|
|
9
10
|
const newDate = new Date();
|
|
10
11
|
const year = newDate.getFullYear();
|
|
@@ -14,7 +15,7 @@ function formatMessage(log, env = 'Staging') {
|
|
|
14
15
|
const timestamp = `${year}-${month}-${date} ${time}`;
|
|
15
16
|
message.push(`[${log?.timestamp || timestamp}]`);
|
|
16
17
|
message.push(`[*${log.level.toUpperCase()}*]`);
|
|
17
|
-
message.push(`[${env?.toUpperCase()}]: `);
|
|
18
|
+
message.push(`[${(env || 'Staging')?.toUpperCase()}]: `);
|
|
18
19
|
if (typeof log === 'string') {
|
|
19
20
|
message.push(prettyFormat(log));
|
|
20
21
|
}
|
|
@@ -25,7 +26,7 @@ function formatMessage(log, env = 'Staging') {
|
|
|
25
26
|
message.push(prettyFormat(log.message));
|
|
26
27
|
}
|
|
27
28
|
if (typeof log?.stack !== 'undefined') {
|
|
28
|
-
message.push(
|
|
29
|
+
message.push('\n```' + JSON.stringify(log?.stack, null, '\t').replace(/\\n\ {4}/g, '\n ') + '```');
|
|
29
30
|
}
|
|
30
31
|
else if (typeof log?.options !== 'undefined') {
|
|
31
32
|
message.push(prettyFormat(log?.options));
|
|
@@ -33,23 +34,23 @@ function formatMessage(log, env = 'Staging') {
|
|
|
33
34
|
return message.join('');
|
|
34
35
|
}
|
|
35
36
|
export default class Logging {
|
|
36
|
-
env;
|
|
37
|
-
enable;
|
|
37
|
+
env = 'DEVELOPMENT';
|
|
38
|
+
enable = false;
|
|
38
39
|
slackWebhook;
|
|
39
40
|
cloudwatchOption;
|
|
40
41
|
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
|
|
41
42
|
this.env = env;
|
|
42
43
|
this.enable = enable;
|
|
43
|
-
if (slackWebhook)
|
|
44
|
+
if (slackWebhook && enable)
|
|
44
45
|
this.slackWebhook = slackWebhook;
|
|
45
|
-
if (cloudwatchOption)
|
|
46
|
+
if (cloudwatchOption && enable)
|
|
46
47
|
this.cloudwatchOption = cloudwatchOption;
|
|
47
48
|
}
|
|
48
49
|
lineFormat(log) {
|
|
49
50
|
return formatMessage(log, this.env).replace(/`/g, '').replace(/\[\*/g, '[').replace(/\*\]/g, ']');
|
|
50
51
|
}
|
|
51
52
|
formatPrint() {
|
|
52
|
-
return format.combine(format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf(this.lineFormat));
|
|
53
|
+
return format.combine(format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf((log) => this.lineFormat(log)));
|
|
53
54
|
}
|
|
54
55
|
log() {
|
|
55
56
|
const transportsData = [
|
|
@@ -80,7 +81,7 @@ export default class Logging {
|
|
|
80
81
|
logStreamName: this.cloudwatchOption?.logStreamName,
|
|
81
82
|
awsOptions: {
|
|
82
83
|
region: this.cloudwatchOption?.region,
|
|
83
|
-
credentials: this?.cloudwatchOption?.credentials
|
|
84
|
+
credentials: this?.cloudwatchOption?.credentials,
|
|
84
85
|
},
|
|
85
86
|
messageFormatter: this.lineFormat,
|
|
86
87
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nextrans-logger",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Logging for nextrans app service",
|
|
5
5
|
"main": "lib/index.cjs",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/jest": "^29.4.0",
|
|
25
25
|
"@types/node": "^18.14.6",
|
|
26
|
+
"dotenv": "^16.0.3",
|
|
26
27
|
"jest": "^29.5.0",
|
|
27
28
|
"ts-jest": "^29.0.5",
|
|
28
29
|
"ts-node": "^10.9.1",
|