Logrus creates a sink that writes to out using a logrus format. Log entries are emitted when their level is at or below verbosity. (Only the most important entries are emitted when verbosity is zero.) Error entries get a logrus.ErrorLevel, Info entries with verbosity less than debug get a logrus.Inf
(out io.Writer, version string, debug, verbosity int)
| 23 | // logrus.InfoLevel, and Info entries with verbosity of debug or more get a |
| 24 | // logrus.DebugLevel. |
| 25 | func Logrus(out io.Writer, version string, debug, verbosity int) logr.LogSink { |
| 26 | root := logrus.New() |
| 27 | |
| 28 | root.SetLevel(logrus.TraceLevel) |
| 29 | root.SetOutput(out) |
| 30 | |
| 31 | root.SetFormatter(&logrus.TextFormatter{ |
| 32 | FullTimestamp: true, |
| 33 | }) |
| 34 | |
| 35 | _, module, _, _ := runtime.Caller(0) |
| 36 | module = strings.TrimSuffix(module, "internal/logging/logrus.go") |
| 37 | |
| 38 | return &sink{ |
| 39 | verbosity: verbosity, |
| 40 | |
| 41 | fnError: func(err error, message string, kv ...any) { |
| 42 | entry := root.WithField("version", version) |
| 43 | entry = logrusFields(entry, kv...) |
| 44 | |
| 45 | if v, ok := entry.Data[logrus.ErrorKey]; ok { |
| 46 | entry.Data["fields."+logrus.ErrorKey] = v |
| 47 | } |
| 48 | entry = entry.WithError(err) |
| 49 | |
| 50 | var t interface{ StackTrace() errors.StackTrace } |
| 51 | if errors.As(err, &t) { |
| 52 | if st := t.StackTrace(); len(st) > 0 { |
| 53 | frame, _ := runtime.CallersFrames([]uintptr{uintptr(st[0])}).Next() |
| 54 | logrusFrame(entry, frame, module) |
| 55 | } |
| 56 | } |
| 57 | entry.Log(logrus.ErrorLevel, message) |
| 58 | }, |
| 59 | |
| 60 | fnInfo: func(level int, message string, kv ...any) { |
| 61 | entry := root.WithField("version", version) |
| 62 | entry = logrusFields(entry, kv...) |
| 63 | |
| 64 | if level >= debug { |
| 65 | entry.Log(logrus.DebugLevel, message) |
| 66 | } else { |
| 67 | entry.Log(logrus.InfoLevel, message) |
| 68 | } |
| 69 | }, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // logrusFields structures and adds the key/value interface to the logrus.Entry; |
| 74 | // for instance, if a key is not a string, this formats the key as a string. |