Skip to content

Commit

Permalink
log opts getter and update print
Browse files Browse the repository at this point in the history
  • Loading branch information
veceraj committed Dec 16, 2024
1 parent 980d6d9 commit b30712f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
6 changes: 6 additions & 0 deletions cmd/pbm-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ func runAgent(
defer logger.Close()

viper.OnConfigChange(func(e fsnotify.Event) {
oldOpts := logger.Opts()
logger.SetOpts(buildLogOpts())
newOpts := logger.Opts()
if *oldOpts != *newOpts {
logger.Printf("log options updated: log-path=%s, log-level:%s, log-json:%t",
newOpts.LogPath, newOpts.LogLevel, newOpts.LogJSON)
}
})

ctx = log.SetLoggerToContext(ctx, logger)
Expand Down
2 changes: 2 additions & 0 deletions pbm/log/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func (discardLoggerImpl) Output(ctx context.Context, e *Entry) error {
return nil
}

func (discardLoggerImpl) Opts() *Opts { return nil }

func (discardLoggerImpl) SetOpts(cfg *Opts) {
}

Expand Down
1 change: 1 addition & 0 deletions pbm/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Logger interface {
Error(event, obj, opid string, epoch primitive.Timestamp, msg string, args ...any)
Fatal(event, obj, opid string, epoch primitive.Timestamp, msg string, args ...any)
Output(ctx context.Context, e *Entry) error
Opts() *Opts
SetOpts(cfg *Opts)
}

Expand Down
24 changes: 8 additions & 16 deletions pbm/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ func (l *loggerImpl) Output(ctx context.Context, e *Entry) error {
return rerr
}

func (l *loggerImpl) Opts() *Opts {
return &Opts{
LogPath: l.logger.logPath,
LogJSON: l.logJSON,
LogLevel: l.logLevel.String(),
}
}

func (l *loggerImpl) SetOpts(cfg *Opts) {
if cfg == nil {
return
Expand All @@ -314,12 +322,6 @@ func (l *loggerImpl) SetOpts(cfg *Opts) {
l.mu.Lock()
defer l.mu.Unlock()

oldOpts := &Opts{
LogPath: l.logger.logPath,
LogJSON: l.logJSON,
LogLevel: l.logLevel.String(),
}

if cfg.LogPath != "" && l.logger.logPath != cfg.LogPath {
l.createLogger(cfg.LogPath)
}
Expand All @@ -329,14 +331,4 @@ func (l *loggerImpl) SetOpts(cfg *Opts) {
if l.logJSON != cfg.LogJSON {
l.logJSON = cfg.LogJSON
}

newOpts := &Opts{
LogPath: l.logger.logPath,
LogJSON: l.logJSON,
LogLevel: l.logLevel.String(),
}

if *oldOpts != *newOpts {
fmt.Printf("Updated log options: %+v\n", newOpts)
}
}

0 comments on commit b30712f

Please sign in to comment.