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, + }) +}