mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
5a3629badf
Added the ability to view and re-download leave application forms in the Employee History page (Leave, Permit, and Sick tabs) Added employee alpha stats and employee alpha detail in Employee Performance Page Added total WFO, WFH and WFS in Admin Summary Report Added batch import from Excel files in Admin Manual Attendance
102 lines
3.6 KiB
TypeScript
102 lines
3.6 KiB
TypeScript
// lib/logger.ts
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
export const writeLog = (type: 'REQUEST' | 'RESPONSE' | 'ERROR', title: string, data: any) => {
|
|
const logProcess = async () => {
|
|
try {
|
|
const now = new Date();
|
|
const dateStr = now.toISOString().split('T')[0];
|
|
const fileName = `${dateStr}.log`;
|
|
const logDir = path.join(process.cwd(), 'logs');
|
|
|
|
// Check folder exists (async version is complex, sync check is tiny and runs once)
|
|
if (!fs.existsSync(logDir)) {
|
|
fs.mkdirSync(logDir);
|
|
}
|
|
|
|
const timestamp = now.toISOString();
|
|
// Safe stringify to prevent circular reference crashes
|
|
const dataString = JSON.stringify(data, getCircularReplacer(), 2);
|
|
|
|
const logEntry = `[${timestamp}] [${type}] ${title}\n${dataString}\n${'-'.repeat(50)}\n`;
|
|
|
|
// Async append - we don't wait for this result
|
|
fs.appendFile(path.join(logDir, fileName), logEntry, (err) => {
|
|
if (err) console.error('Writing log failed:', err);
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Logger internal error:', error);
|
|
}
|
|
};
|
|
|
|
// Trigger execution immediately but don't return a promise to the caller
|
|
logProcess();
|
|
};
|
|
|
|
|
|
// Added standard log levels alongside our custom API ones
|
|
type LogLevel = 'INFO' | 'WARN' | 'ERROR' | 'DEBUG' | 'API_REQ' | 'API_RES';
|
|
|
|
// Helper to handle circular references safely
|
|
const getCircularReplacer = () => {
|
|
const seen = new WeakSet();
|
|
return (key: any, value: any) => {
|
|
if (typeof value === "object" && value !== null) {
|
|
if (seen.has(value)) return "[Circular]";
|
|
seen.add(value);
|
|
}
|
|
return value;
|
|
};
|
|
};
|
|
|
|
// Core write function (Runs in the background)
|
|
const writeToFile = (level: LogLevel, message: string, data?: any) => {
|
|
const logProcess = async () => {
|
|
try {
|
|
const now = new Date();
|
|
const dateStr = now.toISOString().split('T')[0];
|
|
const fileName = `${dateStr}.log`;
|
|
const logDir = path.join(process.cwd(), 'logs');
|
|
|
|
// Check folder exists
|
|
if (!fs.existsSync(logDir)) {
|
|
fs.mkdirSync(logDir);
|
|
}
|
|
|
|
const timestamp = now.toISOString();
|
|
|
|
// Only stringify data if it was actually provided
|
|
let dataString = '';
|
|
if (data !== undefined) {
|
|
dataString = typeof data === 'object'
|
|
? `\n${JSON.stringify(data, getCircularReplacer(), 2)}`
|
|
: `\n${data}`;
|
|
}
|
|
|
|
const logEntry = `[${timestamp}] [${level}] ${message}${dataString}\n${'-'.repeat(50)}\n`;
|
|
|
|
fs.appendFile(path.join(logDir, fileName), logEntry, (err) => {
|
|
if (err) console.error('Writing log failed:', err);
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Logger internal error:', error);
|
|
}
|
|
};
|
|
|
|
logProcess(); // Fire and forget
|
|
};
|
|
|
|
// 🌟 Exported Logger Object for easy use anywhere!
|
|
export const logger = {
|
|
info: (message: string, data?: any) => writeToFile('INFO', message, data),
|
|
warn: (message: string, data?: any) => writeToFile('WARN', message, data),
|
|
error: (message: string, data?: any) => writeToFile('ERROR', message, data),
|
|
debug: (message: string, data?: any) => writeToFile('DEBUG', message, data),
|
|
|
|
// Specific methods for your API helper to keep things organized
|
|
apiReq: (message: string, data?: any) => writeToFile('API_REQ', message, data),
|
|
apiRes: (message: string, data?: any) => writeToFile('API_RES', message, data),
|
|
}; |