txnbot - rewrite and lint (#53)
resolves #46 Co-authored-by: johba <johba@harb.eth> Reviewed-on: https://codeberg.org/johba/harb/pulls/53
This commit is contained in:
parent
dc61771dfc
commit
514be62cbb
19 changed files with 736 additions and 429 deletions
56
services/txnBot/src/logger.ts
Normal file
56
services/txnBot/src/logger.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
type LogLevel = 'info' | 'warn' | 'error';
|
||||
|
||||
interface LogEntry {
|
||||
timestamp: string;
|
||||
level: LogLevel;
|
||||
message: string;
|
||||
data?: unknown;
|
||||
}
|
||||
|
||||
export class Logger {
|
||||
private logFilePath: string;
|
||||
|
||||
constructor(logFileName = 'txnbot.log') {
|
||||
this.logFilePath = path.resolve(__dirname, '..', logFileName);
|
||||
}
|
||||
|
||||
private writeLog(level: LogLevel, message: string, data?: unknown): void {
|
||||
const logEntry: LogEntry = {
|
||||
timestamp: new Date().toISOString(),
|
||||
level,
|
||||
message,
|
||||
...(data !== undefined && { data }),
|
||||
};
|
||||
|
||||
const logLine = JSON.stringify(logEntry) + '\n';
|
||||
|
||||
try {
|
||||
fs.appendFileSync(this.logFilePath, logLine, 'utf8');
|
||||
} catch (error) {
|
||||
// Fallback to stderr if file write fails
|
||||
process.stderr.write(`Failed to write log: ${error}\n`);
|
||||
process.stderr.write(logLine);
|
||||
}
|
||||
}
|
||||
|
||||
info(message: string, data?: unknown): void {
|
||||
this.writeLog('info', message, data);
|
||||
}
|
||||
|
||||
warn(message: string, data?: unknown): void {
|
||||
this.writeLog('warn', message, data);
|
||||
}
|
||||
|
||||
error(message: string, data?: unknown): void {
|
||||
this.writeLog('error', message, data);
|
||||
}
|
||||
}
|
||||
|
||||
export const logger = new Logger();
|
||||
Loading…
Add table
Add a link
Reference in a new issue