debian-mirror-gitlab/workhorse/logging.go

72 lines
1.5 KiB
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package main
import (
"fmt"
"io"
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:
2022-07-23 23:45:48 +05:30
return logkit.Initialize(logkit.WithWriter(io.Discard))
2021-02-22 17:27:13 +05:30
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" {
2022-07-23 23:45:48 +05:30
return log.StandardLogger(), io.NopCloser(nil), nil
2021-02-22 17:27:13 +05:30
}
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
}