Move logging helpers and configuration to dedicated package

parent a84a4f8a
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/common" "gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers/cli" "gitlab.com/gitlab-org/gitlab-runner/log"
_ "gitlab.com/gitlab-org/gitlab-runner/commands/helpers" _ "gitlab.com/gitlab-org/gitlab-runner/commands/helpers"
) )
...@@ -40,8 +40,8 @@ func main() { ...@@ -40,8 +40,8 @@ func main() {
logrus.Fatalln("Command", command, "not found") logrus.Fatalln("Command", command, "not found")
} }
cli_helpers.ConfigureLogging(app) log.AddSecretsCleanupLogHook()
cli_helpers.AddSecretsCleanupLogHook() log.ConfigureLogging(app)
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
......
...@@ -21,10 +21,10 @@ import ( ...@@ -21,10 +21,10 @@ import (
"gitlab.com/gitlab-org/gitlab-runner/common" "gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers" "gitlab.com/gitlab-org/gitlab-runner/helpers"
"gitlab.com/gitlab-org/gitlab-runner/helpers/certificate" "gitlab.com/gitlab-org/gitlab-runner/helpers/certificate"
"gitlab.com/gitlab-org/gitlab-runner/helpers/cli"
prometheus_helper "gitlab.com/gitlab-org/gitlab-runner/helpers/prometheus" prometheus_helper "gitlab.com/gitlab-org/gitlab-runner/helpers/prometheus"
"gitlab.com/gitlab-org/gitlab-runner/helpers/sentry" "gitlab.com/gitlab-org/gitlab-runner/helpers/sentry"
"gitlab.com/gitlab-org/gitlab-runner/helpers/service" "gitlab.com/gitlab-org/gitlab-runner/helpers/service"
"gitlab.com/gitlab-org/gitlab-runner/log"
"gitlab.com/gitlab-org/gitlab-runner/network" "gitlab.com/gitlab-org/gitlab-runner/network"
"gitlab.com/gitlab-org/gitlab-runner/session" "gitlab.com/gitlab-org/gitlab-runner/session"
) )
...@@ -259,7 +259,7 @@ func (mr *RunCommand) loadConfig() error { ...@@ -259,7 +259,7 @@ func (mr *RunCommand) loadConfig() error {
} }
// Set log level // Set log level
if !cli_helpers.IsCustomLevelUsed() && mr.config.LogLevel != nil { if !log.IsCustomLevelUsed() && mr.config.LogLevel != nil {
level, err := logrus.ParseLevel(*mr.config.LogLevel) level, err := logrus.ParseLevel(*mr.config.LogLevel)
if err != nil { if err != nil {
logrus.WithError(err).Fatal("Failed to parse error level from configuration file") logrus.WithError(err).Fatal("Failed to parse error level from configuration file")
...@@ -644,7 +644,7 @@ func (mr *RunCommand) Execute(context *cli.Context) { ...@@ -644,7 +644,7 @@ func (mr *RunCommand) Execute(context *cli.Context) {
} }
if mr.Syslog { if mr.Syslog {
cli_helpers.SetSystemLogger(svc) log.SetSystemLogger(svc)
} }
logrus.AddHook(&mr.sentryLogHook) logrus.AddHook(&mr.sentryLogHook)
......
package cli_helpers package log
import ( import (
"os" "os"
...@@ -10,8 +10,8 @@ import ( ...@@ -10,8 +10,8 @@ import (
) )
const ( const (
LogFormatText = "text" FormatText = "text"
LogFormatJSON = "json" FormatJSON = "json"
) )
var ( var (
...@@ -19,17 +19,17 @@ var ( ...@@ -19,17 +19,17 @@ var (
customLevelUsed = false customLevelUsed = false
logFlags = []cli.Flag{ logFlags = []cli.Flag{
cli.StringFlag{
Name: "log-format",
Usage: "Chose log format (options: text, json)",
EnvVar: "LOG_FORMAT",
Value: LogFormatText,
},
cli.BoolFlag{ cli.BoolFlag{
Name: "debug", Name: "debug",
Usage: "debug mode", Usage: "debug mode",
EnvVar: "DEBUG", EnvVar: "DEBUG",
}, },
cli.StringFlag{
Name: "log-format",
Usage: "Chose log format (options: text, json)",
EnvVar: "LOG_FORMAT",
Value: FormatText,
},
cli.StringFlag{ cli.StringFlag{
Name: "log-level, l", Name: "log-level, l",
Usage: "Log level (options: debug, info, warn, error, fatal, panic)", Usage: "Log level (options: debug, info, warn, error, fatal, panic)",
...@@ -38,8 +38,8 @@ var ( ...@@ -38,8 +38,8 @@ var (
} }
formats = map[string]logrus.Formatter{ formats = map[string]logrus.Formatter{
LogFormatText: new(logrus.TextFormatter), FormatText: new(logrus.TextFormatter),
LogFormatJSON: new(logrus.JSONFormatter), FormatJSON: new(logrus.JSONFormatter),
} }
) )
......
// +build darwin dragonfly freebsd linux netbsd openbsd // +build darwin dragonfly freebsd linux netbsd openbsd
package cli_helpers package log
import ( import (
"os" "os"
......
package cli_helpers package log
func watchForGoroutinesDump() { func watchForGoroutinesDump() {
} }
package cli_helpers package log
import ( import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
...@@ -18,5 +18,5 @@ func (s *SecretsCleanupHook) Fire(entry *logrus.Entry) error { ...@@ -18,5 +18,5 @@ func (s *SecretsCleanupHook) Fire(entry *logrus.Entry) error {
} }
func AddSecretsCleanupLogHook() { func AddSecretsCleanupLogHook() {
logrus.AddHook(&SecretsCleanupHook{}) logrus.AddHook(new(SecretsCleanupHook))
} }
package cli_helpers_test package log
import ( import (
"bytes" "bytes"
...@@ -6,8 +6,6 @@ import ( ...@@ -6,8 +6,6 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-runner/helpers/cli"
) )
func TestSecretsCleanupHook(t *testing.T) { func TestSecretsCleanupHook(t *testing.T) {
...@@ -34,7 +32,7 @@ func TestSecretsCleanupHook(t *testing.T) { ...@@ -34,7 +32,7 @@ func TestSecretsCleanupHook(t *testing.T) {
logger := logrus.New() logger := logrus.New()
logger.Out = buffer logger.Out = buffer
logger.AddHook(&cli_helpers.SecretsCleanupHook{}) logger.AddHook(new(SecretsCleanupHook))
logger.Errorln(test.message) logger.Errorln(test.message)
......
package cli_helpers package log
import ( import (
"github.com/ayufan/golang-kardianos-service" "github.com/ayufan/golang-kardianos-service"
......
package cli_helpers package log
import ( import (
"os" "os"
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"gitlab.com/gitlab-org/gitlab-runner/common" "gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers/cli" "gitlab.com/gitlab-org/gitlab-runner/helpers/cli"
"gitlab.com/gitlab-org/gitlab-runner/log"
_ "gitlab.com/gitlab-org/gitlab-runner/cache/gcs" _ "gitlab.com/gitlab-org/gitlab-runner/cache/gcs"
_ "gitlab.com/gitlab-org/gitlab-runner/cache/s3" _ "gitlab.com/gitlab-org/gitlab-runner/cache/s3"
...@@ -52,11 +53,12 @@ func main() { ...@@ -52,11 +53,12 @@ func main() {
} }
cli_helpers.LogRuntimePlatform(app) cli_helpers.LogRuntimePlatform(app)
cli_helpers.ConfigureLogging(app)
cli_helpers.SetupCPUProfile(app) cli_helpers.SetupCPUProfile(app)
cli_helpers.FixHOME(app) cli_helpers.FixHOME(app)
cli_helpers.WarnOnBool(os.Args) cli_helpers.WarnOnBool(os.Args)
log.ConfigureLogging(app)
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment