From aba579b98e52180ef52877201b0bee917ef3a946 Mon Sep 17 00:00:00 2001 From: Ufuk Date: Tue, 29 Oct 2024 20:17:34 +0300 Subject: [PATCH] mymodule: changes for v0.0.3 --- gcp_handler.go | 72 +++++++++++++++++++++++---------------------- gcp_handler_test.go | 5 +++- version.go | 2 +- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/gcp_handler.go b/gcp_handler.go index 073a54a..5923857 100644 --- a/gcp_handler.go +++ b/gcp_handler.go @@ -29,7 +29,7 @@ import ( type GoogleCloudSlogHandler struct { logger *logging.Logger client *logging.Client - level slog.Leveler + opts *slog.HandlerOptions groupPrefix string attrs []slog.Attr } @@ -37,7 +37,7 @@ type GoogleCloudSlogHandler struct { var _ slog.Handler = &GoogleCloudSlogHandler{} // NewGoogleCloudSlogHandler initializes a new GoogleCloudSlogHandler. -func NewGoogleCloudSlogHandler(ctx context.Context, projectID, logName string, level slog.Level) *GoogleCloudSlogHandler { +func NewGoogleCloudSlogHandler(ctx context.Context, projectID, logName string, opts *slog.HandlerOptions) *GoogleCloudSlogHandler { client, err := logging.NewClient(ctx, projectID) if err != nil { return nil @@ -45,14 +45,14 @@ func NewGoogleCloudSlogHandler(ctx context.Context, projectID, logName string, l return &GoogleCloudSlogHandler{ client: client, logger: client.Logger(logName), - level: level, + opts: opts, } } func (h *GoogleCloudSlogHandler) Enabled(ctx context.Context, level slog.Level) bool { minLevel := slog.LevelInfo - if h.level != nil { - minLevel = h.level.Level() + if h.opts.Level != nil { + minLevel = h.opts.Level.Level() } return level >= minLevel } @@ -88,42 +88,15 @@ func (h *GoogleCloudSlogHandler) Handle(ctx context.Context, r slog.Record) erro return nil } -// mapSeverity converts slog.Level to Google Cloud Logging's Severity. -func (h *GoogleCloudSlogHandler) mapSeverity(level slog.Level) logging.Severity { - switch level { - case slog.LevelDebug: - return logging.Debug - case slog.LevelInfo: - return logging.Info - case slog.LevelWarn: - return logging.Warning - case slog.LevelError: - return logging.Error - default: - return logging.Default - } -} - -// formatAttrValue formats attribute values for Google Cloud Logging. -func (h *GoogleCloudSlogHandler) formatAttrValue(value interface{}) interface{} { - switch v := value.(type) { - case string, int, int64, float64, bool: - return v - case error: - return v.Error() - default: - return fmt.Sprintf("%v", v) // Fallback for unsupported types - } -} - func (h *GoogleCloudSlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { for i, attr := range attrs { attrs[i] = withGroupPrefix(h.groupPrefix, attr) } return &GoogleCloudSlogHandler{ + client: h.client, logger: h.logger, - level: h.level, + opts: h.opts, groupPrefix: h.groupPrefix, attrs: append(h.attrs, attrs...), } @@ -139,13 +112,42 @@ func (h *GoogleCloudSlogHandler) WithGroup(name string) slog.Handler { } return &GoogleCloudSlogHandler{ + client: h.client, logger: h.logger, - level: h.level, + opts: h.opts, attrs: h.attrs, groupPrefix: prefix, } } +// mapSeverity converts slog.Level to Google Cloud Logging's Severity. +func (h *GoogleCloudSlogHandler) mapSeverity(level slog.Level) logging.Severity { + switch level { + case slog.LevelDebug: + return logging.Debug + case slog.LevelInfo: + return logging.Info + case slog.LevelWarn: + return logging.Warning + case slog.LevelError: + return logging.Error + default: + return logging.Default + } +} + +// formatAttrValue formats attribute values for Google Cloud Logging. +func (h *GoogleCloudSlogHandler) formatAttrValue(value interface{}) interface{} { + switch v := value.(type) { + case string, int, int64, float64, bool: + return v + case error: + return v.Error() + default: + return fmt.Sprintf("%v", v) // Fallback for unsupported types + } +} + func withGroupPrefix(groupPrefix string, attr slog.Attr) slog.Attr { if groupPrefix != "" { attr.Key = groupPrefix + attr.Key diff --git a/gcp_handler_test.go b/gcp_handler_test.go index 41712f7..89b8aa6 100644 --- a/gcp_handler_test.go +++ b/gcp_handler_test.go @@ -25,7 +25,9 @@ func TestGoogleCloudSlogHandler(t *testing.T) { // Initialize GoogleCloudSlogHandler logName := "test-log" - handler := NewGoogleCloudSlogHandler(ctx, projectID, logName, slog.LevelInfo) + handler := NewGoogleCloudSlogHandler(ctx, projectID, logName, &slog.HandlerOptions{ + Level: slog.LevelInfo, + }) defer handler.Close() // Set the handler for slog @@ -33,6 +35,7 @@ func TestGoogleCloudSlogHandler(t *testing.T) { // Example log entries slog.Info("Starting application", "version", "1.0") + slog.Debug("Debug", "debug", "sample debug") slog.Warn("This is a warning message", "component", "main") slog.Error("An error occurred", "error", "sample error") diff --git a/version.go b/version.go index d96da1a..8e80b59 100644 --- a/version.go +++ b/version.go @@ -18,5 +18,5 @@ package sloggcp // Version is the current release version of GCP Cloud Logging Slog in use. func Version() string { - return "0.0.2" + return "0.0.3" }