File size: 1,283 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**

 * Plugin logger — per-plugin log isolation.

 *

 * Writes JSON log entries to <pluginDir>/<name>/plugin.log.

 *

 * @module plugins/logger

 */

import { appendFileSync, mkdirSync } from "fs";
import { join, dirname } from "path";

export class PluginLogger {
  private logPath: string;

  constructor(pluginName: string, pluginDir: string) {
    this.logPath = join(pluginDir, pluginName, "plugin.log");
  }

  private write(level: string, message: string, data?: unknown): void {
    const entry = JSON.stringify({
      timestamp: new Date().toISOString(),
      level,
      message,
      ...(data !== undefined ? { data } : {}),
    });

    try {
      mkdirSync(dirname(this.logPath), { recursive: true });
      appendFileSync(this.logPath, entry + "\n", "utf-8");
    } catch {
      // Silent fail — don't crash plugin over logging
    }
  }

  info(message: string, data?: unknown): void {
    this.write("INFO", message, data);
  }

  error(message: string, data?: unknown): void {
    this.write("ERROR", message, data);
  }

  warn(message: string, data?: unknown): void {
    this.write("WARN", message, data);
  }

  debug(message: string, data?: unknown): void {
    this.write("DEBUG", message, data);
  }
}