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;