From 0f047c38168949b246223dc1b1154a6969753d2e Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Tue, 5 Nov 2019 02:09:07 +0200 Subject: [PATCH] Set logging level for Logrus and TDlib from config --- log.go | 29 +++++++++++++++++++++++++++++ telegabber.go | 2 ++ telegram/client.go | 14 +++++++------- 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 log.go diff --git a/log.go b/log.go new file mode 100644 index 0000000..b287815 --- /dev/null +++ b/log.go @@ -0,0 +1,29 @@ +package main + +import ( + log "github.com/sirupsen/logrus" +) + +var logConstants = map[string]log.Level{ + ":fatal": log.FatalLevel, + ":error": log.ErrorLevel, + ":warn": log.WarnLevel, + ":info": log.InfoLevel, + ":debug": log.DebugLevel, + ":verbose": log.TraceLevel, + ":all": log.TraceLevel, +} + +func stringToLogConstant(c string) log.Level { + level, ok := logConstants[c] + if !ok { + level = log.FatalLevel + } + + return level +} + +// SetLogrusLevel sets Logrus logging level from a string +func SetLogrusLevel(level string) { + log.SetLevel(stringToLogConstant(level)) +} diff --git a/telegabber.go b/telegabber.go index 6754038..8c098e1 100644 --- a/telegabber.go +++ b/telegabber.go @@ -19,6 +19,8 @@ func main() { log.Fatal(err) } + SetLogrusLevel(config.XMPP.Loglevel) + cm, err := xmpp.NewComponent(config.XMPP, config.Telegram) if err != nil { log.Fatal(err) diff --git a/telegram/client.go b/telegram/client.go index b0cee44..3d361b6 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -12,13 +12,13 @@ import ( ) var logConstants = map[string]int32{ - "fatal": 0, - "error": 1, - "warn": 2, - "info": 3, - "debug": 4, - "verbose": 5, - "all": 1023, + ":fatal": 0, + ":error": 1, + ":warn": 2, + ":info": 3, + ":debug": 4, + ":verbose": 5, + ":all": 1023, } func stringToLogConstant(c string) int32 {