[Awesome Go] Use logrusHelper package to load configuration for logrus log

logrusHelper

logrusHelper is a Helper arround Logrus to wrap with spf13/Viper to load configuration with fangs!

Example

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
// main.go

import(

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/heirko/go-contrib/logrusHelper"
_ "github.com/heralight/logrus_mate/hooks/file"
)


func initLogger() {

// ########## Init Viper
var viper = viper.New()

viper.SetConfigName("mate") // name of config file (without extension), here we use some logrus_mate sample
viper.AddConfigPath("/etc/appname/") // path to look for the config file in
viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
// ########### End Init Viper

// Read configuration
var c = logrusHelper.UnmarshalConfiguration(viper) // Unmarshal configuration from Viper
logrusHelper.SetConfig(logrus.StandardLogger(), c) // for e.g. apply it to logrus default instance

// ### End Read Configuration

// ### Use logrus as normal
logrus.WithFields(logrus.Fields{
"animal": "walrus",
}).Error("A walrus appears")
}

See mate.conf.example - https://github.com/heralight/logrus_mate/blob/v1.0.1/example/mate.conf.example to learn more.

References

[1] go-contrib/logrusHelper at master · heirko/go-contrib · GitHub - https://github.com/heirko/go-contrib/tree/master/logrusHelper

[2] sirupsen/logrus: Structured, pluggable logging for Go. - https://github.com/sirupsen/logrus

[3] mate.conf.example - https://github.com/heralight/logrus_mate/blob/v1.0.1/example/mate.conf.example

[4] gogap/logrus_mate: tool for logrus, let it easy to use - https://github.com/gogap/logrus_mate

[5] GitHub - spf13/viper: Go configuration with fangs - https://github.com/spf13/viper