-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.go
More file actions
114 lines (98 loc) · 2.62 KB
/
formatter.go
File metadata and controls
114 lines (98 loc) · 2.62 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package utils
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"golang.org/x/term"
)
func FormatJSON(entry map[string]interface{}, pretty bool) ([]byte, error) { // todo: agregar colores q diferencien a los campos de su contenido
if pretty {
return json.MarshalIndent(entry, "", " ")
}
return json.Marshal(entry)
}
func FormatText(entry map[string]interface{}, level string, pretty bool) string {
var sb strings.Builder
//timestamp
ts, _ := entry["ts"].(string)
if ts == "" {
ts = time.Now().Format(time.RFC3339)
}
sb.WriteString(ts)
sb.WriteString(" ")
if isTerminal() {
sb.WriteString(applyColor(level, fmt.Sprintf(" [%s]", level)))
} else {
sb.WriteString(fmt.Sprintf("[%s]", level))
}
// layer
if layer, ok := entry["layer"].(string); ok && layer != "" {
sb.WriteString(fmt.Sprintf(" [%s]", strings.ToUpper(layer)))
} else {
sb.WriteString(" [UNKNOWN]")
}
// message
if msg, ok := entry["msg"].(string); ok && msg != "" {
sb.WriteString(fmt.Sprintf(" %s", msg))
}
// Caller
if caller, ok := entry["caller"].(string); ok && caller != "" {
sb.WriteString(fmt.Sprintf(" (%s)", filepath.Base(caller)))
}
for k, v := range entry {
if k == "ts" || k == "lvl" || k == "msg" || k == "layer" || k == `caller` {
continue
}
sb.WriteString(fmt.Sprintf(" %s: %v", k, v))
}
return sb.String()
}
func isTerminal() bool {
fd := int(os.Stdout.Fd())
return term.IsTerminal(fd)
}
// Aplica color al nivel de log (solo si es TTY)
func applyColor(level, text string) string {
switch level {
case "DEBUG":
return fmt.Sprintf("\x1b[36m%s\x1b[0m", text)
case "INFO":
return fmt.Sprintf("\x1b[32m%s\x1b[0m", text)
case "WARN":
return fmt.Sprintf("\x1b[33m%s\x1b[0m", text)
case "ERROR":
return fmt.Sprintf("\x1b[31m%s\x1b[0m", text)
case "FATAL":
return fmt.Sprintf("\x1b[41m\x1b[37m%s\x1b[0m", text)
default:
return text
}
}
//todo: Mejorar la serialización de campos complejos:
func FormatField(field any, pretty bool) string {
// Si es una estructura o un mapa, lo convertimos a JSON
j, err := json.Marshal(field)
if err != nil {
return fmt.Sprintf("%v", field)
}
if pretty {
var indented bytes.Buffer
json.Indent(&indented, j, "", " ")
return indented.String()
}
return string(j)
}
// Formatea un log completo (timestamp, nivel, mensaje, y otros campos)
func FormatLog(entry map[string]any, level string, pretty bool) string {
if entry["lvl"] == "JSON" {
// Si el formato es JSON, utilizamos el formato JSON
j, _ := FormatJSON(entry, pretty)
return string(j)
}
// Si no es JSON, usamos el formato de texto
return FormatText(entry, level, pretty)
}