Skip to content

Commit

Permalink
Logging (#8)
Browse files Browse the repository at this point in the history
Add support for structured logging with either JSON or [logfmt](https://brandur.org/logfmt) output
  • Loading branch information
groob authored Jul 7, 2017
1 parent 0867ff7 commit 11f3633
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.2
* Added support for structured logging. Use -log-format=json for JSON output, and -log-format=logfmt (default) for [logfmt](https://brandur.org/logfmt) logging.
* Added new flag `-no-help` to disable printing the auth header to stdout.

# 1.0.1
* Added support for pluggable storage - Google Cloud and S3 (#4 and #7)
* Added /version URL route (#2).
Expand Down
30 changes: 27 additions & 3 deletions cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (

"golang.org/x/crypto/acme/autocert"

kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/groob/finalizer/logutil"
"github.com/micromdm/squirrel/storage/gcs"
"github.com/micromdm/squirrel/storage/s3"
"github.com/micromdm/squirrel/version"
Expand All @@ -36,6 +39,8 @@ func serve(cmd *flag.FlagSet) int {
flTLSCert = cmd.String("tls-cert", envString("SQUIRREL_TLS_CERT", ""), "path to TLS certificate")
flTLSKey = cmd.String("tls-key", envString("SQUIRREL_TLS_KEY", ""), "path to TLS private key")
flTLSAuto = cmd.String("tls-domain", envString("SQUIRREL_AUTO_TLS_DOMAIN", ""), "Automatically fetch certs from Let's Encrypt")
flLogFormat = cmd.String("log-format", envString("SQUIRREL_LOG_FORMAT", "logfmt"), "Enable structured logging. Supported formats: logfmt, json")
flSilent = cmd.Bool("no-help", envBool("SQUIRREL_NO_HELP_TEXT", false), "Silence help text to avoid displaying Auth headers in log.")
)
cmd.Parse(os.Args[2:])

Expand Down Expand Up @@ -82,9 +87,26 @@ squirrel with -gcs-credentials=/path/to/service-account.json
mux.Handle("/version", version.Handler())
mux.Handle("/healthz", healthzHandler)

var logger kitlog.Logger
{
w := kitlog.NewSyncWriter(os.Stderr)
switch *flLogFormat {
case "json":
logger = kitlog.NewJSONLogger(w)
default:
logger = kitlog.NewLogfmtLogger(w)
}
log.SetOutput(kitlog.NewStdlibAdapter(logger))
logger = kitlog.With(logger, "ts", kitlog.DefaultTimestampUTC)
logger = kitlog.With(logger, "component", "http")
logger = level.Info(logger)

}
h := logutil.NewHTTPLogger(logger).Middleware(mux)

srv := &http.Server{
Addr: ":https",
Handler: mux,
Handler: h,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
Expand All @@ -93,9 +115,11 @@ squirrel with -gcs-credentials=/path/to/service-account.json
TLSConfig: tlsConfig(),
}

printMunkiHeadersHelp(*flBasicPassword)
if !*flSilent {
printMunkiHeadersHelp(*flBasicPassword)
}
if !*flTLS {
log.Fatal(http.ListenAndServe(":8080", mux))
log.Fatal(http.ListenAndServe(":8080", h))
return 0
}

Expand Down

0 comments on commit 11f3633

Please sign in to comment.