Skip to content

Commit ec8159d

Browse files
committed
v3: changes to get this module ready for release
- Update CI to test v3 - Update v3 dependencies to recent versions - Move compat layer out into its own package/module - Add documentation explaining what changed - fix CONTRIBUTORS and remove duplicate file - run gofmt and check in the results - remove the 'term' package from v3
1 parent 3ad0f3c commit ec8159d

35 files changed

Lines changed: 1831 additions & 421 deletions

.github/workflows/ci.yml

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.18.x, 1.19.x]
7+
go-version: [1.23.x, 1.24.x]
88
runs-on: ubuntu-latest
99
steps:
1010
- name: Install Go
11-
uses: actions/setup-go@v3
11+
uses: actions/setup-go@v5
1212
with:
1313
go-version: ${{ matrix.go-version }}
14-
- uses: actions/checkout@v3
14+
- uses: actions/checkout@v4
1515
with:
1616
path: './src/github.com/inconshreveable/log15'
17-
# staticcheck needs this for GOPATH
18-
- run: |
19-
echo "GOPATH=$GITHUB_WORKSPACE" >> $GITHUB_ENV
20-
echo "GO111MODULE=off" >> $GITHUB_ENV
21-
echo "PATH=$GITHUB_WORKSPACE/bin:$PATH" >> $GITHUB_ENV
22-
- name: Run tests
17+
- name: Run tests for v3
2318
run: |
24-
go get -t -d ./...
25-
go vet ./...
26-
go test -race ./...
19+
go vet -trimpath ./...
20+
go test -trimpath -race ./...
21+
working-directory: './src/github.com/inconshreveable/log15/v3'
22+
- name: Run tests for compat
23+
run: |
24+
go vet -trimpath ./...
25+
go test -trimpath -race ./...
26+
working-directory: './src/github.com/inconshreveable/log15/compat'
27+
- name: Run tests for v2
28+
run: |
29+
go vet -trimpath ./...
30+
go test -trimpath ./...
2731
working-directory: './src/github.com/inconshreveable/log15'

.mailmap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Evan Shaw <edsrzf@gmail.com>
99
Gonzalo Serrano <boikot@gmail.com>
1010
Jeremy <jrbudnack@starkandwayne.com>
1111
Jonathan Rudenberg <jonathan@titanous.com>
12-
Kevin Burke <kev@inburke.com>
12+
Kevin Burke <kevin@burke.dev> <kev@inburke.com>
13+
Nikolay Petrov <nikolay@ngrok.com> <45884158+nikolay-ngrok@users.noreply.github.com>
1314
Marc Abramowitz <marc@marc-abramowitz.com>
1415
Nathan Baulch <nathan.baulch@gmail.com>
1516
NotZippy <notzippy@gmail.com>

CONTRIBUTORS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ Carl Veazey <Carl_Veazey@cable.comcast.com>
66
Chris Hines <github@cs-guy.com>
77
Christoph Hack <christoph@tux21b.org>
88
Ciaran Downey <me@ciarand.me>
9+
Devendranath Thadi <devendranath.thadi3@gmail.com>
910
Dmitry Chestnykh <dmitry@codingrobots.com>
1011
Evan Shaw <edsrzf@gmail.com>
1112
Gonzalo Serrano <boikot@gmail.com>
1213
Jeremy <jrbudnack@starkandwayne.com>
1314
Jonathan Rudenberg <jonathan@titanous.com>
15+
Josh Robson Chase <josh@robsonchase.com>
1416
Kang Seong-Min <kang.seongmin@gmail.com>
15-
Kevin Burke <kev@inburke.com>
17+
Kevin Burke <kevin@burke.dev>
1618
Marc Abramowitz <marc@marc-abramowitz.com>
1719
Nathan Baulch <nathan.baulch@gmail.com>
20+
Nikolay Petrov <nikolay@ngrok.com>
1821
NotZippy <notzippy@gmail.com>
1922
Péter Szilágyi <peterke@gmail.com>
2023
Robert Egorov <robert.egorov@gmail.com>

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
force: ;
2+
3+
CONTRIBUTORS: force
4+
go run github.com/kevinburke/write_mailmap > CONTRIBUTORS

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,40 @@
44

55
Package log15 provides an opinionated, simple toolkit for best-practice logging in Go (golang) that is both human and machine readable. It is modeled after the Go standard library's [`io`](http://golang.org/pkg/io/) and [`net/http`](http://golang.org/pkg/net/http/) packages and is an alternative to the standard library's [`log`](http://golang.org/pkg/log/) package.
66

7+
## v3 upgrade
8+
9+
Version 3 compiles with Go modules. It also provides some basic simplifications:
10+
11+
- For performance, the `Call` property has been removed from a `log15.Record`.
12+
13+
- The CallerStackHandler, CallerFuncHandler, and CallerFileHandler
14+
have been removed (all three relied on the `Call` property).
15+
16+
- The `term` subpackage has been removed (there are multiple better replacements
17+
you can use now).
18+
19+
In most cases, however, it should be sufficient to just change your import path:
20+
21+
```patch
22+
- import log "github.com/inconshreveable/log15"
23+
+ import log "github.com/inconshreveable/log15/v3"
24+
```
25+
26+
A lot has happened since log15 was released; you may find the `log/slog` package
27+
in the standard library to be a better fit for your use case.
28+
29+
Development of the `v3` branch happens in the `v3` subdirectory.
30+
31+
We also added a compatibility layer, which allows existing code to use the v3
32+
library, so you can incrementally migrate.
33+
34+
```go
35+
import compat "github.com/inconshreveable/log15/compat"
36+
```
37+
38+
- `compat.CompatHandler` takes a v3 Handler, and returns a legacy log15 Handler.
39+
- `compat.CompatLogger` does the same with the Logger type.
40+
741
## Features
842
- A simple, easy-to-understand API
943
- Promotes structured logging by encouraging use of key/value pairs

bench_test.go

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build go1.3
12
// +build go1.3
23

34
package log15
@@ -31,33 +32,49 @@ func BenchmarkDiscard(b *testing.B) {
3132
}
3233
}
3334

35+
func BenchmarkCallerFileHandler(b *testing.B) {
36+
lg := New()
37+
lg.SetHandler(CallerFileHandler(DiscardHandler()))
38+
39+
for i := 0; i < b.N; i++ {
40+
lg.Info("test message")
41+
}
42+
}
43+
44+
func BenchmarkCallerFuncHandler(b *testing.B) {
45+
lg := New()
46+
lg.SetHandler(CallerFuncHandler(DiscardHandler()))
47+
48+
for i := 0; i < b.N; i++ {
49+
lg.Info("test message")
50+
}
51+
}
52+
3453
func BenchmarkLogfmtNoCtx(b *testing.B) {
3554
r := Record{
36-
Time: time.Now(),
37-
Lvl: LvlInfo,
38-
Msg: "test message",
39-
Ctx: []interface{}{},
40-
KeyNames: DefaultRecordKeyNames,
55+
Time: time.Now(),
56+
Lvl: LvlInfo,
57+
Msg: "test message",
58+
Ctx: []interface{}{},
4159
}
4260

4361
logfmt := LogfmtFormat()
4462
for i := 0; i < b.N; i++ {
45-
logfmt.Format(r)
63+
logfmt.Format(&r)
4664
}
4765
}
4866

4967
func BenchmarkJsonNoCtx(b *testing.B) {
5068
r := Record{
51-
Time: time.Now(),
52-
Lvl: LvlInfo,
53-
Msg: "test message",
54-
Ctx: []interface{}{},
55-
KeyNames: DefaultRecordKeyNames,
69+
Time: time.Now(),
70+
Lvl: LvlInfo,
71+
Msg: "test message",
72+
Ctx: []interface{}{},
5673
}
5774

5875
jsonfmt := JsonFormat()
5976
for i := 0; i < b.N; i++ {
60-
jsonfmt.Format(r)
77+
jsonfmt.Format(&r)
6178
}
6279
}
6380

@@ -189,32 +206,3 @@ func BenchmarkLog15WithoutFields(b *testing.B) {
189206
}
190207
})
191208
}
192-
193-
func BenchmarkDefaultWarn(b *testing.B) {
194-
logger := New("source", "default", "partition", 3)
195-
logger.SetHandler(newHandler())
196-
b.ReportAllocs()
197-
b.ResetTimer()
198-
b.RunParallel(func(pb *testing.PB) {
199-
for pb.Next() {
200-
logger.Warn("warn message")
201-
}
202-
})
203-
}
204-
func BenchmarkDefaultDebug(b *testing.B) {
205-
logger := New("source", "default", "partition", 3)
206-
logger.SetHandler(newHandler())
207-
b.ReportAllocs()
208-
b.ResetTimer()
209-
b.RunParallel(func(pb *testing.PB) {
210-
for pb.Next() {
211-
logger.Debug("debug message")
212-
}
213-
})
214-
}
215-
func newHandler() Handler {
216-
lvl := LvlWarn
217-
logformat := LogfmtFormat()
218-
handler := StreamHandler(ioutil.Discard, logformat)
219-
return LvlFilterHandler(lvl, handler)
220-
}

v3/compat.go renamed to compat/compat.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
1-
package log15
1+
// This file provides backwards compatibility with pre-v3 log15 code.
2+
//
3+
// The compatibility layer allows existing code that uses the legacy log15 API
4+
// to work seamlessly with the v3 version. It provides adapter types and functions
5+
// that translate between the old and new interfaces, enabling incremental migration
6+
// from legacy code to the new v3 API.
7+
//
8+
// Users may want this when:
9+
// - Migrating existing applications to use v3 gradually
10+
// - Working with third-party libraries that still use the legacy log15 API
11+
// - Maintaining backwards compatibility while adopting new v3 features
12+
//
13+
// Use CompatHandler() and CompatLogger() to wrap v3 types for legacy code.
14+
15+
package compat
216

317
import (
418
legacy "github.com/inconshreveable/log15"
19+
"github.com/inconshreveable/log15/v3"
520
)
621

722
type compat struct {
8-
Handler
9-
Logger
23+
log15.Handler
24+
log15.Logger
1025
}
1126

12-
func fromLegacyRecord(r *legacy.Record) Record {
13-
return Record{
27+
func fromLegacyRecord(r *legacy.Record) log15.Record {
28+
return log15.Record{
1429
Time: r.Time,
15-
Lvl: Lvl(r.Lvl),
30+
Lvl: log15.Lvl(r.Lvl),
1631
Msg: r.Msg,
1732
Ctx: r.Ctx,
18-
KeyNames: &RecordKeyNames{
33+
KeyNames: &log15.RecordKeyNames{
1934
Lvl: r.KeyNames.Lvl,
2035
Msg: r.KeyNames.Msg,
2136
Time: r.KeyNames.Time,
2237
},
2338
}
2439
}
2540

26-
func toLegacyRecord(r Record) *legacy.Record {
41+
func toLegacyRecord(r log15.Record) *legacy.Record {
2742
var keyNames legacy.RecordKeyNames
2843
if r.KeyNames != nil {
2944
keyNames.Lvl = r.KeyNames.Lvl
@@ -48,7 +63,7 @@ func (c *compat) GetHandler() legacy.Handler {
4863
}
4964

5065
func (c *compat) SetHandler(h legacy.Handler) {
51-
c.Logger.SetHandler(FuncHandler(func(r Record) error {
66+
c.Logger.SetHandler(log15.FuncHandler(func(r log15.Record) error {
5267
return h.Log(toLegacyRecord(r))
5368
}))
5469
}
@@ -58,12 +73,12 @@ func (c *compat) New(args ...interface{}) legacy.Logger {
5873
}
5974

6075
// CompatHandler wraps a handler for use with pre-v3 log15.Handler consumers.
61-
func CompatHandler(h Handler) legacy.Handler {
76+
func CompatHandler(h log15.Handler) legacy.Handler {
6277
return &compat{Handler: h}
6378
}
6479

6580
// CompatLogger wraps a Logger for use with pre-v3 log15.Logger consumers.
66-
func CompatLogger(l Logger) legacy.Logger {
81+
func CompatLogger(l log15.Logger) legacy.Logger {
6782
return &compat{Logger: l}
6883
}
6984

0 commit comments

Comments
 (0)