Skip to content

Commit aba579b

Browse files
committed
mymodule: changes for v0.0.3
1 parent 94138ce commit aba579b

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

gcp_handler.go

+37-35
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,30 @@ import (
2929
type 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

3737
var _ 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

5252
func (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-
11991
func (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+
149151
func withGroupPrefix(groupPrefix string, attr slog.Attr) slog.Attr {
150152
if groupPrefix != "" {
151153
attr.Key = groupPrefix + attr.Key

gcp_handler_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ func TestGoogleCloudSlogHandler(t *testing.T) {
2525

2626
// Initialize GoogleCloudSlogHandler
2727
logName := "test-log"
28-
handler := NewGoogleCloudSlogHandler(ctx, projectID, logName, slog.LevelInfo)
28+
handler := NewGoogleCloudSlogHandler(ctx, projectID, logName, &slog.HandlerOptions{
29+
Level: slog.LevelInfo,
30+
})
2931
defer handler.Close()
3032

3133
// Set the handler for slog
3234
slog.SetDefault(slog.New(handler))
3335

3436
// Example log entries
3537
slog.Info("Starting application", "version", "1.0")
38+
slog.Debug("Debug", "debug", "sample debug")
3639
slog.Warn("This is a warning message", "component", "main")
3740
slog.Error("An error occurred", "error", "sample error")
3841

version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ package sloggcp
1818

1919
// Version is the current release version of GCP Cloud Logging Slog in use.
2020
func Version() string {
21-
return "0.0.2"
21+
return "0.0.3"
2222
}

0 commit comments

Comments
 (0)