summaryrefslogtreecommitdiff
path: root/internal/llm/logging.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-08-16 16:09:56 +0300
committerPaul Buetow <paul@buetow.org>2025-08-16 16:09:56 +0300
commit148cda5f7ed4513528e3a46164b990708eeb1bc6 (patch)
treebced7495844265b74776b83f360a9baa9cad0737 /internal/llm/logging.go
parent2a7acf566f93a7d660e6909dc6a829e17fe76066 (diff)
llm: centralize ANSI color and logging helpers in logging.go; remove duplicate color constants from openai.go
Diffstat (limited to 'internal/llm/logging.go')
-rw-r--r--internal/llm/logging.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/llm/logging.go b/internal/llm/logging.go
new file mode 100644
index 0000000..a6a6e51
--- /dev/null
+++ b/internal/llm/logging.go
@@ -0,0 +1,43 @@
+package llm
+
+import (
+ "fmt"
+ "log"
+)
+
+// ANSI color utilities shared across LLM providers.
+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
+
+// LogPrintf wraps a formatted message with a base style and prints with a prefix.
+func LogPrintf(logger *log.Logger, prefix, format string, args ...any) {
+ if logger == nil {
+ return
+ }
+ msg := fmt.Sprintf(format, args...)
+ logger.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 }
+
+func previewForLog(s string) string {
+ if logPreviewLimit > 0 {
+ return trimPreview(s, logPreviewLimit)
+ }
+ return s
+}
+