2020-02-09 19:45:23 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-01-10 15:21:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-02-21 12:31:16 +03:00
|
|
|
)
|
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)
|
|
|
|
}
|
2020-03-03 20:26:06 +03:00
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
type loggerKey struct{}
|
2020-03-03 20:26:06 +03:00
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
func WithLogger(l Logger) logger.Option {
|
|
|
|
return logger.SetOption(loggerKey{}, l)
|
2020-03-03 20:26:06 +03:00
|
|
|
}
|