Move system logging integration to separated file

parent f09a3dc5
...@@ -638,25 +638,19 @@ func (mr *RunCommand) Execute(context *cli.Context) { ...@@ -638,25 +638,19 @@ func (mr *RunCommand) Execute(context *cli.Context) {
}, },
} }
service, err := service_helpers.New(mr, svcConfig) svc, err := service_helpers.New(mr, svcConfig)
if err != nil { if err != nil {
logrus.Fatalln(err) logrus.Fatalln(err)
} }
if mr.Syslog { if mr.Syslog {
logrus.SetFormatter(new(logrus.TextFormatter)) cli_helpers.SetSystemLogger(svc)
logger, err := service.SystemLogger(nil)
if err == nil {
logrus.AddHook(&ServiceLogHook{logger, logrus.InfoLevel})
} else {
logrus.Errorln(err)
}
} }
logrus.AddHook(&mr.sentryLogHook) logrus.AddHook(&mr.sentryLogHook)
logrus.AddHook(&mr.prometheusLogHook) logrus.AddHook(&mr.prometheusLogHook)
err = service.Run() err = svc.Run()
if err != nil { if err != nil {
logrus.Fatalln(err) logrus.Fatalln(err)
} }
......
...@@ -20,43 +20,6 @@ const ( ...@@ -20,43 +20,6 @@ const (
defaultDescription = "GitLab Runner" defaultDescription = "GitLab Runner"
) )
type ServiceLogHook struct {
service.Logger
Level logrus.Level
}
func (s *ServiceLogHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
}
}
func (s *ServiceLogHook) Fire(entry *logrus.Entry) error {
if entry.Level > s.Level {
return nil
}
msg, err := entry.String()
if err != nil {
return err
}
switch entry.Level {
case logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel:
s.Error(msg)
case logrus.WarnLevel:
s.Warning(msg)
case logrus.InfoLevel:
s.Info(msg)
}
return nil
}
type NullService struct { type NullService struct {
} }
......
...@@ -2,41 +2,14 @@ package commands ...@@ -2,41 +2,14 @@ package commands
import ( import (
"fmt" "fmt"
"os"
"testing" "testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/urfave/cli" "github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
"gitlab.com/gitlab-org/gitlab-runner/helpers/service/mocks" "gitlab.com/gitlab-org/gitlab-runner/helpers"
) )
func TestServiceLogHook(t *testing.T) {
formatter := new(logrus.TextFormatter)
formatter.DisableColors = true
formatter.DisableTimestamp = true
logger := &logrus.Logger{
Formatter: formatter,
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
Out: os.Stderr,
}
mockServiceLogger := new(mocks.Logger)
mockServiceLogger.On("Info", "level=info msg=test\n").Return(nil)
logHook := &ServiceLogHook{mockServiceLogger, logrus.InfoLevel}
logger.Hooks.Add(logHook)
logger.Info("test")
mockServiceLogger.AssertExpectations(t)
}
func newTestGetServiceArgumentsCommand(t *testing.T, expectedArgs []string) func(*cli.Context) { func newTestGetServiceArgumentsCommand(t *testing.T, expectedArgs []string) func(*cli.Context) {
return func(c *cli.Context) { return func(c *cli.Context) {
arguments := getServiceArguments(c) arguments := getServiceArguments(c)
......
package cli_helpers
import (
"github.com/ayufan/golang-kardianos-service"
"github.com/sirupsen/logrus"
)
type ServiceLogHook struct {
service.Logger
Level logrus.Level
}
func (s *ServiceLogHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
}
}
func (s *ServiceLogHook) Fire(entry *logrus.Entry) error {
if entry.Level > s.Level {
return nil
}
msg, err := entry.String()
if err != nil {
return err
}
switch entry.Level {
case logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel:
s.Error(msg)
case logrus.WarnLevel:
s.Warning(msg)
case logrus.InfoLevel:
s.Info(msg)
}
return nil
}
func SetSystemLogger(svc service.Service) {
logrus.SetFormatter(new(logrus.TextFormatter))
logger, err := svc.SystemLogger(nil)
if err == nil {
logrus.AddHook(&ServiceLogHook{logger, logrus.InfoLevel})
} else {
logrus.Errorln(err)
}
}
package cli_helpers
import (
"os"
"testing"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-runner/helpers/service/mocks"
)
func TestServiceLogHook(t *testing.T) {
formatter := new(logrus.TextFormatter)
formatter.DisableColors = true
formatter.DisableTimestamp = true
logger := &logrus.Logger{
Formatter: formatter,
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
Out: os.Stderr,
}
mockServiceLogger := new(mocks.Logger)
mockServiceLogger.On("Info", "level=info msg=test\n").Return(nil)
logHook := &ServiceLogHook{mockServiceLogger, logrus.InfoLevel}
logger.Hooks.Add(logHook)
logger.Info("test")
mockServiceLogger.AssertExpectations(t)
}
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