forked from zelenkovsky/opencode-reminders
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
69 lines (59 loc) · 1.65 KB
/
Copy pathlogger.ts
File metadata and controls
69 lines (59 loc) · 1.65 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { appendFileSync, mkdirSync } from "node:fs"
import { join } from "node:path"
import { homedir } from "node:os"
const LOG_DIR = 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 {
const env = process.env.REMINDERS_LOG_LEVEL?.toLowerCase()
switch (env) {
case "debug":
return LogLevel.DEBUG
case "info":
return LogLevel.INFO
case "error":
return LogLevel.ERROR
default:
return LogLevel.INFO
}
}
function writeLog(level: LogLevel, message: string, args: any[]): void {
const currentLevel = getCurrentLevel()
if (level < currentLevel) {
return
}
const timestamp = new Date().toISOString()
const levelName = LOG_LEVEL_NAMES[level]
const formattedArgs = args.length > 0
? ` ${args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(' ')}`
: ""
const formatted = `${timestamp} [${levelName}] ${message}${formattedArgs}\n`
try {
appendFileSync(LOG_FILE, formatted, "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)
},
}