nextrans-logger 0.1.1 → 0.1.3
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 +98 -0
- package/lib/index.d.ts +26 -0
- package/lib/index.js +91 -0
- package/package.json +1 -1
- package/bitbucket-pipelines.yml +0 -26
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const winston_1 = require("winston");
|
|
7
|
+
const winston_cloudwatch_1 = __importDefault(require("winston-cloudwatch"));
|
|
8
|
+
const winston_slack_webhook_transport_1 = __importDefault(require("winston-slack-webhook-transport"));
|
|
9
|
+
function prettyFormat(data) {
|
|
10
|
+
return typeof data === 'object' ? '\n```' + JSON.stringify(data, null, 3) + '```' : `\`${data}\``;
|
|
11
|
+
}
|
|
12
|
+
function formatMessage(log, env = 'Staging') {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
const message = [];
|
|
15
|
+
const newDate = new Date();
|
|
16
|
+
const year = newDate.getFullYear();
|
|
17
|
+
const month = newDate.getMonth();
|
|
18
|
+
const date = newDate.getDate();
|
|
19
|
+
const time = newDate.toTimeString();
|
|
20
|
+
const timestamp = `${year}-${month}-${date} ${time}`;
|
|
21
|
+
message.push(`[${(log === null || log === void 0 ? void 0 : log.timestamp) || timestamp}]`);
|
|
22
|
+
message.push(`[*${log.level.toUpperCase()}*]`);
|
|
23
|
+
message.push(`[${env === null || env === void 0 ? void 0 : env.toUpperCase()}]: `);
|
|
24
|
+
if (typeof log === 'string') {
|
|
25
|
+
message.push(prettyFormat(log));
|
|
26
|
+
}
|
|
27
|
+
else if (typeof ((_a = log === null || log === void 0 ? void 0 : log.errorInfo) === null || _a === void 0 ? void 0 : _a.message) !== 'undefined') {
|
|
28
|
+
message.push(prettyFormat((_b = log.errorInfo) === null || _b === void 0 ? void 0 : _b.message));
|
|
29
|
+
}
|
|
30
|
+
else if (typeof (log === null || log === void 0 ? void 0 : log.message) !== 'undefined') {
|
|
31
|
+
message.push(prettyFormat(log.message));
|
|
32
|
+
}
|
|
33
|
+
if (typeof (log === null || log === void 0 ? void 0 : log.stack) !== 'undefined') {
|
|
34
|
+
message.push("\n```" + JSON.stringify(log === null || log === void 0 ? void 0 : log.stack, null, 3).replace(/\\n {4}/g, "\n ") + "```");
|
|
35
|
+
}
|
|
36
|
+
else if (typeof (log === null || log === void 0 ? void 0 : log.options) !== 'undefined') {
|
|
37
|
+
message.push(prettyFormat(log === null || log === void 0 ? void 0 : log.options));
|
|
38
|
+
}
|
|
39
|
+
return message.join('');
|
|
40
|
+
}
|
|
41
|
+
class Logging {
|
|
42
|
+
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
|
|
43
|
+
this.env = env;
|
|
44
|
+
this.enable = enable;
|
|
45
|
+
if (slackWebhook)
|
|
46
|
+
this.slackWebhook = slackWebhook;
|
|
47
|
+
if (cloudwatchOption)
|
|
48
|
+
this.cloudwatchOption = cloudwatchOption;
|
|
49
|
+
}
|
|
50
|
+
lineFormat(log) {
|
|
51
|
+
return formatMessage(log, this.env).replace(/`/g, '').replace(/\[\*/g, '[').replace(/\*\]/g, ']');
|
|
52
|
+
}
|
|
53
|
+
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));
|
|
55
|
+
}
|
|
56
|
+
log() {
|
|
57
|
+
var _a, _b, _c, _d;
|
|
58
|
+
const transportsData = [
|
|
59
|
+
new winston_1.transports.Console({ format: this.formatPrint(), handleExceptions: true, level: 'debug' }),
|
|
60
|
+
];
|
|
61
|
+
if (this.enable && (this === null || this === void 0 ? void 0 : this.slackWebhook)) {
|
|
62
|
+
const slack = (level) => new winston_slack_webhook_transport_1.default({
|
|
63
|
+
level,
|
|
64
|
+
webhookUrl: this === null || this === void 0 ? void 0 : this.slackWebhook,
|
|
65
|
+
formatter: (log) => ({ text: `:sos: ${formatMessage(log, this.env)}` }),
|
|
66
|
+
});
|
|
67
|
+
transportsData.push(slack('error'));
|
|
68
|
+
}
|
|
69
|
+
const log = (0, winston_1.createLogger)({
|
|
70
|
+
format: this.formatPrint(),
|
|
71
|
+
transports: transportsData,
|
|
72
|
+
exitOnError: false,
|
|
73
|
+
});
|
|
74
|
+
if (this.enable &&
|
|
75
|
+
this.cloudwatchOption &&
|
|
76
|
+
this.cloudwatchOption.logGroupName &&
|
|
77
|
+
this.cloudwatchOption.logStreamName &&
|
|
78
|
+
((_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 &&
|
|
79
|
+
((_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 &&
|
|
80
|
+
this.cloudwatchOption.region) {
|
|
81
|
+
const cloudwatch = () => {
|
|
82
|
+
var _a, _b, _c, _d;
|
|
83
|
+
return new winston_cloudwatch_1.default({
|
|
84
|
+
logGroupName: (_a = this.cloudwatchOption) === null || _a === void 0 ? void 0 : _a.logGroupName,
|
|
85
|
+
logStreamName: (_b = this.cloudwatchOption) === null || _b === void 0 ? void 0 : _b.logStreamName,
|
|
86
|
+
awsOptions: {
|
|
87
|
+
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
|
|
89
|
+
},
|
|
90
|
+
messageFormatter: this.lineFormat,
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
log.add(cloudwatch());
|
|
94
|
+
}
|
|
95
|
+
return log;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.default = Logging;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type ErrorType = 'error' | 'info' | 'warn' | 'debug';
|
|
2
|
+
export interface CloudwatchOptions {
|
|
3
|
+
logGroupName: string;
|
|
4
|
+
logStreamName: string;
|
|
5
|
+
region: string;
|
|
6
|
+
credentials: {
|
|
7
|
+
accessKeyId: string;
|
|
8
|
+
secretAccessKey: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface LoggingOptions {
|
|
12
|
+
env: string;
|
|
13
|
+
enable: boolean;
|
|
14
|
+
slackWebhook?: string;
|
|
15
|
+
cloudwatchOption?: CloudwatchOptions;
|
|
16
|
+
}
|
|
17
|
+
export default class Logging {
|
|
18
|
+
private env;
|
|
19
|
+
private enable;
|
|
20
|
+
private slackWebhook?;
|
|
21
|
+
private cloudwatchOption?;
|
|
22
|
+
constructor({ enable, slackWebhook, cloudwatchOption, env }: LoggingOptions);
|
|
23
|
+
private lineFormat;
|
|
24
|
+
private formatPrint;
|
|
25
|
+
log(): import("winston").Logger;
|
|
26
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createLogger, format, transports } from 'winston';
|
|
2
|
+
import WinstonCloudwatch from 'winston-cloudwatch';
|
|
3
|
+
import SlackHook from 'winston-slack-webhook-transport';
|
|
4
|
+
function prettyFormat(data) {
|
|
5
|
+
return typeof data === 'object' ? '\n```' + JSON.stringify(data, null, 3) + '```' : `\`${data}\``;
|
|
6
|
+
}
|
|
7
|
+
function formatMessage(log, env = 'Staging') {
|
|
8
|
+
const message = [];
|
|
9
|
+
const newDate = new Date();
|
|
10
|
+
const year = newDate.getFullYear();
|
|
11
|
+
const month = newDate.getMonth();
|
|
12
|
+
const date = newDate.getDate();
|
|
13
|
+
const time = newDate.toTimeString();
|
|
14
|
+
const timestamp = `${year}-${month}-${date} ${time}`;
|
|
15
|
+
message.push(`[${log?.timestamp || timestamp}]`);
|
|
16
|
+
message.push(`[*${log.level.toUpperCase()}*]`);
|
|
17
|
+
message.push(`[${env?.toUpperCase()}]: `);
|
|
18
|
+
if (typeof log === 'string') {
|
|
19
|
+
message.push(prettyFormat(log));
|
|
20
|
+
}
|
|
21
|
+
else if (typeof log?.errorInfo?.message !== 'undefined') {
|
|
22
|
+
message.push(prettyFormat(log.errorInfo?.message));
|
|
23
|
+
}
|
|
24
|
+
else if (typeof log?.message !== 'undefined') {
|
|
25
|
+
message.push(prettyFormat(log.message));
|
|
26
|
+
}
|
|
27
|
+
if (typeof log?.stack !== 'undefined') {
|
|
28
|
+
message.push("\n```" + JSON.stringify(log?.stack, null, 3).replace(/\\n {4}/g, "\n ") + "```");
|
|
29
|
+
}
|
|
30
|
+
else if (typeof log?.options !== 'undefined') {
|
|
31
|
+
message.push(prettyFormat(log?.options));
|
|
32
|
+
}
|
|
33
|
+
return message.join('');
|
|
34
|
+
}
|
|
35
|
+
export default class Logging {
|
|
36
|
+
env;
|
|
37
|
+
enable;
|
|
38
|
+
slackWebhook;
|
|
39
|
+
cloudwatchOption;
|
|
40
|
+
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT' }) {
|
|
41
|
+
this.env = env;
|
|
42
|
+
this.enable = enable;
|
|
43
|
+
if (slackWebhook)
|
|
44
|
+
this.slackWebhook = slackWebhook;
|
|
45
|
+
if (cloudwatchOption)
|
|
46
|
+
this.cloudwatchOption = cloudwatchOption;
|
|
47
|
+
}
|
|
48
|
+
lineFormat(log) {
|
|
49
|
+
return formatMessage(log, this.env).replace(/`/g, '').replace(/\[\*/g, '[').replace(/\*\]/g, ']');
|
|
50
|
+
}
|
|
51
|
+
formatPrint() {
|
|
52
|
+
return format.combine(format.errors({ stack: true }), format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.printf(this.lineFormat));
|
|
53
|
+
}
|
|
54
|
+
log() {
|
|
55
|
+
const transportsData = [
|
|
56
|
+
new transports.Console({ format: this.formatPrint(), handleExceptions: true, level: 'debug' }),
|
|
57
|
+
];
|
|
58
|
+
if (this.enable && this?.slackWebhook) {
|
|
59
|
+
const slack = (level) => new SlackHook({
|
|
60
|
+
level,
|
|
61
|
+
webhookUrl: this?.slackWebhook,
|
|
62
|
+
formatter: (log) => ({ text: `:sos: ${formatMessage(log, this.env)}` }),
|
|
63
|
+
});
|
|
64
|
+
transportsData.push(slack('error'));
|
|
65
|
+
}
|
|
66
|
+
const log = createLogger({
|
|
67
|
+
format: this.formatPrint(),
|
|
68
|
+
transports: transportsData,
|
|
69
|
+
exitOnError: false,
|
|
70
|
+
});
|
|
71
|
+
if (this.enable &&
|
|
72
|
+
this.cloudwatchOption &&
|
|
73
|
+
this.cloudwatchOption.logGroupName &&
|
|
74
|
+
this.cloudwatchOption.logStreamName &&
|
|
75
|
+
this?.cloudwatchOption?.credentials?.secretAccessKey != undefined &&
|
|
76
|
+
this?.cloudwatchOption?.credentials?.accessKeyId != undefined &&
|
|
77
|
+
this.cloudwatchOption.region) {
|
|
78
|
+
const cloudwatch = () => new WinstonCloudwatch({
|
|
79
|
+
logGroupName: this.cloudwatchOption?.logGroupName,
|
|
80
|
+
logStreamName: this.cloudwatchOption?.logStreamName,
|
|
81
|
+
awsOptions: {
|
|
82
|
+
region: this.cloudwatchOption?.region,
|
|
83
|
+
credentials: this?.cloudwatchOption?.credentials
|
|
84
|
+
},
|
|
85
|
+
messageFormatter: this.lineFormat,
|
|
86
|
+
});
|
|
87
|
+
log.add(cloudwatch());
|
|
88
|
+
}
|
|
89
|
+
return log;
|
|
90
|
+
}
|
|
91
|
+
}
|
package/package.json
CHANGED
package/bitbucket-pipelines.yml
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
image: node:16
|
|
2
|
-
|
|
3
|
-
pipelines:
|
|
4
|
-
branches:
|
|
5
|
-
main:
|
|
6
|
-
- step:
|
|
7
|
-
name: Build and Test
|
|
8
|
-
caches:
|
|
9
|
-
- node
|
|
10
|
-
script:
|
|
11
|
-
- npm install
|
|
12
|
-
- npm run build
|
|
13
|
-
- npm run build:cjs
|
|
14
|
-
- npm run move:cjs
|
|
15
|
-
- step:
|
|
16
|
-
name: Security Scan
|
|
17
|
-
script:
|
|
18
|
-
- pipe: atlassian/git-secrets-scan:0.5.1
|
|
19
|
-
- step:
|
|
20
|
-
name: Publish
|
|
21
|
-
deployment: production
|
|
22
|
-
script:
|
|
23
|
-
- npm config set registry "http://registry.npmjs.org"
|
|
24
|
-
- pipe: atlassian/npm-publish:0.3.2
|
|
25
|
-
variables:
|
|
26
|
-
NPM_TOKEN: $NPM_TOKEN
|