From 833bb66706dd991ecd3973da360c472d818e970a Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 16 Aug 2025 16:26:12 +0300 Subject: 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 --- internal/logging/logging.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 internal/logging/logging.go (limited to 'internal/logging/logging.go') 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 +} + -- cgit v1.2.3