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

feat: add custom logger for producer #294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions producer/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"io"
"os"
)

func logConfig(producerConfig *ProducerConfig) log.Logger {
Expand All @@ -27,7 +27,9 @@ func logConfig(producerConfig *ProducerConfig) log.Logger {
}
}
var logger log.Logger
if producerConfig.IsJsonType {
if producerConfig.Logger != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If custom logger is not nil, maybe we can just skip logConfig?

var logger log.Logger
if producerConfig.Logger != nil {
     logger = producerConfig.Logger
} else {
    logger = logConfig(producerConfig)
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we need the level filter in logConfig.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok, you could wrapper it yourself before passing to producerConfig, NewFilter also returns a new logger.
You can do anything you like

yourLogger := log.NewJSONLogger(writer)
yourLogger = level.NewFilter(yourLogger, level.AllowDebug())
yourLogger = log.With(yourLogger, "time", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
yourLogger = log.With(yourLogger, "hello", "this is my machine 1.1.1.1")

Then you can pass it to producerConfig

producerConfig := GetDefaultProducerConfig()
producerConfig.Logger = yourLogger

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, if we modify the logger, it may override your settings unexpectedly

logger = producerConfig.Logger
} else if producerConfig.IsJsonType {
logger = log.NewJSONLogger(writer)
} else {
logger = log.NewLogfmtLogger(writer)
Expand Down
2 changes: 2 additions & 0 deletions producer/producer_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package producer

import (
"github.com/go-kit/kit/log"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -58,6 +59,7 @@ type ProducerConfig struct {
Region string
AuthVersion sls.AuthVersionType
CompressType int // only work for logstore now
Logger log.Logger
}

func GetDefaultProducerConfig() *ProducerConfig {
Expand Down