diff options
author | Jacob Vosmaer (GitLab) <jacob@gitlab.com> | 2018-03-19 11:58:59 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-03-19 11:58:59 +0000 |
commit | b8a5e52193b7d02de4802b589e098bbbfa0ec425 (patch) | |
tree | 30833503feb797f8610b21f2475c04e959b0ebac /go | |
parent | 355e70e08b9180456ef57fb79a2c3b5654f85479 (diff) | |
download | gitlab-shell-b8a5e52193b7d02de4802b589e098bbbfa0ec425.tar.gz |
Switch to structured logging
Diffstat (limited to 'go')
-rw-r--r-- | go/internal/config/config.go | 11 | ||||
-rw-r--r-- | go/internal/config/config_test.go | 33 | ||||
-rw-r--r-- | go/internal/logger/logger.go | 25 |
3 files changed, 46 insertions, 23 deletions
diff --git a/go/internal/config/config.go b/go/internal/config/config.go index c57b4de..7d521f5 100644 --- a/go/internal/config/config.go +++ b/go/internal/config/config.go @@ -5,7 +5,7 @@ import ( "os" "path" - "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v2" ) const ( @@ -14,8 +14,9 @@ const ( ) type Config struct { - RootDir string - LogFile string `yaml:"log_file"` + RootDir string + LogFile string `yaml:"log_file"` + LogFormat string `yaml:"log_format"` } func New() (*Config, error) { @@ -53,5 +54,9 @@ func parseConfig(configBytes []byte, cfg *Config) error { cfg.LogFile = path.Join(cfg.RootDir, cfg.LogFile) } + if cfg.LogFormat == "" { + cfg.LogFormat = "text" + } + return nil } diff --git a/go/internal/config/config_test.go b/go/internal/config/config_test.go index 0ca2dab..0a5c842 100644 --- a/go/internal/config/config_test.go +++ b/go/internal/config/config_test.go @@ -1,28 +1,37 @@ package config import ( + "fmt" "testing" ) func TestConfigLogFile(t *testing.T) { testRoot := "/foo/bar" testCases := []struct { - yaml string - path string + yaml string + path string + format string }{ - {path: "/foo/bar/gitlab-shell.log"}, - {yaml: "log_file: my-log.log", path: "/foo/bar/my-log.log"}, - {yaml: "log_file: /qux/my-log.log", path: "/qux/my-log.log"}, + {path: "/foo/bar/gitlab-shell.log", format: "text"}, + {yaml: "log_file: my-log.log", path: "/foo/bar/my-log.log", format: "text"}, + {yaml: "log_file: /qux/my-log.log", path: "/qux/my-log.log", format: "text"}, + {yaml: "log_format: json", path: "/foo/bar/gitlab-shell.log", format: "json"}, } for _, tc := range testCases { - cfg := Config{RootDir: testRoot} - if err := parseConfig([]byte(tc.yaml), &cfg); err != nil { - t.Fatalf("%q: %v", tc.yaml, err) - } + t.Run(fmt.Sprintf("yaml input: %q", tc.yaml), func(t *testing.T) { + cfg := Config{RootDir: testRoot} + if err := parseConfig([]byte(tc.yaml), &cfg); err != nil { + t.Fatal(err) + } - if cfg.LogFile != tc.path { - t.Fatalf("%q: expected %q, got %q", tc.yaml, tc.path, cfg.LogFile) - } + if cfg.LogFile != tc.path { + t.Fatalf("expected %q, got %q", tc.path, cfg.LogFile) + } + + if cfg.LogFormat != tc.format { + t.Fatalf("expected %q, got %q", tc.format, cfg.LogFormat) + } + }) } } diff --git a/go/internal/logger/logger.go b/go/internal/logger/logger.go index 6a4f2e3..c356d43 100644 --- a/go/internal/logger/logger.go +++ b/go/internal/logger/logger.go @@ -3,18 +3,19 @@ package logger import ( "fmt" "io" - "log" + golog "log" "log/syslog" "os" "sync" - "time" "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" + + log "github.com/sirupsen/logrus" ) var ( logWriter io.Writer - bootstrapLogger *log.Logger + bootstrapLogger *golog.Logger pid int mutex sync.Mutex ProgName string @@ -28,7 +29,16 @@ func Configure(cfg *config.Config) error { var err error logWriter, err = os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_APPEND, 0) - return err + if err != nil { + return err + } + + log.SetOutput(logWriter) + if cfg.LogFormat == "json" { + log.SetFormatter(&log.JSONFormatter{}) + } + + return nil } func logPrint(msg string, err error) { @@ -40,10 +50,9 @@ func logPrint(msg string, err error) { return } - // Emulate the existing log format of gitlab-shell - t := time.Now().Format("2006-01-02T15:04:05.999999") - prefix := fmt.Sprintf("E, [%s #%d] ERROR -- : %s:", t, pid, ProgName) - fmt.Fprintf(logWriter, "%s %s: %v\n", prefix, msg, err) + log.WithError(err).WithFields(log.Fields{ + "pid": pid, + }).Error(msg) } func Fatal(msg string, err error) { |