Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom log.Logger #474

Merged
merged 2 commits into from May 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions promlog/log.go
Expand Up @@ -111,13 +111,16 @@ type Config struct {
// New returns a new leveled oklog logger. Each logged line will be annotated
// with a timestamp. The output always goes to stderr.
func New(config *Config) log.Logger {
var l log.Logger
if config.Format != nil && config.Format.s == "json" {
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
jkroepke marked this conversation as resolved.
Show resolved Hide resolved
} else {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
return NewWithLogger(log.NewJSONLogger(log.NewSyncWriter(os.Stderr)), config)
}

return NewWithLogger(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)), config)
}

// NewWithLogger returns a new leveled oklog logger with a custom log.Logger.
// Each logged line will be annotated with a timestamp.
func NewWithLogger(l log.Logger, config *Config) log.Logger {
if config.Level != nil {
l = log.With(l, "ts", timestampFormat, "caller", log.Caller(5))
l = level.NewFilter(l, config.Level.o)
Expand All @@ -131,13 +134,17 @@ func New(config *Config) log.Logger {
// with a timestamp. The output always goes to stderr. Some properties can be
// changed, like the level.
func NewDynamic(config *Config) *logger {
var l log.Logger
if config.Format != nil && config.Format.s == "json" {
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
} else {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
return NewDynamicWithLogger(log.NewJSONLogger(log.NewSyncWriter(os.Stderr)), config)
}

return NewDynamicWithLogger(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)), config)
}

// NewDynamicWithLogger returns a new leveled logger with a custom io.Writer.
// Each logged line will be annotated with a timestamp.
// Some properties can be changed, like the level.
func NewDynamicWithLogger(l log.Logger, config *Config) *logger {
lo := &logger{
base: l,
leveled: l,
Expand Down