summaryrefslogtreecommitdiff
path: root/workhorse/internal/log/logging.go
blob: c65ec07743a61485835509ee0b7a7bc2f4a75dbb (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package log

import (
	"net/http"

	"github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/labkit/log"
	"gitlab.com/gitlab-org/labkit/mask"
	"golang.org/x/net/context"

	"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
)

type Fields = log.Fields

type Builder struct {
	entry  *logrus.Entry
	fields log.Fields
	req    *http.Request
	err    error
}

func NewBuilder() *Builder {
	return &Builder{entry: log.WithFields(nil)}
}

func WithRequest(r *http.Request) *Builder {
	return NewBuilder().WithRequest(r)
}

func (b *Builder) WithRequest(r *http.Request) *Builder {
	if r == nil {
		return b
	}

	b.req = r
	b.WithFields(log.ContextFields(r.Context())).WithFields(
		Fields{
			"method": r.Method,
			"uri":    mask.URL(r.RequestURI),
		},
	)
	return b
}

func WithFields(fields Fields) *Builder {
	return NewBuilder().WithFields(fields)
}

func (b *Builder) WithFields(fields Fields) *Builder {
	b.fields = fields
	b.entry = b.entry.WithFields(fields)
	return b
}

func WithContextFields(ctx context.Context, fields Fields) *Builder {
	return WithFields(log.ContextFields(ctx)).WithFields(fields)
}

func WithError(err error) *Builder {
	return NewBuilder().WithError(err)
}

func (b *Builder) WithError(err error) *Builder {
	b.err = err
	b.entry = b.entry.WithError(err)
	return b
}

func Info(args ...interface{}) {
	NewBuilder().Info(args...)
}

func (b *Builder) Info(args ...interface{}) {
	b.entry.Info(args...)
}

func Error(args ...interface{}) {
	NewBuilder().Error(args...)
}

func (b *Builder) Error(args ...interface{}) {
	b.entry.Error(args...)

	if b.req != nil && b.err != nil {
		helper.CaptureRavenError(b.req, b.err, b.fields)
	}
}