Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pfortin-urbn/538a16939fea62a21cb1275ceaf4b560 to your computer and use it in GitHub Desktop.
Save pfortin-urbn/538a16939fea62a21cb1275ceaf4b560 to your computer and use it in GitHub Desktop.
logger.go
package logging
import (
"io/ioutil"
"os"
"github.com/lovoo/goka/logger"
log "github.com/sirupsen/logrus"
"github.com/urbn/gourbnkit/v2/urbnlog"
"atp_router/app"
)
type LogrusLogger interface {
WithFields(map[string]interface{}) AtpLogger
Infoln(...interface{})
Infof(string, ...interface{})
Info(string)
Warning(string)
Errorf(string, ...interface{})
Errorln(...interface{})
Error(string)
Panic(...interface{})
}
type AtpLogger interface {
logger.Logger
LogrusLogger
}
// std bridges the logger calls to the logrus library log.
type std struct {
entry *log.Entry
}
func (s *std) Print(i ...interface{}) {
s.entry.Print(i...)
}
func (s *std) Println(i ...interface{}) {
s.entry.Println(i...)
}
func (s *std) Printf(s2 string, i ...interface{}) {
s.entry.Printf(s2, i...)
}
func (s *std) Debugf(s2 string, i ...interface{}) {
s.entry.Debugf(s2, i...)
}
func (s *std) Panicf(s2 string, i ...interface{}) {
s.entry.Panicf(s2, i...)
}
func (s *std) Panic(args ...interface{}) {
s.entry.Panic(args...)
}
func (s *std) Infoln(args ...interface{}) {
s.entry.Infoln(args...)
}
func (s *std) Infof(msg string, args ...interface{}) {
s.entry.Infof(msg, args...)
}
func (s *std) Warning(msg string) {
s.entry.Warning(msg)
}
func (s *std) Prefix(_ string) logger.Logger {
return s
}
func (s *std) Info(msg string) {
s.entry.Info(msg)
}
func (s *std) Errorln(args ...interface{}) {
s.entry.Infoln(args...)
}
func (s *std) Errorf(msg string, args ...interface{}) {
s.entry.Infof(msg, args...)
}
func (s *std) Error(msg string) {
s.entry.Info(msg)
}
func (s *std) WithFields(fields map[string]interface{}) AtpLogger {
newAtpLogger := &std{
s.entry.WithFields(fields),
}
return newAtpLogger
}
var atpLogger *std
var logLevels = map[string]log.Level{
"DEBUG": 5,
"INFO": 4,
"WARNING": 3,
"ERROR": 2,
}
func GetLogger(settings *app.Settings) AtpLogger {
if atpLogger != nil {
return atpLogger
}
logLevel := logLevels["INFO"]
outFile := ioutil.Discard
if settings != nil {
logLevel = logLevels[settings.LogLevel]
outFile = os.Stdout
}
logger := &log.Logger{
Out: outFile,
Hooks: nil,
Formatter: &NewFormatter{},
ReportCaller: false,
Level: logLevel,
ExitFunc: nil,
}
entry := logger.WithFields(log.Fields{
urbnlog.SERVICE: "atprouter",
urbnlog.PROCESS: os.Args[0],
urbnlog.PID: os.Getpid(),
})
atpLogger = &std{
entry: entry,
}
return atpLogger
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment