summaryrefslogtreecommitdiff
path: root/internal/logging/logging.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-08-16 16:26:12 +0300
committerPaul Buetow <paul@buetow.org>2025-08-16 16:26:12 +0300
commit833bb66706dd991ecd3973da360c472d818e970a (patch)
tree62d8465b305883af1002063eb54ef38a08de299d /internal/logging/logging.go
parent148cda5f7ed4513528e3a46164b990708eeb1bc6 (diff)
logging: migrate LSP logs to global singleton (internal/logging); use consistent colors/prefix; refactor LLM provider to use global logger and remove per-client loggers
Diffstat (limited to 'internal/logging/logging.go')
-rw-r--r--internal/logging/logging.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/logging/logging.go b/internal/logging/logging.go
new file mode 100644
index 0000000..2e4bbc8
--- /dev/null
+++ b/internal/logging/logging.go
@@ -0,0 +1,53 @@
+package logging
+
+import (
+ "fmt"
+ "log"
+)
+
+// ANSI color utilities shared across Hexai.
+const (
+ AnsiBgBlack = "\x1b[40m"
+ AnsiGrey = "\x1b[90m"
+ AnsiCyan = "\x1b[36m"
+ AnsiGreen = "\x1b[32m"
+ AnsiRed = "\x1b[31m"
+ AnsiReset = "\x1b[0m"
+)
+
+// AnsiBase is the default style: black background + grey foreground.
+const AnsiBase = AnsiBgBlack + AnsiGrey
+
+// singleton logger used across the codebase
+var std *log.Logger
+
+// Bind sets the underlying standard logger to use for Logf.
+func Bind(l *log.Logger) { std = l }
+
+// Logf prints a formatted message with a module prefix and base ANSI style.
+func Logf(prefix, format string, args ...any) {
+ if std == nil {
+ return
+ }
+ msg := fmt.Sprintf(format, args...)
+ std.Print(AnsiBase + prefix + msg + AnsiReset)
+}
+
+// Logging configuration for previews (shared)
+var logPreviewLimit int // 0 means unlimited
+
+// SetLogPreviewLimit sets the maximum number of characters to log for
+// request/response previews. Set to 0 for unlimited.
+func SetLogPreviewLimit(n int) { logPreviewLimit = n }
+
+// PreviewForLog returns the string truncated to the configured preview limit.
+func PreviewForLog(s string) string {
+ if logPreviewLimit > 0 {
+ if len(s) <= logPreviewLimit {
+ return s
+ }
+ return s[:logPreviewLimit] + "…"
+ }
+ return s
+}
+