-
-
Save michaeltrobinson/4d521cb4267bcf7bda9b0c775df4a440 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"strings" | |
log "github.com/Sirupsen/logrus" | |
"github.com/nsqio/go-nsq" | |
) | |
var ( | |
nsqDebugLevel = nsq.LogLevelDebug.String() | |
nsqInfoLevel = nsq.LogLevelInfo.String() | |
nsqWarnLevel = nsq.LogLevelWarning.String() | |
nsqErrLevel = nsq.LogLevelError.String() | |
) | |
// NSQLogrusLogger is an adaptor between the weird go-nsq Logger and our | |
// standard logrus logger. | |
type NSQLogrusLogger struct{} | |
// NewNSQLogrusLogger returns a new NSQLogrusLogger and the current log level. | |
// This is a format to easily plug into nsq.SetLogger. | |
func NewNSQLogrusLogger() (logger NSQLogrusLogger, level nsq.LogLevel) { | |
return NewNSQLogrusLoggerAtLevel(log.GetLevel()) | |
} | |
// NewNSQLogrusLoggerAtLevel returns a new NSQLogrusLogger with the provided log level mapped to nsq.LogLevel for easily plugging into nsq.SetLogger. | |
func NewNSQLogrusLoggerAtLevel(l log.Level) (logger NSQLogrusLogger, level nsq.LogLevel) { | |
logger = NSQLogrusLogger{} | |
level = nsq.LogLevelWarning | |
switch l { | |
case log.DebugLevel: | |
level = nsq.LogLevelDebug | |
case log.InfoLevel: | |
level = nsq.LogLevelInfo | |
case log.WarnLevel: | |
level = nsq.LogLevelWarning | |
case log.ErrorLevel: | |
level = nsq.LogLevelError | |
} | |
return | |
} | |
// Output implements stdlib log.Logger.Output using logrus | |
// Decodes the go-nsq log messages to figure out the log level | |
func (n NSQLogrusLogger) Output(_ int, s string) error { | |
if len(s) > 3 { | |
msg := strings.TrimSpace(s[3:]) | |
switch s[:3] { | |
case nsqDebugLevel: | |
log.Debugln(msg) | |
case nsqInfoLevel: | |
log.Infoln(msg) | |
case nsqWarnLevel: | |
log.Warnln(msg) | |
case nsqErrLevel: | |
log.Errorln(msg) | |
default: | |
log.Infoln(msg) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You would use it like this (assuming you already have a consumer/producer object):