This repository has been archived by the owner on Jan 21, 2025. It is now read-only.
generated from logur/integration-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
71 lines (55 loc) · 1.76 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Package watermill provides Logur integration for Watermill.
package watermill
import (
"github.com/ThreeDotsLabs/watermill"
"logur.dev/logur"
)
// ErrorHandler handles an error passed to the logger.
type ErrorHandler interface {
// Handle handles an error passed to the logger.
Handle(err error, fields map[string]interface{})
}
// Logger is a github.com/ThreeDotsLabs/watermill.LoggerAdapter compatible logger.
type Logger struct {
logger logur.Logger
errorHandler ErrorHandler
}
// New returns a github.com/ThreeDotsLabs/watermill.LoggerAdapter compatible logger.
func New(logger logur.Logger) *Logger {
return &Logger{logger: logger}
}
// NewWithErrorHandler returns a github.com/ThreeDotsLabs/watermill.LoggerAdapter compatible logger.
// Compared to the logger returned by New, this logger sends errors to the error handler.
func NewWithErrorHandler(logger logur.Logger, errorHandler ErrorHandler) *Logger {
return &Logger{
logger: logger,
errorHandler: errorHandler,
}
}
func (l *Logger) Error(msg string, err error, fields watermill.LogFields) {
if l.errorHandler == nil {
if fields == nil {
fields = watermill.LogFields{}
}
fields["err"] = err
l.logger.Error(msg, fields)
return
}
err = errorWithMessage(err, msg)
l.errorHandler.Handle(err, fields)
}
func (l *Logger) Info(msg string, fields watermill.LogFields) {
l.logger.Info(msg, fields)
}
func (l *Logger) Debug(msg string, fields watermill.LogFields) {
l.logger.Debug(msg, fields)
}
func (l *Logger) Trace(msg string, fields watermill.LogFields) {
l.logger.Trace(msg, fields)
}
func (l *Logger) With(fields watermill.LogFields) watermill.LoggerAdapter {
if len(fields) == 0 {
return l
}
return NewWithErrorHandler(logur.WithFields(l.logger, fields), l.errorHandler)
}