2020-02-09 19:45:23 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
|
|
|
)
|
2020-02-09 19:45:23 +03:00
|
|
|
|
|
|
|
type Options struct {
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.Options
|
|
|
|
Formatter logrus.Formatter
|
|
|
|
Hooks logrus.LevelHooks
|
|
|
|
// Flag for whether to log caller info (off by default)
|
|
|
|
ReportCaller bool
|
|
|
|
// Exit Function to call when FatalLevel log
|
|
|
|
ExitFunc func(int)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
type formatterKey struct{}
|
2020-02-09 19:45:23 +03:00
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func WithTextTextFormatter(formatter *logrus.TextFormatter) logger.Option {
|
|
|
|
return logger.SetOption(formatterKey{}, formatter)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
func WithJSONFormatter(formatter *logrus.JSONFormatter) logger.Option {
|
|
|
|
return logger.SetOption(formatterKey{}, formatter)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
type hooksKey struct{}
|
2020-02-09 19:45:23 +03:00
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func WithLevelHooks(hooks logrus.LevelHooks) logger.Option {
|
|
|
|
return logger.SetOption(hooksKey{}, hooks)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
type reportCallerKey struct{}
|
|
|
|
|
2020-02-09 19:45:23 +03:00
|
|
|
// warning to use this option. because logrus doest not open CallerDepth option
|
|
|
|
// this will only print this package
|
2020-02-21 12:31:16 +03:00
|
|
|
func ReportCaller() logger.Option {
|
|
|
|
return logger.SetOption(reportCallerKey{}, true)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
type exitKey struct{}
|
2020-02-09 19:45:23 +03:00
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func WithExitFunc(exit func(int)) logger.Option {
|
|
|
|
return logger.SetOption(exitKey{}, exit)
|
|
|
|
}
|