Skip to content

Commit 63fd6d7

Browse files
committed
go 1.26 upgrade, major refactoring
1 parent 319eeff commit 63fd6d7

23 files changed

+710
-561
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
test-go:
66
# Install go modules and run tests
77
docker:
8-
- image: cimg/go:1.20
8+
- image: cimg/go:1.26
99
steps:
1010
- checkout
1111
- restore_cache:

.gitignore

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,46 @@
44
*.dll
55
*.so
66
*.dylib
7+
go-logger
78

8-
# Test binary, built with `go test -c`
9+
# Test binaries
910
*.test
11+
*.test.exe
1012

11-
# Output of the go coverage tool, specifically when used with LiteIDE
13+
# Output of the go coverage tool
1214
*.out
15+
coverage.txt
1316

14-
## IntelliJ
17+
# OS-specific files
18+
.DS_Store
19+
.DS_Store?
20+
._*
21+
.Spotlight-V100
22+
.Trashes
23+
ehthumbs.db
24+
Thumbs.db
25+
26+
# IDEs
1527
.idea/
1628
*.iml
1729
*.ipr
1830
*.iws
19-
20-
# VSCode
21-
.vs/
2231
.vscode/
32+
.vs/
33+
*.sublime-project
34+
*.sublime-workspace
2335

24-
# Vendor
25-
vendor
26-
27-
# target
28-
target
36+
# Dependency directories
37+
vendor/
2938

30-
## Mac
31-
.DS_Store
39+
# Build artifacts
40+
target/
41+
bin/
3242

33-
##Docker
43+
# Environment files
3444
.env
45+
.env.*
46+
!.env.example
3547

36-
##Binary
37-
go-logger
48+
# Local logs
49+
*.log

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Arun Gopalpuri.
3+
Copyright (c) 2026 Arun Gopalpuri.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
.DEFAULT_GOAL := help
22
SHELL := /bin/bash
33

4-
help: ## This help
4+
# Project variables
5+
BINARY_NAME=go-logger
6+
PKG=./pkg/logger/...
7+
8+
.PHONY: help build test bench vet format tidy clean
9+
10+
help: ## Show this help message
511
@awk 'BEGIN {FS = ":.*?## "} /^[a-z0-9A-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
612

7-
build: ## Build go app for current OS/Arch
13+
build: ## Build the library
814
go build ./...
915

10-
test: ## Run all unit tests
11-
go test ./...
16+
test: ## Run all unit tests
17+
go test -v $(PKG)
18+
19+
bench: ## Run benchmarks
20+
go test -bench=. -benchmem $(PKG)
1221

13-
vet:
14-
go list ./... | grep -v /vendor/ | xargs -L1 go vet
22+
vet: ## Run go vet
23+
go vet $(PKG)
1524

16-
# Format the source code
17-
format:
18-
@find ./ -type f -name "*.go" -exec gofmt -w {} \;
25+
tidy: ## Tidy up go.mod and go.sum
26+
go mod tidy
1927

20-
lint: ## Lint all go source files
21-
go list ./... | grep -v /vendor/ | xargs -L1 golint -set_exit_status
28+
format: ## Format source code
29+
go fmt $(PKG)
2230

23-
# Check if the source code has been formatted
24-
fmtcheck:
25-
@mkdir -p target
26-
@find ./ -type f -name "*.go" -exec gofmt -d {} \; | tee target/format.diff
27-
@test ! -s target/format.diff || { echo "ERROR: the source code has not been formatted - please use 'make format' or 'gofmt'"; exit 1; }
31+
clean: ## Remove build artifacts and test cache
32+
go clean -testcache
33+
rm -rf target/
34+
rm -f $(BINARY_NAME)
35+
rm -f *.out
36+
rm -f *.test

README.md

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,107 @@
1-
## Logger interface with implementations (Zap and Logrus)
1+
# ⚡ go-logger
22

3-
You can use `logger` as an interface (example below) and set actual implementation to `ReplaceGlobals`, this allows
4-
you to change log library without changing your application code.
3+
[![Go Version](https://img.shields.io/badge/Go-1.26+-00ADD8?style=for-the-badge&logo=go)](https://golang.org)
4+
[![License](https://img.shields.io/badge/License-MIT-green?style=for-the-badge)](LICENSE)
5+
[![Performance](https://img.shields.io/badge/Performance-High-blueviolet?style=for-the-badge)](README.md#benchmarks)
56

6-
Also, when we create go libraries in general we shouldn't be logging but at times we do have to log, debug what the
7-
library is doing or trace the log.
7+
A modernized, high-performance, and context-aware structured logging interface for Go. Designed to be modular and pluggable, allowing you to swap backends without changing a single line of application code.
88

9-
We cannot implement a library with one log library and expect applications to use the same log library. We use two
10-
of the popular log libraries [logrus](https://github.com/sirupsen/logrus) and [zap](https://github.com/uber-go/zap)
11-
and this `go-logger` library allows you to use either one by using an interface.
9+
---
1210

13-
You can add your implementation if you want to add more log libraries (e.g. zerolog).
11+
## ✨ Features
1412

15-
## Installation
13+
- **Lightning Fast**: Blazing performance with first-class **Zerolog** and **Zap** support.
14+
- **Structured by Default**: Uses modern `keyvals ...any` patterns (compatible with `slog`).
15+
- **Context Aware**: Native `WithContext` support for request tracing and spans.
16+
- **Pluggable Backends**: Out-of-the-box adapters for **Zap**, **Logrus**, **Slog**, and **Zerolog**.
17+
- **Go 1.26 Native**: Utilizes `slog.NewMultiHandler` and `slog.DiscardHandler` for built-in multi-logging and silencing.
18+
- **Developer Friendly**: Support for both Dependency Injection and Global Singleton patterns.
1619

17-
go get -u github.com/arun0009/go-logger
20+
---
1821

1922
## Quick Start
2023

21-
[logrus](https://github.com/sirupsen/logrus) example
24+
### 1. Installation
25+
```bash
26+
go get github.com/arun0009/go-logger
27+
```
2228

29+
### 2. Implementation (using Slog)
2330
```go
2431
package main
2532

2633
import (
34+
"log/slog"
2735
"os"
2836

2937
"github.com/arun0009/go-logger/pkg/logger"
30-
"github.com/sirupsen/logrus"
3138
)
3239

3340
func main() {
34-
logrusLog := logrus.New()
35-
logrusLog.SetFormatter(&logrus.JSONFormatter{})
36-
logrusLog.SetOutput(os.Stdout)
37-
logrusLog.SetLevel(logrus.DebugLevel)
38-
log, _ := logger.NewLogrusLogger(logrusLog)
39-
logger.ReplaceGlobals(log)
40-
//anywhere in your code you can now use logger.L() as its globally set
41-
logger.L().WithFields(logger.Fields{
42-
"foo": "bar",
43-
}).Info("direct")
41+
// Initialize your favorite backend
42+
handler := slog.NewJSONHandler(os.Stdout, nil)
43+
l := logger.NewSlogLogger(slog.New(handler))
44+
45+
// Set as global (optional)
46+
logger.ReplaceGlobals(l)
47+
48+
// Log with structure
49+
logger.L().Info("modern logging enabled",
50+
"version", "Go 1.26",
51+
"env", "production",
52+
)
4453
}
4554
```
4655

47-
[zap](https://github.com/uber-go/zap) example
56+
---
57+
58+
## Supported Adapters
59+
60+
| Backend | Performance | Package | Recommended Use |
61+
| :--- | :--- | :--- | :--- |
62+
| **Zerolog** | Extreme | `github.com/rs/zerolog` | Low-latency microservices |
63+
| **Zap** | High | `go.uber.org/zap` | Enterprise-grade systems |
64+
| **Slog** | Standard | `log/slog` (Stdlib) | Default Go projects |
65+
| **Logrus** | Legacy | `github.com/sirupsen/logrus` | Maintaining older codebases |
4866

67+
### Example: Using Zerolog
4968
```go
50-
package main
69+
zl := zerolog.New(os.Stdout)
70+
l := logger.NewZerologLogger(zl)
71+
logger.ReplaceGlobals(l)
5172

52-
import (
53-
"os"
73+
logger.L().Debug("zerolog is active", "speed", "fast")
74+
```
5475

55-
"github.com/arun0009/go-logger/pkg/logger"
56-
"go.uber.org/zap"
57-
"go.uber.org/zap/zapcore"
58-
)
76+
---
5977

60-
func main() {
61-
consoleEncoder := zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig())
62-
core := zapcore.NewCore(consoleEncoder,
63-
zapcore.Lock(zapcore.AddSync(os.Stderr)),
64-
zapcore.DebugLevel)
65-
zapLogger := zap.New(core)
66-
log, _ := logger.NewZapLogger(zapLogger)
67-
logger.ReplaceGlobals(log)
68-
//anywhere in your code you can now use logger.L() as its globally set
69-
logger.L().WithFields(logger.Fields{
70-
"foo": "bar",
71-
}).Info("direct")
78+
## Context Support
79+
80+
Easily propagate request IDs or trace metadata:
81+
82+
```go
83+
func HandleRequest(ctx context.Context) {
84+
// Attach context-specific fields
85+
l := logger.L().WithContext(ctx)
86+
87+
l.Info("processing request", "path", "/api/data")
7288
}
73-
```
89+
```
90+
91+
---
92+
93+
## Benchmarks
94+
95+
*Results from Apple M4 (Go 1.26)*
96+
97+
| Adapter | ns/op | Operations/s |
98+
| :--- | :--- | :--- |
99+
| **Zerolog** | **188.2** | 6.3M |
100+
| **Zap** | 283.2 | 3.9M |
101+
| **Slog** | 388.5 | 3.1M |
102+
| **Logrus** | 1057.0 | 1.0M |
103+
104+
---
105+
106+
## License
107+
MIT License. See [LICENSE](LICENSE) for details.

go.mod

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
module github.com/arun0009/go-logger
22

3-
go 1.21
3+
go 1.26
44

55
require (
6-
github.com/sirupsen/logrus v1.9.3
7-
github.com/stretchr/testify v1.9.0
8-
go.uber.org/zap v1.27.0
6+
github.com/rs/zerolog v1.34.0
7+
github.com/sirupsen/logrus v1.9.4
8+
github.com/stretchr/testify v1.11.1
9+
go.uber.org/zap v1.27.1
910
)
1011

1112
require (
1213
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/mattn/go-colorable v0.1.13 // indirect
15+
github.com/mattn/go-isatty v0.0.19 // indirect
1316
github.com/pmezard/go-difflib v1.0.0 // indirect
1417
go.uber.org/multierr v1.10.0 // indirect
15-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
18+
golang.org/x/sys v0.13.0 // indirect
1619
gopkg.in/yaml.v3 v3.0.1 // indirect
1720
)

go.sum

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
5+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
6+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
7+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8+
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
9+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
10+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
411
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
512
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6-
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
7-
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
8-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
11-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
13+
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
14+
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
15+
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
16+
github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w=
17+
github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g=
18+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
19+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
1220
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
1321
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
1422
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
1523
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
16-
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
17-
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
18-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
19-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24+
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
25+
go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
26+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
27+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29+
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
30+
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2031
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2132
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
22-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2333
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2434
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)