2019-04-24 21:52:08 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
2020-03-09 19:57:53 +00:00
|
|
|
"os"
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
logrus_stack "github.com/Gurpartap/logrus-stack"
|
2019-04-24 21:52:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-01-21 00:10:00 +00:00
|
|
|
// Logger return the standard logrus logger.
|
2019-04-24 21:52:08 +00:00
|
|
|
func Logger() *logrus.Logger {
|
|
|
|
return logrus.StandardLogger()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLevel set the level of the logger.
|
|
|
|
func SetLevel(level logrus.Level) {
|
|
|
|
logrus.SetLevel(level)
|
|
|
|
}
|
2020-03-09 19:57:53 +00:00
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// InitializeLogger initialize logger.
|
2020-03-09 19:57:53 +00:00
|
|
|
func InitializeLogger(filename string) error {
|
|
|
|
callerLevels := []logrus.Level{}
|
|
|
|
stackLevels := []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}
|
|
|
|
logrus.AddHook(logrus_stack.NewHook(callerLevels, stackLevels))
|
|
|
|
|
|
|
|
if filename != "" {
|
2020-11-12 23:14:45 +00:00
|
|
|
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
|
2020-03-09 19:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-09 19:57:53 +00:00
|
|
|
logrus.SetOutput(f)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-09 19:57:53 +00:00
|
|
|
return nil
|
|
|
|
}
|