Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 20 additions & 82 deletions src/module/cloudwatch-logger/cloudwatch-logger.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, LoggerService } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import * as AWS from "aws-sdk";
import { RequestContextService } from "src/middleware/request-context.service";
import { Injectable, LoggerService } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as AWS from 'aws-sdk';
import { RequestContextService } from 'src/middleware/request-context.service';

@Injectable()
export class CloudWatchLoggerService implements LoggerService {
Expand All @@ -12,7 +12,7 @@ export class CloudWatchLoggerService implements LoggerService {
constructor(
private readonly requestContextService: RequestContextService,

private configService: ConfigService
private configService: ConfigService,
) {
// Initialize AWS CloudWatch Logs
AWS.config.update({ region: this.configService.get<string>('aws.region') }); // Set your AWS region
Expand All @@ -24,96 +24,34 @@ export class CloudWatchLoggerService implements LoggerService {
this.logStreamName = this.configService.get<string>('aws.logStreamName');

// Ensure the log group and stream exist
this.ensureLogGroupAndStream();
}

private async ensureLogGroupAndStream() {
try {
// Check if the log group exists
const logGroups = await this.cloudWatchLogs
.describeLogGroups({ logGroupNamePrefix: this.logGroupName })
.promise();
if (
!(
logGroups.logGroups &&
logGroups.logGroups.find(
(group) => group.logGroupName === this.logGroupName
)
)
) {
await this.cloudWatchLogs
.createLogGroup({ logGroupName: this.logGroupName })
.promise();
}

// Check if the log stream exists
const logStreams = await this.cloudWatchLogs
.describeLogStreams({
logGroupName: this.logGroupName,
logStreamNamePrefix: this.logStreamName,
})
.promise();
if (
!(
logStreams.logStreams &&
logStreams.logStreams.find(
(stream) => stream.logStreamName === this.logStreamName
)
)
) {
await this.cloudWatchLogs
.createLogStream({
logGroupName: this.logGroupName,
logStreamName: this.logStreamName,
})
.promise();
}
} catch (error) {
console.error("Error creating log group or stream:", error);
}
}

private async putLogEvents(message: string) {
const params = {
logEvents: [
{
message,
timestamp: Date.now(),
},
],
logGroupName: this.logGroupName,
logStreamName: this.logStreamName,
};

try {
const sequenceToken = await this.getSequenceToken();
await this.cloudWatchLogs
.putLogEvents({
logGroupName: this.logGroupName,
logStreamName: this.logStreamName,
logEvents: [
{
message,
timestamp: Date.now(),
},
],
sequenceToken,
})
.promise();
await this.cloudWatchLogs?.putLogEvents(params).promise();
} catch (error) {
console.error("Error sending log event to CloudWatch:", error);
console.error('Error putting log events to CloudWatch:', error);
}
}

private async getSequenceToken(): Promise<string | undefined> {
const logStreams = await this.cloudWatchLogs
.describeLogStreams({
logGroupName: this.logGroupName,
logStreamNamePrefix: this.logStreamName,
})
.promise();
const logStream = logStreams.logStreams?.find(
(stream) => stream.logStreamName === this.logStreamName
);
return logStream?.uploadSequenceToken;
}

log(message: string, context: any) {
const contxtConvert = JSON.stringify(context);
const timestamp = new Date().toISOString();
try {
const requestId = this.requestContextService.getRequestId();
this.putLogEvents(
`${timestamp} [Log]: ${message}\nrequestId: ${requestId}\ncontext: ${contxtConvert}`
`${timestamp} [Log]: ${message}\nrequestId: ${requestId}\ncontext: ${contxtConvert}`,
);
} catch (e) {
console.log(e);
Expand All @@ -124,7 +62,7 @@ export class CloudWatchLoggerService implements LoggerService {
const contxtConvert = JSON.stringify(context);
const timestamp = new Date().toISOString();
this.putLogEvents(
`[ERROR]: ${message}\ncontext: ${contxtConvert}\nTrace: ${trace}`
`[ERROR]: ${message}\ncontext: ${contxtConvert}\nTrace: ${trace}`,
);
}

Expand Down