Welcome to mirror list, hosted at ThFree Co, Russian Federation.

logging.go « logging « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6643e169ef17ad5fa0bec80b6b19fefc5e80a50b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package logging

import (
	"net/http"

	"github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/labkit/log"

	"gitlab.com/gitlab-org/gitlab-pages/internal/request"
)

// ConfigureLogging will initialize the system logger.
func ConfigureLogging(format string, verbose bool) error {
	var levelOption log.LoggerOption

	if format == "" {
		format = "text"
	}

	if verbose {
		levelOption = log.WithLogLevel("trace")
	} else {
		levelOption = log.WithLogLevel("info")
	}

	_, err := log.Initialize(
		log.WithFormatter(format),
		levelOption,
	)
	return err
}

// getAccessLogger will return the default logger, except when
// the log format is text, in which case a combined HTTP access
// logger will be configured. This behaviour matches Workhorse
func getAccessLogger(format string) (*logrus.Logger, error) {
	if format != "text" && format != "" {
		return logrus.StandardLogger(), nil
	}

	accessLogger := log.New()
	_, err := log.Initialize(
		log.WithLogger(accessLogger),  // Configure `accessLogger`
		log.WithFormatter("combined"), // Use the combined formatter
	)
	if err != nil {
		return nil, err
	}

	return accessLogger, nil
}

// getExtraLogFields is used to inject additional fields into the
// HTTP access logger middleware.
func getExtraLogFields(r *http.Request) log.Fields {
	logFields := log.Fields{
		"pages_https": request.IsHTTPS(r),
		"pages_host":  request.GetHost(r),
	}

	if d := request.GetDomain(r); d != nil {
		if lp := d.GetLookupPath(r); lp != nil {
			logFields["pages_project_serving_type"] = lp.ServingType
			logFields["pages_project_prefix"] = lp.Prefix
			logFields["pages_project_id"] = lp.ProjectID
		}
	}

	return logFields
}

// BasicAccessLogger configures the GitLab pages basic HTTP access logger middleware
func BasicAccessLogger(handler http.Handler, format string, extraFields log.ExtraFieldsGeneratorFunc) (http.Handler, error) {
	accessLogger, err := getAccessLogger(format)
	if err != nil {
		return nil, err
	}

	if extraFields == nil {
		extraFields = func(r *http.Request) log.Fields {
			return log.Fields{
				"pages_https": request.IsHTTPS(r),
				"pages_host":  r.Host,
			}
		}
	}

	return log.AccessLogger(handler,
		log.WithExtraFields(extraFields),
		log.WithAccessLogger(accessLogger),
	), nil
}

// AccessLogger configures the GitLab pages HTTP access logger middleware with extra log fields
func AccessLogger(handler http.Handler, format string) (http.Handler, error) {
	return BasicAccessLogger(handler, format, getExtraLogFields)
}

// LogRequest will inject request host and path to the logged messages
func LogRequest(r *http.Request) *logrus.Entry {
	return log.WithFields(log.Fields{
		"host": r.Host,
		"path": r.URL.Path,
	})
}