From d0348f4bdeebd20dea1d1806dece8844e4b1dda0 Mon Sep 17 00:00:00 2001 From: Dan Potepa Date: Mon, 16 Aug 2021 12:35:49 +0100 Subject: [PATCH] feat: moved to use logutils --- app.go | 6 ++---- go.mod | 2 ++ go.sum | 9 ++++----- main.go | 20 +++++++++++++++----- template.go | 4 ++-- template_test.go | 2 +- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/app.go b/app.go index 20d4776..5c44235 100644 --- a/app.go +++ b/app.go @@ -37,16 +37,14 @@ func (a *App) handleIndex() http.HandlerFunc { err = getIndexTpl().Execute(buf, model) if err != nil { - log.Printf("failed to render template: %v", err) + log.Printf("[ERROR] failed to render template: %v", err) http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } if _, err := writer.Write(buf.Bytes()); err != nil { - log.Printf("failed to write response: %v\n", err) + log.Printf("[ERROR] failed to write response: %v\n", err) } - - return } } diff --git a/go.mod b/go.mod index d4d0deb..46a4e44 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-units v0.4.0 // indirect github.com/gorilla/mux v1.7.4 + github.com/hashicorp/logutils v1.0.0 github.com/kelseyhightower/envconfig v1.4.0 github.com/markbates/pkger v0.17.1 github.com/opencontainers/go-digest v1.0.0-rc1 // indirect @@ -17,4 +18,5 @@ require ( github.com/stretchr/testify v1.4.0 golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect golang.org/x/sys v0.0.0-20200428200454-593003d681fa // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index c3ff058..0f2c351 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,6 @@ github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -46,6 +45,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= +github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= @@ -95,7 +96,6 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= @@ -115,7 +115,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200428200454-593003d681fa h1:yMbJOvnfYkO1dSAviTu/ZguZWLBTXx4xE3LYrxUCCiA= @@ -131,7 +130,6 @@ google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zim google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -139,5 +137,6 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go index 6b8ab27..0ead996 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,11 @@ package main import ( "log" "net/http" + "os" "github.com/docker/docker/client" "github.com/gorilla/mux" + "github.com/hashicorp/logutils" "github.com/kelseyhightower/envconfig" ) @@ -27,6 +29,12 @@ type Container struct { } func init() { + filter := &logutils.LevelFilter{ + Levels: []logutils.LogLevel{"INFO", "ERROR", "FATAL"}, + MinLevel: logutils.LogLevel("INFO"), + Writer: os.Stderr, + } + log.SetOutput(filter) log.SetFlags(log.LstdFlags | log.Lshortfile) } @@ -35,12 +43,12 @@ func main() { c := &Config{} err := envconfig.Process("", c) if err != nil { - log.Fatal(err) + log.Fatalf("[FATAL] failed to parse env vars: %s", err) } cl, err := client.NewEnvClient() if err != nil { - log.Fatal(err) + log.Fatalf("[FATAL] %s", err) } app := App{ @@ -51,8 +59,10 @@ func main() { } app.routes() - log.Printf("listening on :8080") - log.Printf(`using receptionist label "%v"`, c.Label) + log.Printf("[INFO] listening on :8080") + log.Printf("[INFO] using receptionist label: %v", c.Label) - log.Fatal(http.ListenAndServe(":8080", app.Router)) + if err := http.ListenAndServe(":8080", app.Router); err != nil { + log.Fatalf("[FATAL] %s", err) + } } diff --git a/template.go b/template.go index 4e294e9..74331e1 100644 --- a/template.go +++ b/template.go @@ -17,13 +17,13 @@ func getIndexTpl() *template.Template { tplFile, err := pkger.Open("/assets/index.html.tpl") if err != nil { - log.Fatal("failed to open index template file") + log.Fatal("[FATAL] failed to open index template file") } defer tplFile.Close() tplBody, err := ioutil.ReadAll(tplFile) if err != nil { - log.Fatal("unable to read index template file") + log.Fatal("[FATAL] unable to read index template file") } return template.Must(template.New("IndexTpl").Parse(string(tplBody))) } diff --git a/template_test.go b/template_test.go index 754a77e..eb536a6 100644 --- a/template_test.go +++ b/template_test.go @@ -32,4 +32,4 @@ func TestTemplateRendering(t *testing.T) { // TODO: check if there is a stdlib xhml parser err = xml.Unmarshal(rendered.Bytes(), new(interface{})) assert.NoError(t, err) -} \ No newline at end of file +}