refactor: migrate to winston and enable daily rotated app logs (#71)
This commit is contained in:
17 files changed
+414
-92
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { logger } from '../../utils/logger';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
@@ -12,40 +12,65 @@ vi.mock('../../utils/paths', async () => {
|
||||
|
||||
describe('Logger Utilities', () => {
|
||||
const testLogDir = path.join(process.cwd(), 'temp_test_logs');
|
||||
const testLogFile = path.join(testLogDir, 'app.log');
|
||||
|
||||
const getLatestLogFile = () => {
|
||||
const files = fs
|
||||
.readdirSync(testLogDir)
|
||||
.filter((file) => /^app-\d{4}-\d{2}-\d{2}(\.\d+)?\.log$/.test(file))
|
||||
.sort();
|
||||
|
||||
if (files.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return path.join(testLogDir, files[files.length - 1]);
|
||||
};
|
||||
|
||||
const waitForLogContains = async (text: string) => {
|
||||
for (let i = 0; i < 20; i++) {
|
||||
const filePath = getLatestLogFile();
|
||||
if (filePath && fs.existsSync(filePath)) {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
if (content.includes(text)) {
|
||||
return filePath;
|
||||
}
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
console.log('Test Log Dir:', testLogDir);
|
||||
if (fs.existsSync(testLogDir)) {
|
||||
fs.rmSync(testLogDir, { recursive: true, force: true });
|
||||
}
|
||||
fs.mkdirSync(testLogDir, { recursive: true });
|
||||
console.log('Dir created:', fs.existsSync(testLogDir));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (fs.existsSync(testLogDir)) {
|
||||
fs.rmSync(testLogDir, { recursive: true, force: true });
|
||||
if (!fs.existsSync(testLogDir)) {
|
||||
fs.mkdirSync(testLogDir, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('should create log file if it does not exist', () => {
|
||||
logger.info('Test message');
|
||||
expect(fs.existsSync(testLogFile)).toBe(true);
|
||||
it('should create rotated log file', async () => {
|
||||
const message = 'Test message';
|
||||
logger.info(message);
|
||||
const filePath = await waitForLogContains(message);
|
||||
expect(filePath).not.toBeNull();
|
||||
expect(fs.existsSync(filePath as string)).toBe(true);
|
||||
});
|
||||
|
||||
it('should write formatted message to file', () => {
|
||||
it('should write formatted message to file', async () => {
|
||||
const message = 'Test info message';
|
||||
logger.info(message);
|
||||
const content = fs.readFileSync(testLogFile, 'utf-8');
|
||||
const filePath = await waitForLogContains(message);
|
||||
expect(filePath).not.toBeNull();
|
||||
const content = fs.readFileSync(filePath as string, 'utf-8');
|
||||
expect(content).toContain('[INFO]');
|
||||
expect(content).toContain(message);
|
||||
});
|
||||
|
||||
it('should log error messages', () => {
|
||||
it('should log error messages', async () => {
|
||||
const message = 'Test error message';
|
||||
logger.error(message);
|
||||
const content = fs.readFileSync(testLogFile, 'utf-8');
|
||||
const filePath = await waitForLogContains(message);
|
||||
expect(filePath).not.toBeNull();
|
||||
const content = fs.readFileSync(filePath as string, 'utf-8');
|
||||
expect(content).toContain('[ERROR]');
|
||||
expect(content).toContain(message);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user