-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
97 lines (87 loc) · 2.2 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright 2017 Hans Mündelein
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"github.com/spf13/viper"
log "github.com/Sirupsen/logrus"
valid "github.com/asaskevich/govalidator"
"github.com/fsnotify/fsnotify"
"errors"
)
type Logging struct {
LogLevel string `valid:"in(debug|info|warning|error)~Invalid logLevel"`
LogFormat string `valid:"in(text|json)~Invalid logFormat"`
}
func NewLogginConfig() (*Logging) {
lc := Logging{}
return &lc
}
func (lc *Logging) Init() error {
viper.OnConfigChange(func(e fsnotify.Event) {
log.Debug("LoggingConfig file changed:", e.Name)
err := lc.configure()
if err != nil {
log.Warning("changed config produce err ", err, " falling back to previous")
}
})
err := lc.configure()
if err != nil {
return err
}
return nil
}
func (lc *Logging) configure() error {
err := viper.UnmarshalKey("logging", &lc)
if err != nil {
return err
}
err = lc.Validate()
if err != nil {
return err
}
err = lc.configureLogrus()
if err != nil {
return err
}
return nil
}
func (lc *Logging) Validate() error {
ok, err := valid.ValidateStruct(lc)
if err != nil {
return err
}
if !ok {
return errors.New("error validating logging config")
}
return nil
}
func (lc *Logging) configureLogrus() error {
if logrusLevel, err := log.ParseLevel(lc.LogLevel); err == nil {
log.SetLevel(logrusLevel)
log.Debug("log level set to ", logrusLevel)
} else {
return errors.New("error setting log level")
}
switch lc.LogFormat {
case "text":
log.SetFormatter(&log.TextFormatter{})
log.Debug("logging format set to text")
case "json":
log.SetFormatter(&log.JSONFormatter{})
log.Debug("logging format set to json")
}
return nil
}