|
|
@@ -8,12 +8,15 @@
|
|
|
export class Logger {
|
|
|
private prefix: string;
|
|
|
private isSystemd: boolean;
|
|
|
+ private verboseHttp: boolean;
|
|
|
|
|
|
constructor(prefix: string = 'WebshopScraper') {
|
|
|
this.prefix = prefix;
|
|
|
// Detect if running under systemd by checking for INVOCATION_ID
|
|
|
// This environment variable is set by systemd for each service invocation
|
|
|
this.isSystemd = !!process.env.INVOCATION_ID;
|
|
|
+ // Enable verbose HTTP logging if LOG_HTTP_VERBOSE is set
|
|
|
+ this.verboseHttp = process.env.LOG_HTTP_VERBOSE === 'true';
|
|
|
}
|
|
|
|
|
|
private formatMessage(level: string, message: string, ...args: any[]): string {
|
|
|
@@ -44,6 +47,46 @@ export class Logger {
|
|
|
debug(message: string, ...args: any[]): void {
|
|
|
console.log(this.formatMessage('DEBUG', message, ...args));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Log HTTP access in a structured format
|
|
|
+ * Can be used for metrics and monitoring
|
|
|
+ */
|
|
|
+ http(data: {
|
|
|
+ method: string;
|
|
|
+ path: string;
|
|
|
+ statusCode: number;
|
|
|
+ ip: string;
|
|
|
+ responseTime: number;
|
|
|
+ contentLength?: string | number;
|
|
|
+ userAgent?: string;
|
|
|
+ }): void {
|
|
|
+ const { method, path, statusCode, ip, responseTime, contentLength, userAgent } = data;
|
|
|
+
|
|
|
+ // Color code based on status
|
|
|
+ const statusEmoji =
|
|
|
+ statusCode >= 500 ? '❌' :
|
|
|
+ statusCode >= 400 ? '⚠️' :
|
|
|
+ statusCode >= 300 ? '↪️' :
|
|
|
+ statusCode >= 200 ? '✓' : '→';
|
|
|
+
|
|
|
+ // Basic format (always logged)
|
|
|
+ const basicLog = `${statusEmoji} ${statusCode} ${method} ${path} ${ip} ${responseTime}ms`;
|
|
|
+
|
|
|
+ // Verbose format (includes user-agent and content-length)
|
|
|
+ const verboseLog = this.verboseHttp
|
|
|
+ ? ` ${contentLength || '-'}b UA:"${userAgent || '-'}"`
|
|
|
+ : '';
|
|
|
+
|
|
|
+ this.info(`${basicLog}${verboseLog}`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check if verbose HTTP logging is enabled
|
|
|
+ */
|
|
|
+ isVerboseHttp(): boolean {
|
|
|
+ return this.verboseHttp;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export const logger = new Logger();
|