Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 1ddddcb

Browse files
authored
🦑 Prepare v0.11.8 release (#17)
* 🦑 Prepare v0.11.8 release - `logger.trace_level` - simplify `NewLogger` - replace go-convey with `testify/require` - add to README - fix golint warnings
1 parent c2ff5d3 commit 1ddddcb

File tree

5 files changed

+151
-139
lines changed

5 files changed

+151
-139
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ debug: true
111111
logger:
112112
format: console
113113
level: info
114+
trace_level: fatal
114115
no_disclaimer: false
115116
color: true
116117
full_caller: true
@@ -126,12 +127,14 @@ LOGGER_COLOR=true
126127
LOGGER_FULL_CALLER=true
127128
LOGGER_FORMAT=console
128129
LOGGER_LEVEL=info
130+
LOGGER_TRACE_LEVEL=fatal
129131
LOGGER_SAMPLING_INITIAL=100
130132
LOGGER_SAMPLING_THEREAFTER=100
131133
```
132134

133135
- `debug` - with this option you can enable `zap.DevelopmentConfig()`
134136
- `logger.no_disclaimer` - with this option, you can disable `app_name` and `app_version` for any reason (not recommended in production)
137+
- `logger.trace_level` - configures the Logger to record a stack trace for all messages at or above a given level
135138
- `logger.color` - serializes a Level to an all-caps string and adds color
136139
- `logger.full_caller` - serializes a caller in /full/path/to/package/file:line format
137140
- `logger.sampling.initial` and `logger.sampling.thereafter` to setup [logger sampling](https://godoc.org/go.uber.org/zap#SamplingConfig). SamplingConfig sets a sampling strategy for the logger. Sampling caps the global CPU and I/O load that logging puts on your process while attempting to preserve a representative subset of your logs. Values configured here are per-second. See [zapcore.NewSampler](https://godoc.org/go.uber.org/zap/zapcore#NewSampler) for details.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/golang/protobuf v1.3.1 // indirect
1111
github.com/onsi/ginkgo v1.8.0 // indirect
1212
github.com/onsi/gomega v1.5.0 // indirect
13-
github.com/pkg/errors v0.8.1 // indirect
13+
github.com/pkg/errors v0.8.1
1414
github.com/prometheus/client_golang v0.9.2
1515
github.com/prometheus/common v0.3.0 // indirect
1616
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967 // indirect

logger/std_test.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,36 @@ import (
55
"testing"
66

77
"bou.ke/monkey"
8-
. "github.com/smartystreets/goconvey/convey"
8+
"github.com/stretchr/testify/require"
99
"go.uber.org/zap"
1010
)
1111

1212
func TestStdLogger(t *testing.T) {
13-
Convey("StdLogger test suite", t, func(c C) {
14-
std := NewStdLogger(zap.L())
13+
std := NewStdLogger(zap.L())
1514

16-
c.Convey("Not fatal calls should not panic", func(c C) {
17-
c.Convey("print", func(c C) {
18-
c.So(func() { std.Print("panic no") }, ShouldNotPanic)
19-
})
20-
21-
c.Convey("printf", func(c C) {
22-
c.So(func() { std.Printf("panic %s", "no") }, ShouldNotPanic)
23-
})
15+
t.Run("not fatal calls should not panic", func(t *testing.T) {
16+
t.Run("print", func(t *testing.T) {
17+
require.NotPanics(t, func() { std.Print("panic no") })
18+
})
2419

20+
t.Run("printf", func(t *testing.T) {
21+
require.NotPanics(t, func() { std.Printf("panic no") })
2522
})
23+
})
2624

27-
c.Convey("Fatal(f) should call os.Exit with 1 code", func(c C) {
28-
var exitCode int
29-
monkey.Patch(os.Exit, func(code int) { exitCode = code })
30-
defer monkey.Unpatch(os.Exit)
25+
t.Run("Fatal(f) should call os.Exit with 1 code", func(t *testing.T) {
26+
var exitCode int
27+
monkey.Patch(os.Exit, func(code int) { exitCode = code })
28+
defer monkey.Unpatch(os.Exit)
3129

32-
c.Convey("Fatal", func(c C) {
33-
c.So(func() { std.Fatal("panic no") }, ShouldNotPanic)
34-
c.So(exitCode, ShouldEqual, 1)
35-
})
30+
t.Run("fatal", func(t *testing.T) {
31+
require.NotPanics(t, func() { std.Fatal("panic no") })
32+
require.Equal(t, 1, exitCode)
33+
})
3634

37-
c.Convey("Fatalf", func(c C) {
38-
c.So(func() { std.Fatalf("panic %s", "no") }, ShouldNotPanic)
39-
c.So(exitCode, ShouldEqual, 1)
40-
})
35+
t.Run("fatalf", func(t *testing.T) {
36+
require.NotPanics(t, func() { std.Fatalf("panic no") })
37+
require.Equal(t, 1, exitCode)
4138
})
4239
})
4340
}

logger/zap.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package logger
22

33
import (
4+
"strings"
5+
46
"github.com/im-kulikov/helium/settings"
57
"github.com/spf13/viper"
68
"go.uber.org/zap"
@@ -10,6 +12,7 @@ import (
1012
// Config for logger
1113
type Config struct {
1214
Level string
15+
TraceLevel string
1316
Format string
1417
Debug bool
1518
Color bool
@@ -28,6 +31,7 @@ func NewLoggerConfig(v *viper.Viper) *Config {
2831
cfg := &Config{
2932
Debug: v.GetBool("debug"),
3033
Level: v.GetString("logger.level"),
34+
TraceLevel: v.GetString("logger.trace_level"),
3135
Format: v.GetString("logger.format"),
3236
Color: v.GetBool("logger.color"),
3337
FullCaller: v.GetBool("logger.full_caller"),
@@ -52,20 +56,24 @@ func NewLoggerConfig(v *viper.Viper) *Config {
5256
return cfg
5357
}
5458

55-
// SafeLevel returns valid logger level
56-
// use info level by default
57-
func (c Config) SafeLevel() string {
58-
switch c.Level {
59-
case "debug", "DEBUG":
60-
case "info", "INFO":
61-
case "warn", "WARN":
62-
case "error", "ERROR":
63-
case "panic", "PANIC":
64-
case "fatal", "FATAL":
59+
// SafeLevel returns valid logger level or default
60+
func SafeLevel(lvl string, defaultLvl zapcore.Level) zap.AtomicLevel {
61+
switch strings.ToLower(lvl) {
62+
case "debug":
63+
return zap.NewAtomicLevelAt(zapcore.DebugLevel)
64+
case "info":
65+
return zap.NewAtomicLevelAt(zapcore.InfoLevel)
66+
case "warn":
67+
return zap.NewAtomicLevelAt(zapcore.WarnLevel)
68+
case "error":
69+
return zap.NewAtomicLevelAt(zapcore.ErrorLevel)
70+
case "panic":
71+
return zap.NewAtomicLevelAt(zapcore.PanicLevel)
72+
case "fatal":
73+
return zap.NewAtomicLevelAt(zapcore.FatalLevel)
6574
default:
66-
return "info"
75+
return zap.NewAtomicLevelAt(defaultLvl)
6776
}
68-
return c.Level
6977
}
7078

7179
// SafeFormat returns valid logger output format
@@ -112,13 +120,12 @@ func NewLogger(lcfg *Config, app *settings.Core) (*zap.Logger, error) {
112120
cfg.EncoderConfig.EncodeCaller = zapcore.FullCallerEncoder
113121
}
114122

115-
var lvl zapcore.Level
116-
if err := lvl.Set(lcfg.Level); err != nil {
117-
return nil, err
118-
}
119-
cfg.Level = zap.NewAtomicLevelAt(lvl)
123+
cfg.Level = SafeLevel(lcfg.Level, zapcore.InfoLevel)
124+
traceLevel := SafeLevel(lcfg.TraceLevel, zapcore.WarnLevel)
120125

121-
l, err := cfg.Build()
126+
l, err := cfg.Build(
127+
// enable trace only for current log-level
128+
zap.AddStacktrace(traceLevel))
122129
if err != nil {
123130
return nil, err
124131
}

0 commit comments

Comments
 (0)