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

Commit

Permalink
🦑 Prepare v0.11.8 release (#17)
Browse files Browse the repository at this point in the history
* 🦑 Prepare v0.11.8 release

- `logger.trace_level`
- simplify `NewLogger`
- replace go-convey with `testify/require`
- add to README
- fix golint warnings
  • Loading branch information
im-kulikov authored Apr 25, 2019
1 parent c2ff5d3 commit 1ddddcb
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 139 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ debug: true
logger:
format: console
level: info
trace_level: fatal
no_disclaimer: false
color: true
full_caller: true
Expand All @@ -126,12 +127,14 @@ LOGGER_COLOR=true
LOGGER_FULL_CALLER=true
LOGGER_FORMAT=console
LOGGER_LEVEL=info
LOGGER_TRACE_LEVEL=fatal
LOGGER_SAMPLING_INITIAL=100
LOGGER_SAMPLING_THEREAFTER=100
```

- `debug` - with this option you can enable `zap.DevelopmentConfig()`
- `logger.no_disclaimer` - with this option, you can disable `app_name` and `app_version` for any reason (not recommended in production)
- `logger.trace_level` - configures the Logger to record a stack trace for all messages at or above a given level
- `logger.color` - serializes a Level to an all-caps string and adds color
- `logger.full_caller` - serializes a caller in /full/path/to/package/file:line format
- `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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/golang/protobuf v1.3.1 // indirect
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.2
github.com/prometheus/common v0.3.0 // indirect
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967 // indirect
Expand Down
43 changes: 20 additions & 23 deletions logger/std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,36 @@ import (
"testing"

"bou.ke/monkey"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

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

c.Convey("Not fatal calls should not panic", func(c C) {
c.Convey("print", func(c C) {
c.So(func() { std.Print("panic no") }, ShouldNotPanic)
})

c.Convey("printf", func(c C) {
c.So(func() { std.Printf("panic %s", "no") }, ShouldNotPanic)
})
t.Run("not fatal calls should not panic", func(t *testing.T) {
t.Run("print", func(t *testing.T) {
require.NotPanics(t, func() { std.Print("panic no") })
})

t.Run("printf", func(t *testing.T) {
require.NotPanics(t, func() { std.Printf("panic no") })
})
})

c.Convey("Fatal(f) should call os.Exit with 1 code", func(c C) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
defer monkey.Unpatch(os.Exit)
t.Run("Fatal(f) should call os.Exit with 1 code", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
defer monkey.Unpatch(os.Exit)

c.Convey("Fatal", func(c C) {
c.So(func() { std.Fatal("panic no") }, ShouldNotPanic)
c.So(exitCode, ShouldEqual, 1)
})
t.Run("fatal", func(t *testing.T) {
require.NotPanics(t, func() { std.Fatal("panic no") })
require.Equal(t, 1, exitCode)
})

c.Convey("Fatalf", func(c C) {
c.So(func() { std.Fatalf("panic %s", "no") }, ShouldNotPanic)
c.So(exitCode, ShouldEqual, 1)
})
t.Run("fatalf", func(t *testing.T) {
require.NotPanics(t, func() { std.Fatalf("panic no") })
require.Equal(t, 1, exitCode)
})
})
}
43 changes: 25 additions & 18 deletions logger/zap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package logger

import (
"strings"

"github.com/im-kulikov/helium/settings"
"github.com/spf13/viper"
"go.uber.org/zap"
Expand All @@ -10,6 +12,7 @@ import (
// Config for logger
type Config struct {
Level string
TraceLevel string
Format string
Debug bool
Color bool
Expand All @@ -28,6 +31,7 @@ func NewLoggerConfig(v *viper.Viper) *Config {
cfg := &Config{
Debug: v.GetBool("debug"),
Level: v.GetString("logger.level"),
TraceLevel: v.GetString("logger.trace_level"),
Format: v.GetString("logger.format"),
Color: v.GetBool("logger.color"),
FullCaller: v.GetBool("logger.full_caller"),
Expand All @@ -52,20 +56,24 @@ func NewLoggerConfig(v *viper.Viper) *Config {
return cfg
}

// SafeLevel returns valid logger level
// use info level by default
func (c Config) SafeLevel() string {
switch c.Level {
case "debug", "DEBUG":
case "info", "INFO":
case "warn", "WARN":
case "error", "ERROR":
case "panic", "PANIC":
case "fatal", "FATAL":
// SafeLevel returns valid logger level or default
func SafeLevel(lvl string, defaultLvl zapcore.Level) zap.AtomicLevel {
switch strings.ToLower(lvl) {
case "debug":
return zap.NewAtomicLevelAt(zapcore.DebugLevel)
case "info":
return zap.NewAtomicLevelAt(zapcore.InfoLevel)
case "warn":
return zap.NewAtomicLevelAt(zapcore.WarnLevel)
case "error":
return zap.NewAtomicLevelAt(zapcore.ErrorLevel)
case "panic":
return zap.NewAtomicLevelAt(zapcore.PanicLevel)
case "fatal":
return zap.NewAtomicLevelAt(zapcore.FatalLevel)
default:
return "info"
return zap.NewAtomicLevelAt(defaultLvl)
}
return c.Level
}

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

var lvl zapcore.Level
if err := lvl.Set(lcfg.Level); err != nil {
return nil, err
}
cfg.Level = zap.NewAtomicLevelAt(lvl)
cfg.Level = SafeLevel(lcfg.Level, zapcore.InfoLevel)
traceLevel := SafeLevel(lcfg.TraceLevel, zapcore.WarnLevel)

l, err := cfg.Build()
l, err := cfg.Build(
// enable trace only for current log-level
zap.AddStacktrace(traceLevel))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 1ddddcb

Please sign in to comment.