Skip to content

Commit

Permalink
updates to slogger constructors (text handler now has colored logs)
Browse files Browse the repository at this point in the history
  • Loading branch information
samlaf committed Apr 25, 2024
1 parent d8ca2a5 commit 9f1ed96
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/consensys/gnark-crypto v0.12.1
github.com/ethereum/go-ethereum v1.13.14
github.com/google/uuid v1.6.0
github.com/lmittmann/tint v1.0.4
github.com/prometheus/client_golang v1.19.0
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.29.1
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc=
github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
Expand All @@ -178,6 +177,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc=
github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down
63 changes: 63 additions & 0 deletions logging/slog_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"runtime"
"time"

"github.com/lmittmann/tint"
)

type SLogger struct {
Expand All @@ -16,13 +18,72 @@ type SLogger struct {

var _ Logger = (*SLogger)(nil)

// SLoggerOptions are options when creating a new SLogger.
// A zero Options consists entirely of default values.
//
// SLoggerOptions are an extension of [slog.HandlerOptions].
type SLoggerOptions struct {
// Enable source code location (Default: false)
AddSource bool

// Minimum level to log (Default: slog.LevelInfo)
Level slog.Leveler

// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
// See https://pkg.go.dev/log/slog#HandlerOptions for details.
ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr

// Time format (Default: time.StampMilli)
// only supported with text handler
TimeFormat string

// Disable color (Default: false)
// only supported with text handler (no color in json)
NoColor bool
}

// NewSlogTextLogger creates a new SLogger with a text handler
// Default behavior is colored log outputs. To disable colors, set opts.NoColor to true.
func NewTextSLogger(outputWriter io.Writer, opts *SLoggerOptions) *SLogger {
tintOptions := &tint.Options{
AddSource: opts.AddSource,
Level: opts.Level,
ReplaceAttr: opts.ReplaceAttr,
TimeFormat: opts.TimeFormat,
NoColor: opts.NoColor,
}
handler := tint.NewHandler(outputWriter, tintOptions)
logger := slog.New(handler)
return &SLogger{
logger,
}
}

// NewSlogJsonLogger creates a new SLogger with a Json handler
// Currently colors are not supported with json handler. If colors are required,
// use NewTextSLogger instead.
func NewJsonSLogger(outputWriter io.Writer, opts *SLoggerOptions) *SLogger {
handlerOpts := &slog.HandlerOptions{
AddSource: opts.AddSource,
Level: opts.Level,
ReplaceAttr: opts.ReplaceAttr,
}
handler := slog.NewJSONHandler(outputWriter, handlerOpts)
logger := slog.New(handler)
return &SLogger{
logger,
}
}

// NewSlogTextLogger creates a new SLogger with a text handler
//
// outputWriter is the writer to write the logs to (typically os.Stdout,
// but can also use a io.MultiWriter(os.Stdout, file) to write to multiple outputs)
// handlerOptions is the options for the handler, such as
// Level is the minimum level to log
// AddSource if true, adds source information to the log
//
// Deprecated: use NewTextSLogger instead
func NewSlogTextLogger(outputWriter io.Writer, handlerOpts *slog.HandlerOptions) *SLogger {
handler := slog.NewTextHandler(outputWriter, handlerOpts)
logger := slog.New(handler)
Expand All @@ -38,6 +99,8 @@ func NewSlogTextLogger(outputWriter io.Writer, handlerOpts *slog.HandlerOptions)
// handlerOptions is the options for the handler, such as
// Level is the minimum level to log
// AddSource if true, adds source information to the log
//
// Deprecated: use NewJsonSLogger instead
func NewSlogJsonLogger(outputWriter io.Writer, handlerOpts *slog.HandlerOptions) *SLogger {
handler := slog.NewJSONHandler(outputWriter, handlerOpts)
logger := slog.New(handler)
Expand Down

0 comments on commit 9f1ed96

Please sign in to comment.