From 36a69b48802d7aefa51f60893ab82b34a2f918fb Mon Sep 17 00:00:00 2001 From: seongha <11pi885@gmail.com> Date: Tue, 3 Dec 2024 04:07:45 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20log=20=EC=97=86=EC=9D=84=EC=8B=9C=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/src/common/log/logger.batch.ts | 13 +++++++++++-- apps/api/src/common/ncp/ncp.service.ts | 13 ++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/apps/api/src/common/log/logger.batch.ts b/apps/api/src/common/log/logger.batch.ts index 577a628a..b03d7a4a 100644 --- a/apps/api/src/common/log/logger.batch.ts +++ b/apps/api/src/common/log/logger.batch.ts @@ -1,3 +1,4 @@ +import fs from 'fs/promises'; import path from 'path'; import { Injectable } from '@nestjs/common'; @@ -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(); @@ -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' + ); } } } diff --git a/apps/api/src/common/ncp/ncp.service.ts b/apps/api/src/common/ncp/ncp.service.ts index f428820e..7ad89abe 100644 --- a/apps/api/src/common/ncp/ncp.service.ts +++ b/apps/api/src/common/ncp/ncp.service.ts @@ -22,14 +22,13 @@ export class NcpService { const bucketName = this.configService.get('NCP_OBJECT_STORAGE_BUCKET'); const endpoint = this.configService.get('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)); const url = `${endpoint}/${bucketName}/${remoteFileName}`; return remoteFileName;