2020-02-15 09:56:46 +03:00
|
|
|
package zerolog
|
2020-02-11 13:49:33 +03:00
|
|
|
|
|
|
|
import (
|
2020-02-21 12:31:16 +03:00
|
|
|
"github.com/rs/zerolog"
|
2020-02-11 13:49:33 +03:00
|
|
|
|
2021-01-10 15:34:37 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-02-11 13:49:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
logger.Options
|
2020-02-21 12:31:16 +03:00
|
|
|
|
|
|
|
// Flag for whether to log caller info (off by default)
|
|
|
|
ReportCaller bool
|
|
|
|
// Use this logger as system wide default logger (off by default)
|
|
|
|
UseAsDefault bool
|
|
|
|
// zerolog hooks
|
|
|
|
Hooks []zerolog.Hook
|
|
|
|
// TimeFormat is one of time.RFC3339, time.RFC3339Nano, time.*
|
|
|
|
TimeFormat string
|
|
|
|
// Runtime mode. (Production by default)
|
|
|
|
Mode Mode
|
|
|
|
// Exit Function to call when FatalLevel log
|
|
|
|
ExitFunc func(int)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:04:50 +03:00
|
|
|
type reportCallerKey struct{}
|
|
|
|
|
|
|
|
func ReportCaller() logger.Option {
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.SetOption(reportCallerKey{}, true)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:04:50 +03:00
|
|
|
type useAsDefaultKey struct{}
|
|
|
|
|
|
|
|
func UseAsDefault() logger.Option {
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.SetOption(useAsDefaultKey{}, true)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:04:50 +03:00
|
|
|
type developmentModeKey struct{}
|
|
|
|
|
|
|
|
func WithDevelopmentMode() logger.Option {
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.SetOption(developmentModeKey{}, true)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
2020-02-14 08:04:50 +03:00
|
|
|
|
|
|
|
type productionModeKey struct{}
|
|
|
|
|
|
|
|
func WithProductionMode() logger.Option {
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.SetOption(productionModeKey{}, true)
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type timeFormatKey struct{}
|
|
|
|
|
|
|
|
func WithTimeFormat(timeFormat string) logger.Option {
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.SetOption(timeFormatKey{}, timeFormat)
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type hooksKey struct{}
|
|
|
|
|
2020-02-11 13:49:33 +03:00
|
|
|
func WithHooks(hooks []zerolog.Hook) logger.Option {
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.SetOption(hooksKey{}, hooks)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:04:50 +03:00
|
|
|
type exitKey struct{}
|
2020-02-11 13:49:33 +03:00
|
|
|
|
|
|
|
func WithExitFunc(exit func(int)) logger.Option {
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.SetOption(exitKey{}, exit)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
2021-01-10 15:34:37 +03:00
|
|
|
|
|
|
|
type loggerKey struct{}
|
|
|
|
|
|
|
|
func WithLogger(l logger.Logger) logger.Option {
|
|
|
|
return logger.SetOption(loggerKey{}, l)
|
|
|
|
}
|