From 25e90251053fc3a42e015907f47094f4c48ddd74 Mon Sep 17 00:00:00 2001 From: jango-blockchained Date: Mon, 3 Feb 2025 18:52:14 +0100 Subject: [PATCH] refactor: modernize log rotation utility with improved error handling and glob import - Updated glob import to use modern ES module syntax - Simplified log file retrieval and cleanup methods - Added error logging for log rotation interval tasks - Removed deprecated callback-based glob usage - Improved type safety and error handling in log rotation functions --- src/utils/log-rotation.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/utils/log-rotation.ts b/src/utils/log-rotation.ts index d18a921..781ae45 100644 --- a/src/utils/log-rotation.ts +++ b/src/utils/log-rotation.ts @@ -9,7 +9,7 @@ import fs from 'fs/promises'; import path from 'path'; -import glob from 'glob'; +import { glob } from 'glob'; import { logger } from './logger.js'; import { APP_CONFIG } from '../config/app.config.js'; import { unlink } from 'fs/promises'; @@ -73,7 +73,7 @@ const parseDuration = (duration: string): number => { */ const getLogFiles = async (): Promise => { const logDir = APP_CONFIG.LOGGING.DIR; - const files = await globPromise('*.log*', { cwd: logDir }); + const files: string[] = await glob('*.log*', { cwd: logDir }); const fileInfos: LogFileInfo[] = []; for (const file of files) { @@ -99,12 +99,7 @@ const getLogFiles = async (): Promise => { */ export async function cleanupOldLogs(logDir: string, maxDays: number): Promise { try { - const files = await new Promise((resolve, reject) => { - glob('*.log*', { cwd: logDir }, (err, matches) => { - if (err) reject(err); - else resolve(matches); - }); - }); + const files: string[] = await glob('*.log*', { cwd: logDir }); const now = Date.now(); const maxAge = maxDays * 24 * 60 * 60 * 1000; @@ -151,10 +146,19 @@ const checkLogSize = async (): Promise => { */ export const initLogRotation = (): void => { // Check log sizes every hour - setInterval(checkLogSize, 60 * 60 * 1000); + setInterval(() => { + checkLogSize().catch(error => { + logger.error('Error checking log sizes:', error); + }); + }, 60 * 60 * 1000); // Clean up old logs daily - setInterval(cleanupOldLogs, 24 * 60 * 60 * 1000); + setInterval(() => { + cleanupOldLogs(APP_CONFIG.LOGGING.DIR, parseDuration(APP_CONFIG.LOGGING.MAX_DAYS)) + .catch(error => { + logger.error('Error cleaning up old logs:', error); + }); + }, 24 * 60 * 60 * 1000); // Initial check checkLogSize().catch(error => { @@ -162,9 +166,10 @@ export const initLogRotation = (): void => { }); // Initial cleanup - cleanupOldLogs(APP_CONFIG.LOGGING.DIR, parseDuration(APP_CONFIG.LOGGING.MAX_DAYS)).catch(error => { - logger.error('Error in initial log cleanup:', error); - }); + cleanupOldLogs(APP_CONFIG.LOGGING.DIR, parseDuration(APP_CONFIG.LOGGING.MAX_DAYS)) + .catch(error => { + logger.error('Error in initial log cleanup:', error); + }); logger.info('Log rotation initialized'); }; \ No newline at end of file