Skip to content

Commit

Permalink
Merge pull request #281 from ploxiln/refactor_logfmt
Browse files Browse the repository at this point in the history
minor cleanup of log format handling for per-level logger
  • Loading branch information
jehiah authored Jan 13, 2020
2 parents 0abe494 + 86c3138 commit d9600b3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
9 changes: 6 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func (c *Conn) SetLogger(l logger, lvl LogLevel, format string) {
c.logGuard.Lock()
defer c.logGuard.Unlock()

if format == "" {
format = "(%s)"
}
for level := range c.logger {
c.logger[level] = l
c.logFmt[level] = format
if c.logFmt[level] == "" {
c.logFmt[level] = "(%s)"
}
}
c.logLvl = lvl
}
Expand All @@ -139,6 +139,9 @@ func (c *Conn) SetLoggerForLevel(l logger, lvl LogLevel, format string) {
c.logGuard.Lock()
defer c.logGuard.Unlock()

if format == "" {
format = "(%s)"
}
c.logger[lvl] = l
c.logFmt[lvl] = format
}
Expand Down
4 changes: 2 additions & 2 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,9 @@ func (r *Consumer) ConnectToNSQD(addr string) error {

conn := NewConn(addr, &r.config, &consumerConnDelegate{r})
conn.SetLoggerLevel(r.getLogLevel())
format := fmt.Sprintf("%3d [%s/%s] (%%s)", r.id, r.topic, r.channel)
for index := range r.logger {
conn.SetLoggerForLevel(r.logger[index], LogLevel(index),
fmt.Sprintf("%3d [%s/%s] (%%s)", r.id, r.topic, r.channel))
conn.SetLoggerForLevel(r.logger[index], LogLevel(index), format)
}
r.mtx.Lock()
_, pendingOk := r.pendingConnections[addr]
Expand Down
3 changes: 2 additions & 1 deletion producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ func (w *Producer) connect() error {

w.conn = NewConn(w.addr, &w.config, &producerConnDelegate{w})
w.conn.SetLoggerLevel(w.getLogLevel())
format := fmt.Sprintf("%3d (%%s)", w.id)
for index := range w.logger {
w.conn.SetLoggerForLevel(w.logger[index], LogLevel(index), fmt.Sprintf("%3d (%%s)", w.id))
w.conn.SetLoggerForLevel(w.logger[index], LogLevel(index), format)
}

_, err := w.conn.Connect()
Expand Down

0 comments on commit d9600b3

Please sign in to comment.