Skip to content

Commit

Permalink
fix: log 없을시 에러처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixtar committed Dec 2, 2024
1 parent ec2bdbb commit 36a69b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
13 changes: 11 additions & 2 deletions apps/api/src/common/log/logger.batch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs/promises';
import path from 'path';

import { Injectable } from '@nestjs/common';
Expand All @@ -13,7 +14,8 @@ export class LogBatchService {
private readonly loggerService: LoggerService
) {}

@Cron(CronExpression.EVERY_DAY_AT_1AM)
// @Cron(CronExpression.EVERY_DAY_AT_1AM)
@Cron('10 * * * * *')
async uploadLogToObjectStorage() {
const logsDir = path.join(__dirname, '../../../logs');
const today = new Date();
Expand All @@ -23,11 +25,18 @@ export class LogBatchService {
const logFileName = `application-${today.toISOString().split('T')[0]}.log`;
const localFilePath = path.join(logsDir, logFileName);
try {
await fs.access(localFilePath);

const remoteFileName = `logs/${logFileName}`;
const result = await this.ncpService.uploadFile(localFilePath, remoteFileName);
this.loggerService.log(`Log file uploaded successfully: ${result}`, 'logBatchService');
} catch (error) {
this.loggerService.log(`Failed to upload log file: ${error}`, 'logBatchService');
const err = error as Error;
this.loggerService.error(
`Log file not found: ${localFilePath}`,
err.stack,
'logBatchService'
);
}
}
}
13 changes: 6 additions & 7 deletions apps/api/src/common/ncp/ncp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ export class NcpService {
const bucketName = this.configService.get<string>('NCP_OBJECT_STORAGE_BUCKET');
const endpoint = this.configService.get<string>('NCP_OBJECT_STORAGE_ENDPOINT');

const fileStream = fs.createReadStream(localFilePath);
const params = {
Bucket: bucketName,
Key: remoteFileName,
Body: fileStream,
};

try {
const fileStream = fs.createReadStream(localFilePath);
const params = {
Bucket: bucketName,
Key: remoteFileName,
Body: fileStream,
};
const uploadResponse = await this.s3.send(new PutObjectCommand(params));

Check warning on line 32 in apps/api/src/common/ncp/ncp.service.ts

View workflow job for this annotation

GitHub Actions / check

'uploadResponse' is assigned a value but never used
const url = `${endpoint}/${bucketName}/${remoteFileName}`;

Check warning on line 33 in apps/api/src/common/ncp/ncp.service.ts

View workflow job for this annotation

GitHub Actions / check

'url' is assigned a value but never used
return remoteFileName;
Expand Down

0 comments on commit 36a69b4

Please sign in to comment.