add lib with local_logging

This commit is contained in:
Martin/Geno 2019-02-25 19:36:59 +01:00
parent e47e3faee9
commit 5fa4371b33
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
1 changed files with 53 additions and 0 deletions

53
lib/logging.go Normal file
View File

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