123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package log
- import (
- "github.com/sirupsen/logrus"
- prefixed "github.com/x-cray/logrus-prefixed-formatter"
- )
- type LogrusLogger struct {
- log logrus.Ext1FieldLogger
- prefix string
- fields Fields
- }
- // Level type
- type Level uint32
- // These are the different logging levels. You can set the logging level to log
- // on your instance of logger, obtained with `logrus.New()`.
- const (
- // PanicLevel level, highest level of severity. Logs and then calls panic with the
- // message passed to Debug, Info, ...
- PanicLevel Level = iota
- // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
- // logging level is set to Panic.
- FatalLevel
- // ErrorLevel level. Logs. Used for errors that should definitely be noted.
- // Commonly used for hooks to send errors to an error tracking service.
- ErrorLevel
- // WarnLevel level. Non-critical entries that deserve eyes.
- WarnLevel
- // InfoLevel level. General operational entries about what's going on inside the
- // application.
- InfoLevel
- // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
- DebugLevel
- // TraceLevel level. Designates finer-grained informational events than the Debug.
- TraceLevel
- )
- func NewLogrusLogger(logrus logrus.Ext1FieldLogger, prefix string, fields Fields) *LogrusLogger {
- return &LogrusLogger{
- log: logrus,
- prefix: prefix,
- fields: fields,
- }
- }
- func NewDefaultLogrusLogger() *LogrusLogger {
- logger := logrus.New()
- logger.Formatter = &prefixed.TextFormatter{
- FullTimestamp: true,
- TimestampFormat: "2006-01-02 15:04:05.000",
- }
- return NewLogrusLogger(logger, "main", nil)
- }
- func (l *LogrusLogger) Print(args ...interface{}) {
- l.prepareEntry().Print(args...)
- }
- func (l *LogrusLogger) Printf(format string, args ...interface{}) {
- l.prepareEntry().Printf(format, args...)
- }
- func (l *LogrusLogger) Trace(args ...interface{}) {
- l.prepareEntry().Trace(args...)
- }
- func (l *LogrusLogger) Tracef(format string, args ...interface{}) {
- l.prepareEntry().Tracef(format, args...)
- }
- func (l *LogrusLogger) Debug(args ...interface{}) {
- l.prepareEntry().Debug(args...)
- }
- func (l *LogrusLogger) Debugf(format string, args ...interface{}) {
- l.prepareEntry().Debugf(format, args...)
- }
- func (l *LogrusLogger) Info(args ...interface{}) {
- l.prepareEntry().Info(args...)
- }
- func (l *LogrusLogger) Infof(format string, args ...interface{}) {
- l.prepareEntry().Infof(format, args...)
- }
- func (l *LogrusLogger) Warn(args ...interface{}) {
- l.prepareEntry().Warn(args...)
- }
- func (l *LogrusLogger) Warnf(format string, args ...interface{}) {
- l.prepareEntry().Warnf(format, args...)
- }
- func (l *LogrusLogger) Error(args ...interface{}) {
- l.prepareEntry().Error(args...)
- }
- func (l *LogrusLogger) Errorf(format string, args ...interface{}) {
- l.prepareEntry().Errorf(format, args...)
- }
- func (l *LogrusLogger) Fatal(args ...interface{}) {
- l.prepareEntry().Fatal(args...)
- }
- func (l *LogrusLogger) Fatalf(format string, args ...interface{}) {
- l.prepareEntry().Fatalf(format, args...)
- }
- func (l *LogrusLogger) Panic(args ...interface{}) {
- l.prepareEntry().Panic(args...)
- }
- func (l *LogrusLogger) Panicf(format string, args ...interface{}) {
- l.prepareEntry().Panicf(format, args...)
- }
- func (l *LogrusLogger) WithPrefix(prefix string) Logger {
- return NewLogrusLogger(l.log, prefix, l.Fields())
- }
- func (l *LogrusLogger) Prefix() string {
- return l.prefix
- }
- func (l *LogrusLogger) WithFields(fields Fields) Logger {
- return NewLogrusLogger(l.log, l.Prefix(), l.Fields().WithFields(fields))
- }
- func (l *LogrusLogger) Fields() Fields {
- return l.fields
- }
- func (l *LogrusLogger) prepareEntry() *logrus.Entry {
- return l.log.
- WithFields(logrus.Fields(l.Fields())).
- WithField("prefix", l.Prefix())
- }
- func (l *LogrusLogger) SetLevel(level Level) {
- if ll, ok := l.log.(*logrus.Logger); ok {
- ll.SetLevel(logrus.Level(level))
- }
- }
|