diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
commit | 8b573c94895dc0ac0e1d9d59cf3e8745e8b539ca (patch) | |
tree | 544930fb309b30317ae9797a9683768705d664c4 /workhorse/logging.go | |
parent | 4b1de649d0168371549608993deac953eb692019 (diff) |
Add latest changes from gitlab-org/gitlab@13-7-stable-eev13.7.0-rc42
Diffstat (limited to 'workhorse/logging.go')
-rw-r--r-- | workhorse/logging.go | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/workhorse/logging.go b/workhorse/logging.go new file mode 100644 index 00000000000..69718e6e834 --- /dev/null +++ b/workhorse/logging.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "io" + "io/ioutil" + goLog "log" + "os" + + log "github.com/sirupsen/logrus" + logkit "gitlab.com/gitlab-org/labkit/log" +) + +const ( + jsonLogFormat = "json" + textLogFormat = "text" + structuredFormat = "structured" + noneLogType = "none" +) + +func startLogging(file string, format string) (io.Closer, error) { + // Golog always goes to stderr + goLog.SetOutput(os.Stderr) + + if file == "" { + file = "stderr" + } + + switch format { + case noneLogType: + return logkit.Initialize(logkit.WithWriter(ioutil.Discard)) + case jsonLogFormat: + return logkit.Initialize( + logkit.WithOutputName(file), + logkit.WithFormatter("json"), + ) + case textLogFormat: + // In this mode, default (non-access) logs will always go to stderr + return logkit.Initialize( + logkit.WithOutputName("stderr"), + logkit.WithFormatter("text"), + ) + case structuredFormat: + return logkit.Initialize( + logkit.WithOutputName(file), + logkit.WithFormatter("color"), + ) + } + + return nil, fmt.Errorf("unknown logFormat: %v", format) +} + +// In text format, we use a separate logger for access logs +func getAccessLogger(file string, format string) (*log.Logger, io.Closer, error) { + if format != "text" { + return log.StandardLogger(), ioutil.NopCloser(nil), nil + } + + if file == "" { + file = "stderr" + } + + accessLogger := log.New() + accessLogger.SetLevel(log.InfoLevel) + closer, err := logkit.Initialize( + logkit.WithLogger(accessLogger), // Configure `accessLogger` + logkit.WithFormatter("combined"), // Use the combined formatter + logkit.WithOutputName(file), + ) + + return accessLogger, closer, err +} |