blob: 259ad68aef9454558ebffc24320c9f0fa1d17f8d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
// Summary: ANSI-styled logging utilities with a bound standard logger and configurable preview truncation.
package logging
import (
"fmt"
"log"
)
// ANSI color utilities shared across Hexai.
const (
AnsiBgBlack = "\x1b[40m"
AnsiGrey = "\x1b[90m"
AnsiCyan = "\x1b[36m"
AnsiGreen = "\x1b[32m"
AnsiYellow = "\x1b[33m"
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
}
|