Skip to content

Commit

Permalink
Some changes with caller configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
smgladkovskiy committed Jan 18, 2021
1 parent 0d761f9 commit 707432d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
15 changes: 10 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ const (
FormatJSON = "json"
)

type CallerConfig struct {
Disabled bool `yaml:"hide_caller"`
CallerSkipFrames int `yaml:"skip_frames"`
}

type Config struct {
Level string `yaml:"level"`
Format Format `yaml:"format"`
NoColor bool `yaml:"no_color"`
ShowCaller bool `yaml:"show_caller"`
Sentry *SentryConfig `yaml:"sentry,omitempty"`
Level string `yaml:"level"`
Format Format `yaml:"format"`
NoColor bool `yaml:"no_color"`
Caller *CallerConfig `yaml:"caller"`
Sentry *SentryConfig `yaml:"sentry,omitempty"`
}

type SentryConfig struct {
Expand Down
6 changes: 4 additions & 2 deletions configuration/defaults/logs.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defaults:
logs:
log:
level: debug
format: text
no_color: false
show_caller: true
caller:
hide_caller: false
skip_frames: 2
sentry:
enable: false
dsn: ""
23 changes: 19 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

// logger is the global logger.
var logger, _ = newZerolog(Config{Level: "debug", Format: FormatText}, os.Stdout)
var logger, _ = newZerolog(Config{Level: "debug", Format: FormatText, Caller: &CallerConfig{CallerSkipFrames: 6}}, os.Stdout)

// set global Zerolog logger
func Init(stage string, cfg Config, serviceAlias string, serviceVersion string, w io.Writer) (err error) {
Expand All @@ -33,6 +33,12 @@ func Init(stage string, cfg Config, serviceAlias string, serviceVersion string,
cfg.Format = FormatText
}

if cfg.Caller == nil {
cfg.Caller = &CallerConfig{
CallerSkipFrames: 2,
}
}

if cfg.Sentry == nil || !cfg.Sentry.Enable || cfg.Sentry.DSN == "" {
logger, err = newZerolog(cfg, w)
return err
Expand Down Expand Up @@ -82,7 +88,7 @@ func newZerolog(cfg Config, w io.Writer) (logger zerolog.Logger, err error) {
zerolog.TimeFieldFormat = time.RFC3339Nano

// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
zerolog.CallerSkipFrameCount = 2
zerolog.CallerSkipFrameCount = cfg.Caller.CallerSkipFrames

output := w

Expand All @@ -94,7 +100,10 @@ func newZerolog(cfg Config, w io.Writer) (logger zerolog.Logger, err error) {
zerolog.TimestampFieldName,
zerolog.LevelFieldName,
zerolog.MessageFieldName,
zerolog.CallerFieldName,
}

if !cfg.Caller.Disabled {
out.PartsOrder = append(out.PartsOrder, zerolog.CallerFieldName)
}

out.FormatMessage = func(i interface{}) string {
Expand All @@ -113,7 +122,13 @@ func newZerolog(cfg Config, w io.Writer) (logger zerolog.Logger, err error) {
return logger, err
}

logger = zerolog.New(output).With().Timestamp().Caller().Logger().Level(level)
lctx := zerolog.New(output).With().Timestamp()

if !cfg.Caller.Disabled {
lctx = lctx.Caller()
}

logger = lctx.Logger().Level(level)

stdlog.SetFlags(0)
stdlog.SetOutput(logger)
Expand Down

0 comments on commit 707432d

Please sign in to comment.