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
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import glob from 'glob';
|
import { glob } from 'glob';
|
||||||
import { logger } from './logger.js';
|
import { logger } from './logger.js';
|
||||||
import { APP_CONFIG } from '../config/app.config.js';
|
import { APP_CONFIG } from '../config/app.config.js';
|
||||||
import { unlink } from 'fs/promises';
|
import { unlink } from 'fs/promises';
|
||||||
@@ -73,7 +73,7 @@ const parseDuration = (duration: string): number => {
|
|||||||
*/
|
*/
|
||||||
const getLogFiles = async (): Promise<LogFileInfo[]> => {
|
const getLogFiles = async (): Promise<LogFileInfo[]> => {
|
||||||
const logDir = APP_CONFIG.LOGGING.DIR;
|
const logDir = APP_CONFIG.LOGGING.DIR;
|
||||||
const files = await globPromise('*.log*', { cwd: logDir });
|
const files: string[] = await glob('*.log*', { cwd: logDir });
|
||||||
|
|
||||||
const fileInfos: LogFileInfo[] = [];
|
const fileInfos: LogFileInfo[] = [];
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
@@ -99,12 +99,7 @@ const getLogFiles = async (): Promise<LogFileInfo[]> => {
|
|||||||
*/
|
*/
|
||||||
export async function cleanupOldLogs(logDir: string, maxDays: number): Promise<void> {
|
export async function cleanupOldLogs(logDir: string, maxDays: number): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const files = await new Promise<string[]>((resolve, reject) => {
|
const files: string[] = await glob('*.log*', { cwd: logDir });
|
||||||
glob('*.log*', { cwd: logDir }, (err, matches) => {
|
|
||||||
if (err) reject(err);
|
|
||||||
else resolve(matches);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const maxAge = maxDays * 24 * 60 * 60 * 1000;
|
const maxAge = maxDays * 24 * 60 * 60 * 1000;
|
||||||
@@ -151,10 +146,19 @@ const checkLogSize = async (): Promise<void> => {
|
|||||||
*/
|
*/
|
||||||
export const initLogRotation = (): void => {
|
export const initLogRotation = (): void => {
|
||||||
// Check log sizes every hour
|
// 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
|
// 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
|
// Initial check
|
||||||
checkLogSize().catch(error => {
|
checkLogSize().catch(error => {
|
||||||
@@ -162,9 +166,10 @@ export const initLogRotation = (): void => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Initial cleanup
|
// Initial cleanup
|
||||||
cleanupOldLogs(APP_CONFIG.LOGGING.DIR, parseDuration(APP_CONFIG.LOGGING.MAX_DAYS)).catch(error => {
|
cleanupOldLogs(APP_CONFIG.LOGGING.DIR, parseDuration(APP_CONFIG.LOGGING.MAX_DAYS))
|
||||||
logger.error('Error in initial log cleanup:', error);
|
.catch(error => {
|
||||||
});
|
logger.error('Error in initial log cleanup:', error);
|
||||||
|
});
|
||||||
|
|
||||||
logger.info('Log rotation initialized');
|
logger.info('Log rotation initialized');
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user