Skip to content

Commit

Permalink
feat: Change any to interface{} to support Go 1.17.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamd3vil authored and mr-karan committed Jul 18, 2022
1 parent ad4369d commit 0c2345f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ name: build
on:
push:
branches:
- 'main'
- "main"
tags:
- 'v*'
- "v*"
pull_request:

jobs:
build:
strategy:
matrix:
go-version: [~1.18]
os: [ ubuntu-latest, macos-latest, windows-latest ]
go-version: [~1.17, ~1.18]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -37,4 +37,4 @@ jobs:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func BenchmarkOneField(b *testing.B) {
}

func BenchmarkOneFieldWithDefaultFields(b *testing.B) {
logger := logf.New(logf.Opts{Writer: io.Discard, DefaultFields: []any{"component", "logf"}})
logger := logf.New(logf.Opts{Writer: io.Discard, DefaultFields: []interface{}{"component", "logf"}})
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
Expand Down
20 changes: 10 additions & 10 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Opts struct {
CallerSkipFrameCount int

// These fields will be printed with every log.
DefaultFields []any
DefaultFields []interface{}
}

// Logger is the interface for all log operations
Expand Down Expand Up @@ -158,35 +158,35 @@ func LevelFromString(lvl string) (Level, error) {
}

// Debug emits a debug log line.
func (l Logger) Debug(msg string, fields ...any) {
func (l Logger) Debug(msg string, fields ...interface{}) {
l.handleLog(msg, DebugLevel, fields...)
}

// Info emits a info log line.
func (l Logger) Info(msg string, fields ...any) {
func (l Logger) Info(msg string, fields ...interface{}) {
l.handleLog(msg, InfoLevel, fields...)
}

// Warn emits a warning log line.
func (l Logger) Warn(msg string, fields ...any) {
func (l Logger) Warn(msg string, fields ...interface{}) {
l.handleLog(msg, WarnLevel, fields...)
}

// Error emits an error log line.
func (l Logger) Error(msg string, fields ...any) {
func (l Logger) Error(msg string, fields ...interface{}) {
l.handleLog(msg, ErrorLevel, fields...)
}

// Fatal emits a fatal level log line.
// It aborts the current program with an exit code of 1.
func (l Logger) Fatal(msg string, fields ...any) {
func (l Logger) Fatal(msg string, fields ...interface{}) {
l.handleLog(msg, FatalLevel, fields...)
exit()
}

// handleLog emits the log after filtering log level
// and applying formatting of the fields.
func (l Logger) handleLog(msg string, lvl Level, fields ...any) {
func (l Logger) handleLog(msg string, lvl Level, fields ...interface{}) {
// Discard the log if the verbosity is higher.
// For eg, if the lvl is `3` (error), but the incoming message is `0` (debug), skip it.
if lvl < l.Opts.Level {
Expand All @@ -210,7 +210,7 @@ func (l Logger) handleLog(msg string, lvl Level, fields ...any) {
count int // to find out if this is the last key in while itering fields.
fieldCount = len(l.DefaultFields) + len(fields)
key string
val any
val interface{}
)

// If there are odd number of fields, ignore the last.
Expand Down Expand Up @@ -310,7 +310,7 @@ func writeCallerToBuf(buf *byteBuffer, key string, depth int, lvl Level, color,
}

// writeToBuf takes key, value and additional options to write to the buffer in logfmt.
func writeToBuf(buf *byteBuffer, key string, val any, lvl Level, color, space bool) {
func writeToBuf(buf *byteBuffer, key string, val interface{}, lvl Level, color, space bool) {
if color {
escapeAndWriteString(buf, getColoredKey(key, lvl))
} else {
Expand Down Expand Up @@ -354,7 +354,7 @@ func writeToBuf(buf *byteBuffer, key string, val any, lvl Level, color, space bo
}
}

// escapeAndWriteString escapes the string if any unwanted chars are there.
// escapeAndWriteString escapes the string if interface{} unwanted chars are there.
func escapeAndWriteString(buf *byteBuffer, s string) {
idx := strings.IndexFunc(s, checkEscapingRune)
if idx != -1 || s == "null" {
Expand Down
4 changes: 2 additions & 2 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestLoggerTypes(t *testing.T) {

func TestLogFormatWithDefaultFields(t *testing.T) {
buf := &bytes.Buffer{}
l := New(Opts{Writer: buf, DefaultFields: []any{"defaultkey", "defaultvalue"}})
l := New(Opts{Writer: buf, DefaultFields: []interface{}{"defaultkey", "defaultvalue"}})

l.Info("hello world")
require.Contains(t, buf.String(), `level=info message="hello world" defaultkey=defaultvalue`)
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestOddNumberedFields(t *testing.T) {

func TestOddNumberedFieldsWithDefaultFields(t *testing.T) {
buf := &bytes.Buffer{}
l := New(Opts{Writer: buf, DefaultFields: []any{
l := New(Opts{Writer: buf, DefaultFields: []interface{}{
"defaultkey", "defaultval",
}})

Expand Down

0 comments on commit 0c2345f

Please sign in to comment.