v2: Logging! (#2831)

* logging: Initial implementation

* logging: More encoder formats, better defaults

* logging: Fix repetition bug with FilterEncoder; add more presets

* logging: DiscardWriter; delete or no-op logs that discard their output

* logging: Add http.handlers.log module; enhance Replacer methods

The Replacer interface has new methods to customize how to handle empty
or unrecognized placeholders. Closes #2815.

* logging: Overhaul HTTP logging, fix bugs, improve filtering, etc.

* logging: General cleanup, begin transitioning to using new loggers

* Fixes after merge conflict
This commit is contained in:
Matt Holt
2019-10-28 14:39:37 -06:00
committed by GitHub
parent 6c533558a3
commit b00dfd3965
34 changed files with 2234 additions and 201 deletions

View File

@ -34,6 +34,7 @@ import (
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/mholt/certmagic"
"github.com/rs/cors"
"go.uber.org/zap"
)
var (
@ -52,6 +53,8 @@ var DefaultAdminConfig = &AdminConfig{
Listen: DefaultAdminListen,
}
// TODO: holy smokes, the admin endpoint might not have to live in caddy's core.
// StartAdmin starts Caddy's administration endpoint,
// bootstrapping it with an optional configuration
// in the format of JSON bytes. It opens a listener
@ -113,7 +116,15 @@ func StartAdmin(initialConfigJSON []byte) error {
}
}
handler := cors.Default().Handler(mux)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: improve/organize this logging
Log().Named("admin.request").Info("",
zap.String("method", r.Method),
zap.String("uri", r.RequestURI),
zap.String("remote", r.RemoteAddr),
)
cors.Default().Handler(mux).ServeHTTP(w, r)
})
cfgEndptSrv = &http.Server{
Handler: handler,
@ -125,14 +136,14 @@ func StartAdmin(initialConfigJSON []byte) error {
go cfgEndptSrv.Serve(ln)
log.Println("Caddy 2 admin endpoint listening on", adminConfig.Listen)
fmt.Println("Caddy 2 admin endpoint listening on", adminConfig.Listen)
if len(initialConfigJSON) > 0 {
err := Load(bytes.NewReader(initialConfigJSON))
if err != nil {
return fmt.Errorf("loading initial config: %v", err)
}
log.Println("Caddy 2 serving initial configuration")
fmt.Println("Caddy 2 serving initial configuration")
}
return nil