Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

Commit

Permalink
main: added structured log option [Fixes #39]
Browse files Browse the repository at this point in the history
  • Loading branch information
faizan.ali authored and faizan.ali committed May 15, 2020
1 parent 842c5f8 commit 2566ba9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Usage of git2consul:
path to config file
-debug
enable debugging mode
-logfmt string
specify log format [text | json] (default "text")
-once
run git2consul once and exit
-version
Expand Down
37 changes: 26 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/KohlsTechnology/git2consul-go/pkg/version"
"github.com/KohlsTechnology/git2consul-go/runner"
"github.com/apex/log"
"github.com/apex/log/handlers/json"
"github.com/apex/log/handlers/text"
)

Expand All @@ -39,17 +40,30 @@ const (
)

func main() {
var filename string
var printVersion bool
var debug bool
var once bool

var (
filename string
printVersion bool
debug bool
once bool
logfmt string
)

flag.StringVar(&filename, "config", "", "path to config file")
flag.BoolVar(&printVersion, "version", false, "show version")
flag.BoolVar(&debug, "debug", false, "enable debugging mode")
flag.BoolVar(&once, "once", false, "run git2consul once and exit")
// allow switching logformat. Structured output helps with parsers
flag.StringVar(&logfmt, "logfmt", "text", "specify log format [text | json] ")
flag.Parse()

// Init checks
if len(filename) == 0 {
log.Errorf("No configuration file provided")
flag.Usage()
os.Exit(ExitCodeFlagError)
}

if debug {
log.SetLevel(log.DebugLevel)
}
Expand All @@ -60,15 +74,16 @@ func main() {
}

// TODO: Accept other logger inputs
log.SetHandler(text.New(os.Stderr))

log.Infof("Starting git2consul version: %s", version.Version)

if len(filename) == 0 {
log.Error("No configuration file provided")
os.Exit(ExitCodeFlagError)
switch logfmt {
case "text":
log.SetHandler(text.New(os.Stderr))
case "json":
log.SetHandler(json.New(os.Stderr))
}

//log.Infof("Starting git2consul version: %s", version.Version)
log.WithField("caller", "main").Infof("Starting git2consul version: %s", version.Version)

// Load configuration from file
cfg, err := config.Load(filename)
if err != nil {
Expand Down

0 comments on commit 2566ba9

Please sign in to comment.