Skip to content

Commit

Permalink
Merge pull request #393 from hellofresh/patch/early-logger-writer-init
Browse files Browse the repository at this point in the history
Init log writer as early as possible
  • Loading branch information
vgarvardt authored Mar 27, 2019
2 parents 771ac8b + 21650ce commit 2f2497e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
16 changes: 16 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

"github.com/hellofresh/janus/pkg/config"
obs "github.com/hellofresh/janus/pkg/observability"
"github.com/hellofresh/logging-go"
"github.com/hellofresh/stats-go"
"github.com/hellofresh/stats-go/bucket"
"github.com/hellofresh/stats-go/client"
Expand All @@ -23,6 +26,19 @@ var (
statsClient client.Client
)

func initLogWriterEarly() {
switch logging.LogWriter(strings.ToLower(os.Getenv("LOG_WRITER"))) {
case logging.StdOut:
log.SetOutput(os.Stdout)
case logging.Discard:
log.SetOutput(ioutil.Discard)
case logging.StdErr:
fallthrough
default:
log.SetOutput(os.Stderr)
}
}

func initConfig() {
var err error
globalConfig, err = config.Load(configFile)
Expand Down
5 changes: 5 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func NewServerStartCmd(ctx context.Context) *cobra.Command {

// RunServerStart is the run command to start Janus
func RunServerStart(ctx context.Context, opts *ServerStartOptions) error {
// all the logging configurations are initialised in initLog() later,
// but we try to initialise Writer (STDIN/STDERR/etc.) as early as possible manually
// to avoid loosing logs in systems heavily relying on them (e.g. running in docker)
initLogWriterEarly()

log.WithField("version", version).Info("Janus starting...")

initConfig()
Expand Down
3 changes: 1 addition & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"fmt"

"github.com/spf13/cobra"
)
Expand All @@ -16,7 +15,7 @@ func NewVersionCmd(ctx context.Context) *cobra.Command {
Short: "Print the version information",
Aliases: []string{"v"},
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("janus %s\n", version)
cmd.Printf("janus %s\n", version)
},
}
}

0 comments on commit 2f2497e

Please sign in to comment.