This repository was archived by the owner on Jul 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
143 lines (121 loc) · 4.38 KB
/
Copy pathlog.go
File metadata and controls
143 lines (121 loc) · 4.38 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"github.com/go-logr/logr"
wailsLogger "github.com/wailsapp/wails/v2/pkg/logger"
"strings"
)
// WailsLogSink implements the logr.LogSink interface by directing logs to the Wails logger.
// Since the Wails logger is string-based, we handle logr's structured key/value pairs by formatting them into a single string.
type WailsLogSink struct {
wailsLog wailsLogger.Logger
name string
values []interface{} // Stored keys and values from WithValues
}
// NewWailsLogr returns a logr.Logger instance that writes its output to the provided Wails Logger.
func NewWailsLogr(wailsLog wailsLogger.Logger) logr.Logger {
return logr.New(WailsLogSink{wailsLog: wailsLog})
}
// Init implements logr.LogSink.
// It is called once when the logger is constructed, but logr.RuntimeInfo currently only provides CallDepth, not the name.
func (s WailsLogSink) Init(info logr.RuntimeInfo) {
// The name is accumulated via WithName, not set here, as logr.RuntimeInfo does not contain a name field.
}
// Enabled implements logr.LogSink. It always returns true, relying on the Wails logger's internal logic
// or the application's configured Wails LogLevel to filter messages.
// NOTE: For better performance, you could try to check the Wails logger's active log level here.
func (s WailsLogSink) Enabled(level int) bool {
// Wails logger interface doesn't expose its current LogLevel for runtime checking,
// so we assume it's always enabled for simplicity and rely on Wails' own filtering.
return true
}
// Info implements logr.LogSink for non-error messages.
func (s WailsLogSink) Info(level int, msg string, keysAndValues ...interface{}) {
// Combine stored values and current values
allKeysAndValues := append(s.values, keysAndValues...)
formattedMsg := s.formatMessage(msg, allKeysAndValues...)
// Map logr V-levels to Wails levels:
switch level {
case 0:
s.wailsLog.Info(formattedMsg)
case 1:
s.wailsLog.Debug(formattedMsg)
case 2:
s.wailsLog.Trace(formattedMsg)
default:
// Use Trace for V-levels > 2
if level > 2 {
s.wailsLog.Trace(formattedMsg)
} else {
// Negative or unusual V-levels default to Info
s.wailsLog.Info(formattedMsg)
}
}
}
// Error implements logr.LogSink for error messages.
func (s WailsLogSink) Error(err error, msg string, keysAndValues ...interface{}) {
// Combine stored values, current values, and the error object
allKeysAndValues := append(s.values, keysAndValues...)
allKeysAndValues = append(allKeysAndValues, "error", err.Error())
formattedMsg := s.formatMessage(msg, allKeysAndValues...)
s.wailsLog.Error(formattedMsg)
}
// WithValues implements logr.LogSink to return a new LogSink with additional key/value pairs.
func (s WailsLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink {
newValues := make([]interface{}, 0, len(s.values)+len(keysAndValues))
newValues = append(newValues, s.values...)
newValues = append(newValues, keysAndValues...)
return WailsLogSink{
wailsLog: s.wailsLog,
name: s.name,
values: newValues,
}
}
// WithName implements logr.LogSink to return a new LogSink with the specified name appended.
func (s WailsLogSink) WithName(name string) logr.LogSink {
newName := s.name
if newName != "" {
newName = newName + "/"
}
newName += name
newSink := WailsLogSink{
wailsLog: s.wailsLog,
name: newName,
values: s.values,
}
// Init is not called on WithName return, so we set the name directly
return newSink
}
// V is required by logr but not used in this Wails adapter as we rely on the
// level passed to the Info method. We return a clone.
func (s WailsLogSink) V(level int) logr.LogSink {
return s
}
// formatMessage converts the logr structured message into a Wails compatible string.
func (s WailsLogSink) formatMessage(msg string, keysAndValues ...interface{}) string {
var builder strings.Builder
// 1. Write Logger Name
if s.name != "" {
builder.WriteString("[")
builder.WriteString(s.name)
builder.WriteString("] ")
}
// 2. Write Message
builder.WriteString(msg)
// 3. Write Key/Value pairs
if len(keysAndValues) > 0 {
builder.WriteString(" | ")
for i := 0; i < len(keysAndValues); i += 2 {
key := keysAndValues[i]
value := interface{}("")
if i+1 < len(keysAndValues) {
value = keysAndValues[i+1]
}
builder.WriteString(fmt.Sprintf("%v=%v", key, value))
if i+2 < len(keysAndValues) {
builder.WriteString(", ")
}
}
}
return builder.String()
}