@@ -29,30 +29,30 @@ import (
29
29
type GoogleCloudSlogHandler struct {
30
30
logger * logging.Logger
31
31
client * logging.Client
32
- level slog.Leveler
32
+ opts * slog.HandlerOptions
33
33
groupPrefix string
34
34
attrs []slog.Attr
35
35
}
36
36
37
37
var _ slog.Handler = & GoogleCloudSlogHandler {}
38
38
39
39
// NewGoogleCloudSlogHandler initializes a new GoogleCloudSlogHandler.
40
- func NewGoogleCloudSlogHandler (ctx context.Context , projectID , logName string , level slog.Level ) * GoogleCloudSlogHandler {
40
+ func NewGoogleCloudSlogHandler (ctx context.Context , projectID , logName string , opts * slog.HandlerOptions ) * GoogleCloudSlogHandler {
41
41
client , err := logging .NewClient (ctx , projectID )
42
42
if err != nil {
43
43
return nil
44
44
}
45
45
return & GoogleCloudSlogHandler {
46
46
client : client ,
47
47
logger : client .Logger (logName ),
48
- level : level ,
48
+ opts : opts ,
49
49
}
50
50
}
51
51
52
52
func (h * GoogleCloudSlogHandler ) Enabled (ctx context.Context , level slog.Level ) bool {
53
53
minLevel := slog .LevelInfo
54
- if h .level != nil {
55
- minLevel = h .level .Level ()
54
+ if h .opts . Level != nil {
55
+ minLevel = h .opts . Level .Level ()
56
56
}
57
57
return level >= minLevel
58
58
}
@@ -88,42 +88,15 @@ func (h *GoogleCloudSlogHandler) Handle(ctx context.Context, r slog.Record) erro
88
88
return nil
89
89
}
90
90
91
- // mapSeverity converts slog.Level to Google Cloud Logging's Severity.
92
- func (h * GoogleCloudSlogHandler ) mapSeverity (level slog.Level ) logging.Severity {
93
- switch level {
94
- case slog .LevelDebug :
95
- return logging .Debug
96
- case slog .LevelInfo :
97
- return logging .Info
98
- case slog .LevelWarn :
99
- return logging .Warning
100
- case slog .LevelError :
101
- return logging .Error
102
- default :
103
- return logging .Default
104
- }
105
- }
106
-
107
- // formatAttrValue formats attribute values for Google Cloud Logging.
108
- func (h * GoogleCloudSlogHandler ) formatAttrValue (value interface {}) interface {} {
109
- switch v := value .(type ) {
110
- case string , int , int64 , float64 , bool :
111
- return v
112
- case error :
113
- return v .Error ()
114
- default :
115
- return fmt .Sprintf ("%v" , v ) // Fallback for unsupported types
116
- }
117
- }
118
-
119
91
func (h * GoogleCloudSlogHandler ) WithAttrs (attrs []slog.Attr ) slog.Handler {
120
92
for i , attr := range attrs {
121
93
attrs [i ] = withGroupPrefix (h .groupPrefix , attr )
122
94
}
123
95
124
96
return & GoogleCloudSlogHandler {
97
+ client : h .client ,
125
98
logger : h .logger ,
126
- level : h . level ,
99
+ opts : h . opts ,
127
100
groupPrefix : h .groupPrefix ,
128
101
attrs : append (h .attrs , attrs ... ),
129
102
}
@@ -139,13 +112,42 @@ func (h *GoogleCloudSlogHandler) WithGroup(name string) slog.Handler {
139
112
}
140
113
141
114
return & GoogleCloudSlogHandler {
115
+ client : h .client ,
142
116
logger : h .logger ,
143
- level : h . level ,
117
+ opts : h . opts ,
144
118
attrs : h .attrs ,
145
119
groupPrefix : prefix ,
146
120
}
147
121
}
148
122
123
+ // mapSeverity converts slog.Level to Google Cloud Logging's Severity.
124
+ func (h * GoogleCloudSlogHandler ) mapSeverity (level slog.Level ) logging.Severity {
125
+ switch level {
126
+ case slog .LevelDebug :
127
+ return logging .Debug
128
+ case slog .LevelInfo :
129
+ return logging .Info
130
+ case slog .LevelWarn :
131
+ return logging .Warning
132
+ case slog .LevelError :
133
+ return logging .Error
134
+ default :
135
+ return logging .Default
136
+ }
137
+ }
138
+
139
+ // formatAttrValue formats attribute values for Google Cloud Logging.
140
+ func (h * GoogleCloudSlogHandler ) formatAttrValue (value interface {}) interface {} {
141
+ switch v := value .(type ) {
142
+ case string , int , int64 , float64 , bool :
143
+ return v
144
+ case error :
145
+ return v .Error ()
146
+ default :
147
+ return fmt .Sprintf ("%v" , v ) // Fallback for unsupported types
148
+ }
149
+ }
150
+
149
151
func withGroupPrefix (groupPrefix string , attr slog.Attr ) slog.Attr {
150
152
if groupPrefix != "" {
151
153
attr .Key = groupPrefix + attr .Key
0 commit comments