Skip to content

Commit

Permalink
feat: Use s.Logger core if Noop
Browse files Browse the repository at this point in the history
  • Loading branch information
devanbenz committed Dec 27, 2024
1 parent 642aec4 commit f0c2c28
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/influxd/run/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (s *Server) Open() error {
}
if s.config.Data.QueryLogPath != "" {
path := s.config.Data.QueryLogPath
s.QueryExecutor.WithLogWriter(path)
s.QueryExecutor.WithLogWriter(s.Logger, path)
}

s.PointsWriter.WithLogger(s.Logger)
Expand Down
13 changes: 8 additions & 5 deletions query/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (e *Executor) WithLogger(log *zap.Logger) {
e.TaskManager.Logger = e.Logger
}

func initQueryLogWriter(e *Executor, path string) (*os.File, error) {
func initQueryLogWriter(log *zap.Logger, e *Executor, path string) (*os.File, error) {
logFile, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
e.Logger.Error("failed to open log file", zap.Error(err))
Expand All @@ -300,15 +300,18 @@ func initQueryLogWriter(e *Executor, path string) (*os.File, error) {

// If existing logger is a noop then we will just want to log to the provided file
if e.Logger == zap.NewNop() {
existingCore := log.Core()

encoderConfig := zap.NewProductionEncoderConfig()

core := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig),
zapcore.Lock(logFile),
zapcore.InfoLevel,
)
newCore := zapcore.NewTee(existingCore, core)

e.Logger = zap.New(core).With(zap.String("service", "query"))
e.Logger = zap.New(newCore).With(zap.String("service", "query"))
e.TaskManager.Logger = e.Logger
return logFile, nil
}
Expand All @@ -331,11 +334,11 @@ func initQueryLogWriter(e *Executor, path string) (*os.File, error) {
return logFile, nil
}

func (e *Executor) WithLogWriter(path string) {
func (e *Executor) WithLogWriter(log *zap.Logger, path string) {
var file *os.File
var err error

file, err = initQueryLogWriter(e, path)
file, err = initQueryLogWriter(log, e, path)
if err != nil {
e.Logger.Error("failed to open log file", zap.Error(err))
return
Expand Down Expand Up @@ -374,7 +377,7 @@ func (e *Executor) WithLogWriter(path string) {
}

e.Logger.Debug("creating a new query log file; registered file was altered.", zap.String("event", event.Name), zap.String("path", path))
file, err = initQueryLogWriter(e, path)
file, err = initQueryLogWriter(log, e, path)
if err != nil {
e.Logger.Error("failed to create log file watcher", zap.Error(err))
return
Expand Down

0 comments on commit f0c2c28

Please sign in to comment.