@gus-eip/loggers 1.0.4 → 2.1.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/dist/cloudwatch.event.formatter.d.ts +28 -0
- package/dist/cloudwatch.event.formatter.js +41 -0
- package/dist/cloudwatch.logger.d.ts +5 -4
- package/dist/cloudwatch.logger.js +53 -27
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/module.js +1 -4
- package/package.json +3 -10
@@ -0,0 +1,28 @@
|
|
1
|
+
interface LogItem {
|
2
|
+
level: string;
|
3
|
+
message: string;
|
4
|
+
meta?: object;
|
5
|
+
date: Date;
|
6
|
+
}
|
7
|
+
interface FormatLogFunction {
|
8
|
+
(item: LogItem): string;
|
9
|
+
}
|
10
|
+
interface FormatLogItemFunction {
|
11
|
+
(item: LogItem): {
|
12
|
+
message: string;
|
13
|
+
timestamp: Date;
|
14
|
+
};
|
15
|
+
}
|
16
|
+
export declare class CloudWatchEventFormatter {
|
17
|
+
constructor({ formatLog, formatLogItem, }?: {
|
18
|
+
formatLog?: FormatLogFunction;
|
19
|
+
formatLogItem?: FormatLogItemFunction;
|
20
|
+
});
|
21
|
+
formatLogItem(item: LogItem): {
|
22
|
+
message: string;
|
23
|
+
timestamp: Date;
|
24
|
+
};
|
25
|
+
formatLog(item: LogItem): string;
|
26
|
+
isEmpty(value: any): boolean;
|
27
|
+
}
|
28
|
+
export {};
|
@@ -0,0 +1,41 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.CloudWatchEventFormatter = void 0;
|
4
|
+
class CloudWatchEventFormatter {
|
5
|
+
constructor({ formatLog, formatLogItem, } = {}) {
|
6
|
+
if (typeof formatLog === 'function') {
|
7
|
+
this.formatLog = formatLog;
|
8
|
+
}
|
9
|
+
else if (typeof formatLogItem === 'function') {
|
10
|
+
this.formatLogItem = formatLogItem;
|
11
|
+
}
|
12
|
+
}
|
13
|
+
formatLogItem(item) {
|
14
|
+
return {
|
15
|
+
message: this.formatLog(item),
|
16
|
+
timestamp: item.date,
|
17
|
+
};
|
18
|
+
}
|
19
|
+
formatLog(item) {
|
20
|
+
const meta = this.isEmpty(item.meta)
|
21
|
+
? ''
|
22
|
+
: ' ' + JSON.stringify(item.meta, null, 2);
|
23
|
+
return `${meta}`;
|
24
|
+
}
|
25
|
+
isEmpty(value) {
|
26
|
+
if (Array.isArray(value)) {
|
27
|
+
return value.length === 0;
|
28
|
+
}
|
29
|
+
else if (typeof value === 'object' && value !== null) {
|
30
|
+
return Object.keys(value).length === 0;
|
31
|
+
}
|
32
|
+
else if (typeof value === 'string') {
|
33
|
+
return value.trim().length === 0;
|
34
|
+
}
|
35
|
+
else if (value instanceof Map || value instanceof Set) {
|
36
|
+
return value.size === 0;
|
37
|
+
}
|
38
|
+
return value === undefined || value === null;
|
39
|
+
}
|
40
|
+
}
|
41
|
+
exports.CloudWatchEventFormatter = CloudWatchEventFormatter;
|
@@ -1,12 +1,13 @@
|
|
1
1
|
import { ILogger } from './interface';
|
2
2
|
export declare class CloudWatchLoggerService implements ILogger {
|
3
|
-
private
|
3
|
+
private readonly cloudwatch;
|
4
4
|
private region;
|
5
5
|
private logGroupName;
|
6
6
|
constructor(region: string, logGroupName: string);
|
7
|
-
private
|
8
|
-
|
9
|
-
|
7
|
+
private version;
|
8
|
+
createLogStream(logStreamName: string): Promise<any>;
|
9
|
+
logToCloudWatch(type: string, logObject: any, logStreamName: string): Promise<void>;
|
10
|
+
logStreamExists(logStreamName: string): Promise<boolean>;
|
10
11
|
log(requestedId: string, message: any, event: string, statusCode: number, details: any, brand: any, logStreamName: string): Promise<any>;
|
11
12
|
error(requestedId: string, message: any, event: string, errorCode: string, statusCode: number, details: any, brand: any, logStreamName: string): Promise<any>;
|
12
13
|
}
|
@@ -11,40 +11,68 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
12
|
exports.CloudWatchLoggerService = void 0;
|
13
13
|
const common_1 = require("@nestjs/common");
|
14
|
-
const
|
15
|
-
const
|
16
|
-
const AWS = require("aws-sdk");
|
14
|
+
const aws_sdk_1 = require("aws-sdk");
|
15
|
+
const cloudwatch_event_formatter_1 = require("./cloudwatch.event.formatter");
|
17
16
|
let CloudWatchLoggerService = exports.CloudWatchLoggerService = class CloudWatchLoggerService {
|
18
17
|
constructor(region, logGroupName) {
|
19
|
-
this.
|
18
|
+
this.version = 'v1';
|
19
|
+
this.cloudwatch = new aws_sdk_1.CloudWatchLogs({ region: region });
|
20
20
|
this.region = region;
|
21
21
|
this.logGroupName = logGroupName;
|
22
|
-
AWS.config.update({ region: this.region });
|
23
|
-
this.logger = winston.createLogger({
|
24
|
-
format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
|
25
|
-
transports: [new winston.transports.Console()],
|
26
|
-
});
|
27
22
|
}
|
28
|
-
async
|
29
|
-
|
23
|
+
async createLogStream(logStreamName) {
|
24
|
+
const createParams = {
|
30
25
|
logGroupName: this.logGroupName,
|
31
|
-
logStreamName,
|
32
|
-
|
33
|
-
|
34
|
-
level: 'info',
|
35
|
-
});
|
26
|
+
logStreamName: logStreamName,
|
27
|
+
};
|
28
|
+
await this.cloudwatch.createLogStream(createParams).promise();
|
36
29
|
}
|
37
|
-
async logToCloudWatch(
|
38
|
-
this.
|
30
|
+
async logToCloudWatch(type, logObject, logStreamName) {
|
31
|
+
if (!(await this.logStreamExists(logStreamName))) {
|
32
|
+
await this.createLogStream(logStreamName);
|
33
|
+
}
|
34
|
+
const formatter = new cloudwatch_event_formatter_1.CloudWatchEventFormatter();
|
35
|
+
logObject.version = this.version;
|
36
|
+
logObject.timestamp = new Date().toISOString();
|
37
|
+
const formattedLog = formatter.formatLogItem({
|
38
|
+
meta: logObject,
|
39
39
|
...logObject,
|
40
|
-
|
41
|
-
|
40
|
+
level: type,
|
41
|
+
date: new Date(),
|
42
42
|
});
|
43
|
+
const timestampMillis = formattedLog.timestamp.getTime();
|
44
|
+
const putParams = {
|
45
|
+
logGroupName: this.logGroupName,
|
46
|
+
logStreamName: logStreamName,
|
47
|
+
logEvents: [{
|
48
|
+
message: formattedLog.message,
|
49
|
+
timestamp: timestampMillis
|
50
|
+
}],
|
51
|
+
};
|
52
|
+
try {
|
53
|
+
await this.cloudwatch.putLogEvents(putParams).promise();
|
54
|
+
}
|
55
|
+
catch (error) {
|
56
|
+
console.error('Error putting log events:', error);
|
57
|
+
throw error;
|
58
|
+
}
|
59
|
+
}
|
60
|
+
async logStreamExists(logStreamName) {
|
61
|
+
try {
|
62
|
+
const response = await this.cloudwatch
|
63
|
+
.describeLogStreams({
|
64
|
+
logGroupName: this.logGroupName,
|
65
|
+
logStreamNamePrefix: logStreamName,
|
66
|
+
})
|
67
|
+
.promise();
|
68
|
+
return response.logStreams && response.logStreams.length > 0;
|
69
|
+
}
|
70
|
+
catch (error) {
|
71
|
+
console.error('Error checking log stream:', error);
|
72
|
+
throw error;
|
73
|
+
}
|
43
74
|
}
|
44
75
|
async log(requestedId, message, event, statusCode, details, brand, logStreamName) {
|
45
|
-
console.log(this.region, this.logGroupName);
|
46
|
-
const transport = await this.createCloudWatchTransport(logStreamName);
|
47
|
-
await this.logger.add(transport);
|
48
76
|
const logObject = {
|
49
77
|
requestedId,
|
50
78
|
message,
|
@@ -54,11 +82,9 @@ let CloudWatchLoggerService = exports.CloudWatchLoggerService = class CloudWatch
|
|
54
82
|
details,
|
55
83
|
brand,
|
56
84
|
};
|
57
|
-
await this.logToCloudWatch('info', logObject);
|
85
|
+
await this.logToCloudWatch('info', logObject, logStreamName);
|
58
86
|
}
|
59
87
|
async error(requestedId, message, event, errorCode, statusCode, details, brand, logStreamName) {
|
60
|
-
const transport = await this.createCloudWatchTransport(logStreamName);
|
61
|
-
await this.logger.add(transport);
|
62
88
|
const logObject = {
|
63
89
|
requestedId,
|
64
90
|
message,
|
@@ -69,7 +95,7 @@ let CloudWatchLoggerService = exports.CloudWatchLoggerService = class CloudWatch
|
|
69
95
|
details,
|
70
96
|
brand,
|
71
97
|
};
|
72
|
-
await this.logToCloudWatch('error', logObject);
|
98
|
+
await this.logToCloudWatch('error', logObject, logStreamName);
|
73
99
|
}
|
74
100
|
};
|
75
101
|
exports.CloudWatchLoggerService = CloudWatchLoggerService = __decorate([
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/module.js
CHANGED
@@ -23,10 +23,7 @@ let LoggerModule = exports.LoggerModule = LoggerModule_1 = class LoggerModule {
|
|
23
23
|
return {
|
24
24
|
module: LoggerModule_1,
|
25
25
|
exports: [cloudwatchProvider, cloudwatchProviders],
|
26
|
-
providers: [
|
27
|
-
cloudwatchProvider,
|
28
|
-
cloudwatchProviders,
|
29
|
-
],
|
26
|
+
providers: [cloudwatchProvider, cloudwatchProviders],
|
30
27
|
};
|
31
28
|
}
|
32
29
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@gus-eip/loggers",
|
3
|
-
"version": "1.0
|
3
|
+
"version": "2.1.0",
|
4
4
|
"description": "@gus-eip/loggers is a package designed to provide logging functionality for your Node.js applications.",
|
5
5
|
"author": "gus",
|
6
6
|
"readmeFilename": "README.md",
|
@@ -25,20 +25,13 @@
|
|
25
25
|
"nodejs",
|
26
26
|
"javascript",
|
27
27
|
"typescript",
|
28
|
-
"
|
29
|
-
"eip-loggers",
|
30
|
-
"currency-exchange",
|
31
|
-
"conversion",
|
32
|
-
"money",
|
33
|
-
"exchange-rates"
|
28
|
+
"eip-loggers"
|
34
29
|
],
|
35
30
|
"publishConfig": {
|
36
31
|
"access": "public"
|
37
32
|
},
|
38
33
|
"dependencies": {
|
39
|
-
"aws-sdk": "^2.1590.0"
|
40
|
-
"winston": "^3.13.0",
|
41
|
-
"winston-aws-cloudwatch": "^3.0.0"
|
34
|
+
"aws-sdk": "^2.1590.0"
|
42
35
|
},
|
43
36
|
"devDependencies": {
|
44
37
|
"@nestjs/common": "^10.0.2",
|