Debug writes a message to debug.log file in the configured directory
(format string, args ...any)
| 36 | |
| 37 | // Debug writes a message to debug.log file in the configured directory |
| 38 | func Debug(format string, args ...any) { |
| 39 | // Internal fast path check without lock |
| 40 | val := logsDir.Load() |
| 41 | if val == nil { |
| 42 | return |
| 43 | } |
| 44 | dir := val.(string) |
| 45 | if dir == "" { |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | // Calculate timestamp only if we are actually logging |
| 50 | timestamp := time.Now().Format("2006-01-02 15:04:05") |
| 51 | |
| 52 | // Ensure file is open (still needs once, but fast after first time) |
| 53 | debugOnce.Do(func() { |
| 54 | _ = os.MkdirAll(dir, 0o755) |
| 55 | debugFile, _ = os.Create(filepath.Join(dir, fmt.Sprintf("debug-%s.log", time.Now().Format("20060102-150405")))) |
| 56 | }) |
| 57 | |
| 58 | if debugFile != nil { |
| 59 | _, _ = fmt.Fprintf(debugFile, "[%s] %s\n", timestamp, fmt.Sprintf(format, args...)) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // CleanupLogs removes old log files, keeping only the most recent retentionCount files |
| 64 | func CleanupLogs(retentionCount int) { |
no outgoing calls