@@ -29,30 +29,30 @@ import (
2929type GoogleCloudSlogHandler struct {
3030 logger * logging.Logger
3131 client * logging.Client
32- level slog.Leveler
32+ opts * slog.HandlerOptions
3333 groupPrefix string
3434 attrs []slog.Attr
3535}
3636
3737var _ slog.Handler = & GoogleCloudSlogHandler {}
3838
3939// 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 {
4141 client , err := logging .NewClient (ctx , projectID )
4242 if err != nil {
4343 return nil
4444 }
4545 return & GoogleCloudSlogHandler {
4646 client : client ,
4747 logger : client .Logger (logName ),
48- level : level ,
48+ opts : opts ,
4949 }
5050}
5151
5252func (h * GoogleCloudSlogHandler ) Enabled (ctx context.Context , level slog.Level ) bool {
5353 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 ()
5656 }
5757 return level >= minLevel
5858}
@@ -88,42 +88,15 @@ func (h *GoogleCloudSlogHandler) Handle(ctx context.Context, r slog.Record) erro
8888 return nil
8989}
9090
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-
11991func (h * GoogleCloudSlogHandler ) WithAttrs (attrs []slog.Attr ) slog.Handler {
12092 for i , attr := range attrs {
12193 attrs [i ] = withGroupPrefix (h .groupPrefix , attr )
12294 }
12395
12496 return & GoogleCloudSlogHandler {
97+ client : h .client ,
12598 logger : h .logger ,
126- level : h . level ,
99+ opts : h . opts ,
127100 groupPrefix : h .groupPrefix ,
128101 attrs : append (h .attrs , attrs ... ),
129102 }
@@ -139,13 +112,42 @@ func (h *GoogleCloudSlogHandler) WithGroup(name string) slog.Handler {
139112 }
140113
141114 return & GoogleCloudSlogHandler {
115+ client : h .client ,
142116 logger : h .logger ,
143- level : h . level ,
117+ opts : h . opts ,
144118 attrs : h .attrs ,
145119 groupPrefix : prefix ,
146120 }
147121}
148122
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+
149151func withGroupPrefix (groupPrefix string , attr slog.Attr ) slog.Attr {
150152 if groupPrefix != "" {
151153 attr .Key = groupPrefix + attr .Key
0 commit comments