|
1 | 1 | package logging |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "io" |
4 | 6 | "log" |
5 | 7 | "os" |
| 8 | + "time" |
6 | 9 | ) |
7 | 10 |
|
8 | | -// Log levels (higher number = more verbose) |
| 11 | +// ANSI color codes |
9 | 12 | const ( |
10 | | - LevelError = iota // 0 |
11 | | - LevelWarn // 1 |
12 | | - LevelInfo // 2 |
13 | | - LevelDebug // 3 |
| 13 | + colorReset = "\033[0m" |
| 14 | + colorRed = "\033[31m" |
| 15 | + colorYellow = "\033[33m" |
| 16 | + colorBlue = "\033[34m" |
| 17 | + colorCyan = "\033[36m" |
| 18 | + colorGray = "\033[90m" |
| 19 | + colorWhite = "\033[97m" |
| 20 | +) |
| 21 | + |
| 22 | +// Log levels |
| 23 | +const ( |
| 24 | + LevelCrit = iota // 0 - Critical errors (fatal, app should stop) |
| 25 | + LevelError // 1 - Errors (non-fatal but important) |
| 26 | + LevelWarn // 2 - Warnings |
| 27 | + LevelNotice // 3 - Important info (startup, shutdown, config) |
| 28 | + LevelInfo // 4 - General info |
| 29 | + LevelDebug // 5 - Debug details |
14 | 30 | ) |
15 | 31 |
|
16 | 32 | var ( |
17 | 33 | // Logger is the package-level logger used across the project. |
18 | | - Logger = log.New(os.Stdout, "", log.LstdFlags) |
19 | | - // Level controls verbosity. Default to WARN for sane production defaults. |
20 | | - Level = LevelWarn |
| 34 | + Logger = log.New(os.Stdout, "", 0) // We'll handle our own formatting |
| 35 | + // Level controls verbosity. Default to NOTICE for sane production defaults. |
| 36 | + Level = LevelNotice |
| 37 | + UseColors = true // Enable colors by default |
| 38 | + TimeFormat = "Jan 02 15:04:05.000" // Kyle's format: "Oct 14 13:16:37.788" |
21 | 39 | ) |
22 | 40 |
|
23 | 41 | // SetLevel sets the logger verbosity level. |
24 | 42 | func SetLevel(l int) { |
25 | 43 | Level = l |
26 | 44 | } |
27 | 45 |
|
28 | | -// Error logs error-level messages (always important). |
| 46 | +// SetOutput sets the output destination for logs |
| 47 | +func SetOutput(w io.Writer) { |
| 48 | + Logger.SetOutput(w) |
| 49 | +} |
| 50 | + |
| 51 | +// DisableColors disables color output |
| 52 | +func DisableColors() { |
| 53 | + UseColors = false |
| 54 | +} |
| 55 | + |
| 56 | +// formatLog formats a log message with timestamp, colored level (3-letter), and message |
| 57 | +func formatLog(levelAbbrev, color, message string) string { |
| 58 | + timestamp := time.Now().Format(TimeFormat) |
| 59 | + |
| 60 | + if UseColors { |
| 61 | + // Format: "Oct 14 13:16:37.788 INF message" |
| 62 | + return fmt.Sprintf("%s %s%s%s %s", timestamp, color, levelAbbrev, colorReset, message) |
| 63 | + } |
| 64 | + |
| 65 | + // No colors |
| 66 | + return fmt.Sprintf("%s %s %s", timestamp, levelAbbrev, message) |
| 67 | +} |
| 68 | + |
| 69 | +// Crit logs critical errors (application should stop) |
| 70 | +func Crit(format string, v ...interface{}) { |
| 71 | + if Level >= LevelCrit { |
| 72 | + msg := fmt.Sprintf(format, v...) |
| 73 | + Logger.Print(formatLog("CRT", colorRed, msg)) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Error logs error-level messages (non-fatal but important) |
29 | 78 | func Error(format string, v ...interface{}) { |
30 | 79 | if Level >= LevelError { |
31 | | - Logger.Printf("| ERROR | "+format, v...) |
| 80 | + msg := fmt.Sprintf(format, v...) |
| 81 | + Logger.Print(formatLog("ERR", colorRed, msg)) |
32 | 82 | } |
33 | 83 | } |
34 | 84 |
|
35 | | -// Warn logs warning-level messages. |
| 85 | +// Warn logs warning-level messages |
36 | 86 | func Warn(format string, v ...interface{}) { |
37 | 87 | if Level >= LevelWarn { |
38 | | - Logger.Printf("| WARN | "+format, v...) |
| 88 | + msg := fmt.Sprintf(format, v...) |
| 89 | + Logger.Print(formatLog("WRN", colorYellow, msg)) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Notice logs important informational messages (startup, config, shutdown) |
| 94 | +func Notice(format string, v ...interface{}) { |
| 95 | + if Level >= LevelNotice { |
| 96 | + msg := fmt.Sprintf(format, v...) |
| 97 | + Logger.Print(formatLog("NOT", colorCyan, msg)) |
39 | 98 | } |
40 | 99 | } |
41 | 100 |
|
42 | | -// Info logs informational messages (less noisy). |
| 101 | +// Info logs general informational messages |
43 | 102 | func Info(format string, v ...interface{}) { |
44 | 103 | if Level >= LevelInfo { |
45 | | - Logger.Printf("| INFO | "+format, v...) |
| 104 | + msg := fmt.Sprintf(format, v...) |
| 105 | + Logger.Print(formatLog("INF", colorWhite, msg)) |
46 | 106 | } |
47 | 107 | } |
48 | 108 |
|
49 | | -// Debug logs very verbose diagnostic messages. |
| 109 | +// Debug logs very verbose diagnostic messages |
50 | 110 | func Debug(format string, v ...interface{}) { |
51 | 111 | if Level >= LevelDebug { |
52 | | - Logger.Printf("| DEBUG | "+format, v...) |
| 112 | + msg := fmt.Sprintf(format, v...) |
| 113 | + Logger.Print(formatLog("DBG", colorGray, msg)) |
53 | 114 | } |
54 | 115 | } |
0 commit comments