Repository navigation
Expand file tree
/
Copy pathlogger.ts
More file actions
48 lines (41 loc) · 1.45 KB
/
Copy pathlogger.ts
File metadata and controls
48 lines (41 loc) · 1.45 KB
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
import { appendFileSync, mkdirSync } from "node:fs"
import { join } from "node:path"
import { homedir } from "node:os"
const LOG_DIR = join(process.env.XDG_DATA_HOME || join(homedir(), ".local/share"), "opencode/log")
const LOG_FILE = join(LOG_DIR, "reminders.log")
try {
mkdirSync(LOG_DIR, { recursive: true })
} catch (error) {}
enum LogLevel {
DEBUG = 0,
INFO = 1,
ERROR = 2,
}
const LOG_LEVEL_NAMES: Record<LogLevel, string> = {
[LogLevel.DEBUG]: "DEBUG",
[LogLevel.INFO]: "INFO",
[LogLevel.ERROR]: "ERROR",
}
function getCurrentLevel(): LogLevel {
switch (process.env.REMINDERS_LOG_LEVEL?.toLowerCase()) {
case "debug":
return LogLevel.DEBUG
case "error":
return LogLevel.ERROR
default:
return LogLevel.INFO
}
}
function writeLog(level: LogLevel, message: string, args: any[]): void {
if (level < getCurrentLevel()) return
const formattedArgs =
args.length > 0 ? ` ${args.map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a))).join(" ")}` : ""
try {
appendFileSync(LOG_FILE, `${new Date().toISOString()} [${LOG_LEVEL_NAMES[level]}] ${message}${formattedArgs}\n`, "utf-8")
} catch (error) {}
}
export const logger = {
debug: (message: string, ...args: any[]): void => writeLog(LogLevel.DEBUG, message, args),
info: (message: string, ...args: any[]): void => writeLog(LogLevel.INFO, message, args),
error: (message: string, ...args: any[]): void => writeLog(LogLevel.ERROR, message, args),
}