From 5fa4371b334f0457a002da1cb6b7afdb472725c2 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Mon, 25 Feb 2019 19:36:59 +0100 Subject: [PATCH] add lib with local_logging --- lib/logging.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/logging.go diff --git a/lib/logging.go b/lib/logging.go new file mode 100644 index 0000000..fb87886 --- /dev/null +++ b/lib/logging.go @@ -0,0 +1,53 @@ +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, + }) +}