Skip to content

Commit

Permalink
mymodule: changes for v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ucatbas committed Oct 29, 2024
1 parent 94138ce commit aba579b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
72 changes: 37 additions & 35 deletions gcp_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ import (
type GoogleCloudSlogHandler struct {
logger *logging.Logger
client *logging.Client
level slog.Leveler
opts *slog.HandlerOptions
groupPrefix string
attrs []slog.Attr
}

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
}
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
}
Expand Down Expand Up @@ -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...),
}
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion gcp_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ 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
slog.SetDefault(slog.New(handler))

// 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")

Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit aba579b

Please sign in to comment.