This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
microservices-collection/lib/logging.go

54 lines
1023 B
Go
Raw Normal View History

2019-02-25 19:36:59 +01:00
package lib
import (
"flag"
"os"
"github.com/bdlm/log"
stdLogger "github.com/bdlm/std/logger"
)
var (
LogLevel = 40
LogTimestamp = false
)
type Hook struct{}
func (hook *Hook) Fire(entry *log.Entry) error {
switch entry.Level {
case log.PanicLevel:
entry.Logger.Out = os.Stderr
case log.FatalLevel:
entry.Logger.Out = os.Stderr
case log.ErrorLevel:
entry.Logger.Out = os.Stderr
case log.WarnLevel:
entry.Logger.Out = os.Stdout
case log.InfoLevel:
entry.Logger.Out = os.Stdout
case log.DebugLevel:
entry.Logger.Out = os.Stdout
default:
}
return nil
}
func (hook *Hook) Levels() []stdLogger.Level {
return log.AllLevels
}
func init() {
log.AddHook(&Hook{})
flag.BoolVar(&LogTimestamp, "timestamps", LogTimestamp, "Enables timestamps for log output")
flag.IntVar(&LogLevel, "loglevel", LogLevel, "Show log message starting at level")
}
func LogUpdateConfig() {
log.SetLevel(stdLogger.Level(LogLevel))
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: !LogTimestamp,
})
}