Refactor logrus configuration

parent 553ba676
......@@ -6,9 +6,9 @@ import (
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers/cli"
"gitlab.com/gitlab-org/gitlab-runner/helpers/formatter"
_ "gitlab.com/gitlab-org/gitlab-runner/commands/helpers"
)
......@@ -24,9 +24,6 @@ func main() {
}
}()
formatter.SetRunnerFormatter()
cli_helpers.AddSecretsCleanupLogHook()
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Usage = "a GitLab Runner Helper"
......@@ -38,12 +35,14 @@ func main() {
Email: "support@gitlab.com",
},
}
cli_helpers.ConfigureLogging(app)
app.Commands = common.GetCommands()
app.CommandNotFound = func(context *cli.Context, command string) {
logrus.Fatalln("Command", command, "not found")
}
cli_helpers.ConfigureLogging(app)
cli_helpers.AddSecretsCleanupLogHook()
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
......
......@@ -259,10 +259,10 @@ func (mr *RunCommand) loadConfig() error {
}
// Set log level
if !cli_helpers.CustomLogLevelSet && mr.config.LogLevel != nil {
if !cli_helpers.IsCustomLevelUsed() && mr.config.LogLevel != nil {
level, err := logrus.ParseLevel(*mr.config.LogLevel)
if err != nil {
logrus.Fatalf(err.Error())
logrus.WithError(err).Fatal("Failed to parse error level from configuration file")
}
logrus.SetLevel(level)
}
......
......@@ -12,12 +12,14 @@ import (
)
func watchForGoroutinesDump() {
// On USR1 dump stacks of all go routines
dumpStacks := make(chan os.Signal, 1)
// On USR1 dump stacks of all go routines
signal.Notify(dumpStacks, syscall.SIGUSR1)
for _ = range dumpStacks {
for range dumpStacks {
buf := make([]byte, 1<<20)
runtime.Stack(buf, true)
logrus.Printf("=== received SIGUSR1 ===\n*** goroutine dump...\n%s\n*** end\n", buf)
len := runtime.Stack(buf, true)
logrus.Printf("=== received SIGUSR1 ===\n*** goroutine dump...\n%s\n*** end\n", buf[0:len])
}
}
......@@ -5,6 +5,8 @@ import (
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/helpers/formatter"
)
const (
......@@ -13,12 +15,10 @@ const (
)
var (
DefaultLogLevel = logrus.InfoLevel
CustomLogLevelSet = false
)
defaultLogLevel = logrus.InfoLevel
customLevelUsed = false
func ConfigureLogging(app *cli.App) {
newFlags := []cli.Flag{
logFlags = []cli.Flag{
cli.StringFlag{
Name: "log-format",
Usage: "Chose log format (options: text, json)",
......@@ -36,59 +36,80 @@ func ConfigureLogging(app *cli.App) {
EnvVar: "LOG_LEVEL",
},
}
app.Flags = append(app.Flags, newFlags...)
formats = map[string]logrus.Formatter{
LogFormatText: new(logrus.TextFormatter),
LogFormatJSON: new(logrus.JSONFormatter),
}
)
func IsCustomLevelUsed() bool {
return customLevelUsed
}
func ConfigureLogging(app *cli.App) {
app.Flags = append(app.Flags, logFlags...)
appBefore := app.Before
// logs
app.Before = func(c *cli.Context) error {
app.Before = func(cliCtx *cli.Context) error {
logrus.SetOutput(os.Stderr)
logrus.SetLevel(DefaultLogLevel)
setFormat(c)
if c.IsSet("log-level") || c.IsSet("l") {
level, err := logrus.ParseLevel(c.String("log-level"))
if err != nil {
logrus.Fatalf(err.Error())
}
setCustomLevel(level)
} else if c.Bool("debug") {
setCustomLevel(logrus.DebugLevel)
go watchForGoroutinesDump()
}
setupFormatter(cliCtx)
setupLevel(cliCtx)
if appBefore != nil {
return appBefore(c)
return appBefore(cliCtx)
}
return nil
}
}
func setFormat(c *cli.Context) {
if !c.IsSet("log-format") {
func setupFormatter(cliCtx *cli.Context) {
if !cliCtx.IsSet("log-format") {
logrus.SetFormatter(new(formatter.RunnerTextFormatter))
return
}
formats := map[string]logrus.Formatter{
LogFormatText: new(logrus.TextFormatter),
LogFormatJSON: new(logrus.JSONFormatter),
}
format := c.String("log-format")
format := cliCtx.String("log-format")
formatter, ok := formats[format]
if !ok {
formatNames := make([]string, 0)
for name := range formats {
formatNames = append(formatNames, name)
}
logrus.WithField("format", format).Fatalf("Unknown log format. Expected one of: %v", formatNames)
logrus.WithField("format", format).Fatalf("Unknown log format. Expected one of: %v", formatNames())
}
logrus.SetFormatter(formatter)
}
func setCustomLevel(level logrus.Level) {
logrus.SetLevel(level)
CustomLogLevelSet = true
func formatNames() []string {
formatNames := make([]string, 0)
for name := range formats {
formatNames = append(formatNames, name)
}
return formatNames
}
func setupLevel(cliCtx *cli.Context) {
if cliCtx.IsSet("log-level") || cliCtx.IsSet("l") {
level, err := logrus.ParseLevel(cliCtx.String("log-level"))
if err != nil {
logrus.WithError(err).Fatal("Failed to parse log level")
}
logrus.SetLevel(level)
customLevelUsed = true
return
}
if cliCtx.Bool("debug") {
go watchForGoroutinesDump()
logrus.SetLevel(logrus.DebugLevel)
customLevelUsed = true
return
}
logrus.SetLevel(defaultLogLevel)
}
package cli_helpers
import (
"os"
"runtime"
"gitlab.com/gitlab-org/gitlab-runner/common"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/common"
)
func LogRuntimePlatform(app *cli.App) {
appBefore := app.Before
app.Before = func(c *cli.Context) error {
logrus.WithFields(logrus.Fields{
fields := logrus.Fields{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"version": common.VERSION,
"revision": common.REVISION,
}).Debugln("Runtime platform")
"pid": os.Getpid(),
}
logrus.WithFields(fields).Info("Runtime platform")
if appBefore != nil {
return appBefore(c)
......
......@@ -6,9 +6,9 @@ import (
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers/cli"
"gitlab.com/gitlab-org/gitlab-runner/helpers/formatter"
_ "gitlab.com/gitlab-org/gitlab-runner/cache/gcs"
_ "gitlab.com/gitlab-org/gitlab-runner/cache/s3"
......@@ -35,8 +35,6 @@ func main() {
}
}()
formatter.SetRunnerFormatter()
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Usage = "a GitLab Runner"
......@@ -48,16 +46,17 @@ func main() {
Email: "support@gitlab.com",
},
}
cli_helpers.LogRuntimePlatform(app)
cli_helpers.ConfigureLogging(app)
cli_helpers.SetupCPUProfile(app)
cli_helpers.FixHOME(app)
app.Commands = common.GetCommands()
app.CommandNotFound = func(context *cli.Context, command string) {
logrus.Fatalln("Command", command, "not found.")
}
cli_helpers.LogRuntimePlatform(app)
cli_helpers.ConfigureLogging(app)
cli_helpers.SetupCPUProfile(app)
cli_helpers.FixHOME(app)
cli_helpers.WarnOnBool(os.Args)
if err := app.Run(os.Args); err != nil {
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