nextrans-logger 0.2.20 → 0.3.0
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/bun.lock +764 -0
- package/dist/cloudwatch.d.ts +25 -0
- package/dist/cloudwatch.js +82 -0
- package/dist/helper.d.ts +15 -0
- package/dist/helper.js +108 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +168 -0
- package/dist/logger.contract.d.ts +26 -0
- package/dist/logger.contract.js +23 -0
- package/package.json +32 -44
- package/.vscode/settings.json +0 -6
- package/lib/index.cjs +0 -176
- package/lib/index.d.ts +0 -36
- package/lib/index.js +0 -168
package/lib/index.cjs
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
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
|
-
require('dotenv').config();
|
|
7
|
-
const winston_1 = require("winston");
|
|
8
|
-
const winston_cloudwatch_1 = __importDefault(require("winston-cloudwatch"));
|
|
9
|
-
const winston_slack_webhook_transport_1 = __importDefault(require("winston-slack-webhook-transport"));
|
|
10
|
-
function replaceAnsi(data) {
|
|
11
|
-
const ansiiCodePattern = /[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
|
|
12
|
-
return data.replace(ansiiCodePattern, '');
|
|
13
|
-
}
|
|
14
|
-
function formatMessage(log, env) {
|
|
15
|
-
var _a, _b, _c;
|
|
16
|
-
const message = [];
|
|
17
|
-
const newDate = new Date();
|
|
18
|
-
const year = newDate.getFullYear();
|
|
19
|
-
const month = newDate.getMonth();
|
|
20
|
-
const date = newDate.getDate();
|
|
21
|
-
const time = newDate.toTimeString();
|
|
22
|
-
const timestamp = `${year}-${month}-${date} ${time}`;
|
|
23
|
-
message.push(`[${(log === null || log === void 0 ? void 0 : log.timestamp) || timestamp}][${(_a = (env || 'Staging')) === null || _a === void 0 ? void 0 : _a.toUpperCase()}][${log.level}]:`);
|
|
24
|
-
if (typeof log === 'string') {
|
|
25
|
-
message.push(log);
|
|
26
|
-
}
|
|
27
|
-
else if (typeof ((_b = log === null || log === void 0 ? void 0 : log.errorInfo) === null || _b === void 0 ? void 0 : _b.message) !== 'undefined') {
|
|
28
|
-
message.push((_c = log.errorInfo) === null || _c === void 0 ? void 0 : _c.message);
|
|
29
|
-
}
|
|
30
|
-
else if (typeof (log === null || log === void 0 ? void 0 : log.message) !== 'undefined') {
|
|
31
|
-
if (Array.isArray(log.message)) {
|
|
32
|
-
for (const msg of log.message) {
|
|
33
|
-
message.push('\n');
|
|
34
|
-
message.push(msg);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
message.push(log.message);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (typeof (log === null || log === void 0 ? void 0 : log.stack) !== 'undefined') {
|
|
42
|
-
message.push('\n');
|
|
43
|
-
message.push('```' + (log === null || log === void 0 ? void 0 : log.stack) + '```');
|
|
44
|
-
}
|
|
45
|
-
else if (typeof (log === null || log === void 0 ? void 0 : log.options) !== 'undefined') {
|
|
46
|
-
message.push('\n');
|
|
47
|
-
message.push(log === null || log === void 0 ? void 0 : log.options);
|
|
48
|
-
}
|
|
49
|
-
const output = message
|
|
50
|
-
.map((data) => typeof data === 'object'
|
|
51
|
-
? '\n```' + JSON.stringify(data, null, '\t') + '```'
|
|
52
|
-
: `${data}`)
|
|
53
|
-
.join(' ');
|
|
54
|
-
return output;
|
|
55
|
-
}
|
|
56
|
-
class Logging {
|
|
57
|
-
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT', }) {
|
|
58
|
-
this.env = 'DEVELOPMENT';
|
|
59
|
-
this.enable = false;
|
|
60
|
-
this.env = env;
|
|
61
|
-
this.enable = enable;
|
|
62
|
-
if (slackWebhook && enable)
|
|
63
|
-
this.slackWebhook = slackWebhook;
|
|
64
|
-
if (cloudwatchOption && enable)
|
|
65
|
-
this.cloudwatchOption = cloudwatchOption;
|
|
66
|
-
this.setLog();
|
|
67
|
-
}
|
|
68
|
-
lineFormat(log) {
|
|
69
|
-
return formatMessage(log, this.env).replace(/`/g, '');
|
|
70
|
-
}
|
|
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);
|
|
73
|
-
}
|
|
74
|
-
setLog() {
|
|
75
|
-
var _a, _b, _c, _d;
|
|
76
|
-
const transportsData = [
|
|
77
|
-
new winston_1.transports.Console({
|
|
78
|
-
format: this.formatPrint(winston_1.format.colorize({
|
|
79
|
-
all: true,
|
|
80
|
-
colors: { info: 'green', error: 'red', warn: 'yellow' },
|
|
81
|
-
})),
|
|
82
|
-
handleExceptions: true,
|
|
83
|
-
level: 'debug',
|
|
84
|
-
}),
|
|
85
|
-
];
|
|
86
|
-
if (this.enable && (this === null || this === void 0 ? void 0 : this.slackWebhook)) {
|
|
87
|
-
const slack = (level) => new winston_slack_webhook_transport_1.default({
|
|
88
|
-
level,
|
|
89
|
-
webhookUrl: this.slackWebhook,
|
|
90
|
-
formatter: (log) => ({
|
|
91
|
-
text: `:sos: ${formatMessage(log, this.env)}`,
|
|
92
|
-
}),
|
|
93
|
-
});
|
|
94
|
-
transportsData.push(slack('error'));
|
|
95
|
-
}
|
|
96
|
-
const log = (0, winston_1.createLogger)({
|
|
97
|
-
format: this.formatPrint(),
|
|
98
|
-
transports: transportsData,
|
|
99
|
-
exitOnError: false,
|
|
100
|
-
});
|
|
101
|
-
if (this.enable &&
|
|
102
|
-
this.cloudwatchOption &&
|
|
103
|
-
this.cloudwatchOption.logGroupName != undefined &&
|
|
104
|
-
this.cloudwatchOption.logStreamName != undefined &&
|
|
105
|
-
((_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 &&
|
|
106
|
-
((_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 &&
|
|
107
|
-
this.cloudwatchOption.region) {
|
|
108
|
-
const cloudwatch = () => {
|
|
109
|
-
var _a, _b, _c, _d;
|
|
110
|
-
return new winston_cloudwatch_1.default({
|
|
111
|
-
logGroupName: (_a = this.cloudwatchOption) === null || _a === void 0 ? void 0 : _a.logGroupName,
|
|
112
|
-
logStreamName: (_b = this.cloudwatchOption) === null || _b === void 0 ? void 0 : _b.logStreamName,
|
|
113
|
-
awsOptions: {
|
|
114
|
-
region: (_c = this.cloudwatchOption) === null || _c === void 0 ? void 0 : _c.region,
|
|
115
|
-
credentials: (_d = this === null || this === void 0 ? void 0 : this.cloudwatchOption) === null || _d === void 0 ? void 0 : _d.credentials,
|
|
116
|
-
},
|
|
117
|
-
messageFormatter: (log) => replaceAnsi(this.lineFormat(log)),
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
log.add(cloudwatch());
|
|
121
|
-
}
|
|
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
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
exports.default = Logging;
|
package/lib/index.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import * as logform from 'logform';
|
|
2
|
-
import winston from 'winston';
|
|
3
|
-
import { LogObject } from 'winston-cloudwatch';
|
|
4
|
-
export type ErrorType = 'error' | 'info' | 'warn' | 'debug';
|
|
5
|
-
export interface CloudwatchOptions {
|
|
6
|
-
logGroupName: string;
|
|
7
|
-
logStreamName: string;
|
|
8
|
-
region: string;
|
|
9
|
-
credentials: {
|
|
10
|
-
accessKeyId: string;
|
|
11
|
-
secretAccessKey: string;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
export interface LoggingOptions {
|
|
15
|
-
env: string;
|
|
16
|
-
enable: boolean;
|
|
17
|
-
slackWebhook?: string;
|
|
18
|
-
cloudwatchOption?: CloudwatchOptions;
|
|
19
|
-
}
|
|
20
|
-
export default class Logging {
|
|
21
|
-
protected env: string;
|
|
22
|
-
protected enable: boolean;
|
|
23
|
-
protected slackWebhook?: string;
|
|
24
|
-
protected cloudwatchOption?: CloudwatchOptions;
|
|
25
|
-
protected log: winston.Logger;
|
|
26
|
-
constructor({ enable, slackWebhook, cloudwatchOption, env, }: LoggingOptions);
|
|
27
|
-
protected lineFormat(log: LogObject): string;
|
|
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;
|
|
36
|
-
}
|
package/lib/index.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
require('dotenv').config();
|
|
2
|
-
import { createLogger, format, transports } from 'winston';
|
|
3
|
-
import WinstonCloudwatch from 'winston-cloudwatch';
|
|
4
|
-
import SlackHook from 'winston-slack-webhook-transport';
|
|
5
|
-
function replaceAnsi(data) {
|
|
6
|
-
const ansiiCodePattern = /[\u001b\u009b][[()#;?]*(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
|
|
7
|
-
return data.replace(ansiiCodePattern, '');
|
|
8
|
-
}
|
|
9
|
-
function formatMessage(log, env) {
|
|
10
|
-
const message = [];
|
|
11
|
-
const newDate = new Date();
|
|
12
|
-
const year = newDate.getFullYear();
|
|
13
|
-
const month = newDate.getMonth();
|
|
14
|
-
const date = newDate.getDate();
|
|
15
|
-
const time = newDate.toTimeString();
|
|
16
|
-
const timestamp = `${year}-${month}-${date} ${time}`;
|
|
17
|
-
message.push(`[${log?.timestamp || timestamp}][${(env || 'Staging')?.toUpperCase()}][${log.level}]:`);
|
|
18
|
-
if (typeof log === 'string') {
|
|
19
|
-
message.push(log);
|
|
20
|
-
}
|
|
21
|
-
else if (typeof log?.errorInfo?.message !== 'undefined') {
|
|
22
|
-
message.push(log.errorInfo?.message);
|
|
23
|
-
}
|
|
24
|
-
else if (typeof log?.message !== 'undefined') {
|
|
25
|
-
if (Array.isArray(log.message)) {
|
|
26
|
-
for (const msg of log.message) {
|
|
27
|
-
message.push('\n');
|
|
28
|
-
message.push(msg);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
message.push(log.message);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (typeof log?.stack !== 'undefined') {
|
|
36
|
-
message.push('\n');
|
|
37
|
-
message.push('```' + log?.stack + '```');
|
|
38
|
-
}
|
|
39
|
-
else if (typeof log?.options !== 'undefined') {
|
|
40
|
-
message.push('\n');
|
|
41
|
-
message.push(log?.options);
|
|
42
|
-
}
|
|
43
|
-
const output = message
|
|
44
|
-
.map((data) => typeof data === 'object'
|
|
45
|
-
? '\n```' + JSON.stringify(data, null, '\t') + '```'
|
|
46
|
-
: `${data}`)
|
|
47
|
-
.join(' ');
|
|
48
|
-
return output;
|
|
49
|
-
}
|
|
50
|
-
export default class Logging {
|
|
51
|
-
env = 'DEVELOPMENT';
|
|
52
|
-
enable = false;
|
|
53
|
-
slackWebhook;
|
|
54
|
-
cloudwatchOption;
|
|
55
|
-
log;
|
|
56
|
-
constructor({ enable = false, slackWebhook, cloudwatchOption, env = 'DEVELOPMENT', }) {
|
|
57
|
-
this.env = env;
|
|
58
|
-
this.enable = enable;
|
|
59
|
-
if (slackWebhook && enable)
|
|
60
|
-
this.slackWebhook = slackWebhook;
|
|
61
|
-
if (cloudwatchOption && enable)
|
|
62
|
-
this.cloudwatchOption = cloudwatchOption;
|
|
63
|
-
this.setLog();
|
|
64
|
-
}
|
|
65
|
-
lineFormat(log) {
|
|
66
|
-
return formatMessage(log, this.env).replace(/`/g, '');
|
|
67
|
-
}
|
|
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);
|
|
70
|
-
}
|
|
71
|
-
setLog() {
|
|
72
|
-
const transportsData = [
|
|
73
|
-
new transports.Console({
|
|
74
|
-
format: this.formatPrint(format.colorize({
|
|
75
|
-
all: true,
|
|
76
|
-
colors: { info: 'green', error: 'red', warn: 'yellow' },
|
|
77
|
-
})),
|
|
78
|
-
handleExceptions: true,
|
|
79
|
-
level: 'debug',
|
|
80
|
-
}),
|
|
81
|
-
];
|
|
82
|
-
if (this.enable && this?.slackWebhook) {
|
|
83
|
-
const slack = (level) => new SlackHook({
|
|
84
|
-
level,
|
|
85
|
-
webhookUrl: this.slackWebhook,
|
|
86
|
-
formatter: (log) => ({
|
|
87
|
-
text: `:sos: ${formatMessage(log, this.env)}`,
|
|
88
|
-
}),
|
|
89
|
-
});
|
|
90
|
-
transportsData.push(slack('error'));
|
|
91
|
-
}
|
|
92
|
-
const log = createLogger({
|
|
93
|
-
format: this.formatPrint(),
|
|
94
|
-
transports: transportsData,
|
|
95
|
-
exitOnError: false,
|
|
96
|
-
});
|
|
97
|
-
if (this.enable &&
|
|
98
|
-
this.cloudwatchOption &&
|
|
99
|
-
this.cloudwatchOption.logGroupName != undefined &&
|
|
100
|
-
this.cloudwatchOption.logStreamName != undefined &&
|
|
101
|
-
this?.cloudwatchOption?.credentials?.secretAccessKey != undefined &&
|
|
102
|
-
this?.cloudwatchOption?.credentials?.accessKeyId != undefined &&
|
|
103
|
-
this.cloudwatchOption.region) {
|
|
104
|
-
const cloudwatch = () => new WinstonCloudwatch({
|
|
105
|
-
logGroupName: this.cloudwatchOption?.logGroupName,
|
|
106
|
-
logStreamName: this.cloudwatchOption?.logStreamName,
|
|
107
|
-
awsOptions: {
|
|
108
|
-
region: this.cloudwatchOption?.region,
|
|
109
|
-
credentials: this?.cloudwatchOption?.credentials,
|
|
110
|
-
},
|
|
111
|
-
messageFormatter: (log) => replaceAnsi(this.lineFormat(log)),
|
|
112
|
-
});
|
|
113
|
-
log.add(cloudwatch());
|
|
114
|
-
}
|
|
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
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|