Skip to content

Commit

Permalink
feat: add zerolog support (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
Tronje Krop committed Nov 5, 2024
1 parent 028225d commit fd08b36
Show file tree
Hide file tree
Showing 17 changed files with 2,647 additions and 1,270 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: 1.23.2
cache: false

- name: Checkout code
uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export GOPATH ?= $(shell $(GO) env GOPATH)
export GOBIN ?= $(GOPATH)/bin

# Setup go-make version to use desired build and config scripts.
GOMAKE_DEP ?= github.com/tkrop/[email protected].106
GOMAKE_DEP ?= github.com/tkrop/[email protected].113
INSTALL_FLAGS ?= -mod=readonly -buildvcs=auto
# Request targets from go-make targets target.
TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \
$(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) >/dev/stderr && \
$(GOBIN)/go-make show-targets 2>/dev/null)
MAKEFLAGS="" $(GOBIN)/go-make show-targets 2>/dev/null)
# Declare all targets phony to make them available for auto-completion.
.PHONY:: $(TARGETS)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.8
0.0.9
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var testConfigParams = map[string]testConfigParam{
expect: test.Panic(config.NewErrConfig("loading file", "test",
test.NewBuilder[viper.ConfigFileNotFoundError]().
Set("locations", fmt.Sprintf("%s", configPaths)).
Set("name", "test").Get("").(error))),
Set("name", "test").Build())),
},

"panic after unmarschal failure": {
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/tkrop/go-testing v0.0.21
github.com/tkrop/go-testing v0.0.22
gopkg.in/yaml.v3 v3.0.1
)

require golang.org/x/sys v0.26.0 // indirect
require (
github.com/rogpeppe/go-internal v1.13.1 // indirect
golang.org/x/sys v0.26.0 // indirect
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
Expand Down Expand Up @@ -34,8 +34,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
Expand All @@ -61,8 +61,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tkrop/go-testing v0.0.21 h1:lCSLCqsa0KUKOjGOd5euUIpmctSZy9eO7e1pwe2ym2A=
github.com/tkrop/go-testing v0.0.21/go.mod h1:RFUX2nk7n4QPUN0doC4R0KS6PqeaK1vWUDy3tBtjjKQ=
github.com/tkrop/go-testing v0.0.22 h1:zRxOdj4XAmafww6QtdkQlnqlZCnN14DbxSyZSWWjFx0=
github.com/tkrop/go-testing v0.0.22/go.mod h1:S9WAo/AbkqDLp1Jxu8Cd+fbmdgJmyDhv+CruOEBDlIY=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
Expand Down
77 changes: 18 additions & 59 deletions log/format/logrus.go → log/buffer.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,12 @@
// Package format provides common log formatting based on logrus for services,
// jobs, and commands with integrated configuration loading.
package format
package log

import (
"bytes"
"fmt"
"io"
"maps"
"slices"
"sort"
"runtime"
"strconv"

log "github.com/sirupsen/logrus"
)

// Pretty formats logs into a pretty format.
type Pretty struct {
// TimeFormat is defining the time format used for printing timestamps.
TimeFormat string
// ColorMode is defining the color mode (default = ColorAuto).
ColorMode ColorMode
// OrderMode is defining the order mode.
OrderMode OrderMode

// LevelNames is defining the names used for marking the different log
// levels.
LevelNames []string
// LevelColors is defining the colors used for marking the different log
// levels.
LevelColors []string
}

// Format formats the log entry to a pretty format.
func (p *Pretty) Format(entry *log.Entry) ([]byte, error) {
buffer := NewBuffer(p, &bytes.Buffer{})
buffer.WriteString(entry.Time.Format(p.TimeFormat)).
WriteByte(' ').WriteLevel(entry.Level).WriteCaller(entry).
WriteByte(' ').WriteString(entry.Message)

for _, key := range p.getSortedKeys(entry.Data) {
buffer.WriteByte(' ').WriteData(key, entry.Data[key])
}
return buffer.WriteByte('\n').Bytes()
}

// getSortedKeys returns the keys of the given data.
func (p *Pretty) getSortedKeys(data log.Fields) []string {
keys := slices.Collect(maps.Keys(data))
if p.OrderMode.CheckFlag(OrderOn) {
sort.Strings(keys)
}
return keys
}

// Buffer is the interface for writing bytes and strings.
type BufferWriter interface {
// WriteByte writes the given byte to the writer.
Expand All @@ -62,12 +16,14 @@ type BufferWriter interface {

// Bytes returns the current bytes of the writer.
Bytes() []byte
// String returns the current string of the writer.
String() string
}

// Buffer is a buffer for the pretty formatter.
type Buffer struct {
// pretty is the pretty formatter of the buffer.
pretty *Pretty
pretty *Setup
// buffer is the bytes buffer used for writing.
buffer BufferWriter

Expand All @@ -76,7 +32,7 @@ type Buffer struct {
}

// NewBuffer creates a new buffer for the pretty formatter.
func NewBuffer(p *Pretty, b BufferWriter) *Buffer {
func NewBuffer(p *Setup, b BufferWriter) *Buffer {
return &Buffer{pretty: p, buffer: b}
}

Expand Down Expand Up @@ -122,7 +78,7 @@ func (b *Buffer) WriteColored(color, str string) *Buffer {
}

// WriteLevel writes the given log level to the buffer.
func (b *Buffer) WriteLevel(level log.Level) *Buffer {
func (b *Buffer) WriteLevel(level Level) *Buffer {
if b.err != nil {
return b
}
Expand All @@ -135,7 +91,7 @@ func (b *Buffer) WriteLevel(level log.Level) *Buffer {
}

// WriteField writes the given key with the given color to the buffer.
func (b *Buffer) WriteField(level log.Level, key string) *Buffer {
func (b *Buffer) WriteField(level Level, key string) *Buffer {
if b.err != nil {
return b
}
Expand All @@ -147,12 +103,11 @@ func (b *Buffer) WriteField(level log.Level, key string) *Buffer {
}

// WriteCaller writes the caller information to the buffer.
func (b *Buffer) WriteCaller(entry *log.Entry) *Buffer {
if b.err != nil || !entry.HasCaller() {
func (b *Buffer) WriteCaller(caller *runtime.Frame) *Buffer {
if b.err != nil || caller == nil {
return b
}

caller := entry.Caller
return b.WriteByte(' ').WriteByte('[').
WriteString(caller.File).WriteByte(':').
WriteString(strconv.Itoa(caller.Line)).WriteByte('#').
Expand Down Expand Up @@ -181,11 +136,10 @@ func (b *Buffer) WriteData(key string, value any) *Buffer {
return b
}

switch key {
case log.ErrorKey:
return b.WriteField(log.ErrorLevel, key).
if key == b.pretty.ErrorName {
return b.WriteField(ErrorLevel, key).
WriteByte('=').WriteValue(value)
default:
} else {
return b.WriteField(FieldLevel, key).
WriteByte('=').WriteValue(value)
}
Expand All @@ -195,3 +149,8 @@ func (b *Buffer) WriteData(key string, value any) *Buffer {
func (b *Buffer) Bytes() ([]byte, error) {
return b.buffer.Bytes(), b.err
}

// String returns current string of the buffer with the current error.
func (b *Buffer) String() string {
return b.buffer.String()
}
Loading

0 comments on commit fd08b36

Please sign in to comment.