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

simplify log level string handling #122

Merged
merged 1 commit into from
Feb 21, 2015
Merged
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
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func (c *Conn) log(lvl LogLevel, line string, args ...interface{}) {
return
}

logger.Output(2, fmt.Sprintf("%-4s %s %s", logPrefix(lvl),
logger.Output(2, fmt.Sprintf("%-4s %s %s", lvl,
fmt.Sprintf(logFmt, c.String()),
fmt.Sprintf(line, args...)))
}
2 changes: 1 addition & 1 deletion consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,6 @@ func (r *Consumer) log(lvl LogLevel, line string, args ...interface{}) {
}

logger.Output(2, fmt.Sprintf("%-4s %3d [%s/%s] %s",
logPrefix(lvl), r.id, r.topic, r.channel,
lvl, r.id, r.topic, r.channel,
fmt.Sprintf(line, args...)))
}
29 changes: 9 additions & 20 deletions delegates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,31 @@ package nsq

import "time"

// LogLevel specifies the severity of a given log message
type LogLevel int

type logger interface {
Output(calldepth int, s string) error
}

// logging constants
// LogLevel specifies the severity of a given log message
type LogLevel int

const (
LogLevelDebug LogLevel = iota
LogLevelInfo
LogLevelWarning
LogLevelError

LogLevelDebugPrefix = "DBG"
LogLevelInfoPrefix = "INF"
LogLevelWarningPrefix = "WRN"
LogLevelErrorPrefix = "ERR"
)

// LogPrefix Resolution
func logPrefix(lvl LogLevel) string {
var prefix string

// String returns the string form for a given LogLevel
func (lvl LogLevel) String() string {
switch lvl {
case LogLevelDebug:
prefix = LogLevelDebugPrefix
case LogLevelInfo:
prefix = LogLevelInfoPrefix
return "INF"
case LogLevelWarning:
prefix = LogLevelWarningPrefix
return "WRN"
case LogLevelError:
prefix = LogLevelErrorPrefix
return "ERR"
}

return prefix
return "DBG"
}

// MessageDelegate is an interface of methods that are used as
Expand Down
2 changes: 1 addition & 1 deletion producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (w *Producer) log(lvl LogLevel, line string, args ...interface{}) {
return
}

logger.Output(2, fmt.Sprintf("%-4s %3d %s", logPrefix(lvl), w.id, fmt.Sprintf(line, args...)))
logger.Output(2, fmt.Sprintf("%-4s %3d %s", lvl, w.id, fmt.Sprintf(line, args...)))
}

func (w *Producer) onConnResponse(c *Conn, data []byte) { w.responseChan <- data }
Expand Down