Skip to content

Commit

Permalink
caddyhttp: Add --debug flag to commands
Browse files Browse the repository at this point in the history
file-server and reverse-proxy

This might be useful!
  • Loading branch information
mholt committed Sep 16, 2022
1 parent 48d723c commit b6cec37
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions caddyconfig/httpcaddyfile/httptype.go
Expand Up @@ -223,7 +223,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
hasDefaultLog = true
}
if _, ok := options["debug"]; ok && ncl.log.Level == "" {
ncl.log.Level = "DEBUG"
ncl.log.Level = zap.DebugLevel.CapitalString()
}
customLogs = append(customLogs, ncl)
}
Expand All @@ -241,7 +241,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
if _, ok := options["debug"]; ok {
customLogs = append(customLogs, namedCustomLog{
name: "default",
log: &caddy.CustomLog{Level: "DEBUG"},
log: &caddy.CustomLog{Level: zap.DebugLevel.CapitalString()},
})
}
}
Expand Down
10 changes: 10 additions & 0 deletions modules/caddyhttp/fileserver/command.go
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
caddytpl "github.com/caddyserver/caddy/v2/modules/caddyhttp/templates"
"github.com/caddyserver/certmagic"
"go.uber.org/zap"
)

func init() {
Expand Down Expand Up @@ -70,6 +71,7 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
browse := fs.Bool("browse")
templates := fs.Bool("templates")
accessLog := fs.Bool("access-log")
debug := fs.Bool("debug")

var handlers []json.RawMessage

Expand Down Expand Up @@ -130,6 +132,14 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
},
}

if debug {
cfg.Logging = &caddy.Logging{
Logs: map[string]*caddy.CustomLog{
"default": {Level: zap.DebugLevel.CapitalString()},
},
}
}

err := caddy.Run(cfg)
if err != nil {
return caddy.ExitCodeFailedStartup, err
Expand Down
11 changes: 11 additions & 0 deletions modules/caddyhttp/reverseproxy/command.go
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/headers"
"github.com/caddyserver/caddy/v2/modules/caddytls"
"go.uber.org/zap"
)

func init() {
Expand Down Expand Up @@ -62,6 +63,7 @@ default, all incoming headers are passed through unmodified.)
fs.Bool("change-host-header", false, "Set upstream Host header to address of upstream")
fs.Bool("insecure", false, "Disable TLS verification (WARNING: DISABLES SECURITY BY NOT VERIFYING SSL CERTIFICATES!)")
fs.Bool("internal-certs", false, "Use internal CA for issuing certs")
fs.Bool("debug", false, "Enable verbose debug logs")
return fs
}(),
})
Expand All @@ -74,6 +76,7 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
changeHost := fs.Bool("change-host-header")
insecure := fs.Bool("insecure")
internalCerts := fs.Bool("internal-certs")
debug := fs.Bool("debug")

httpPort := strconv.Itoa(caddyhttp.DefaultHTTPPort)
httpsPort := strconv.Itoa(caddyhttp.DefaultHTTPSPort)
Expand Down Expand Up @@ -198,6 +201,14 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
AppsRaw: appsRaw,
}

if debug {
cfg.Logging = &caddy.Logging{
Logs: map[string]*caddy.CustomLog{
"default": {Level: zap.DebugLevel.CapitalString()},
},
}
}

err = caddy.Run(cfg)
if err != nil {
return caddy.ExitCodeFailedStartup, err
Expand Down
3 changes: 2 additions & 1 deletion modules/caddyhttp/staticresp.go
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
"go.uber.org/zap"
)

func init() {
Expand Down Expand Up @@ -403,7 +404,7 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
if debug {
cfg.Logging = &caddy.Logging{
Logs: map[string]*caddy.CustomLog{
"default": {Level: "DEBUG"},
"default": {Level: zap.DebugLevel.CapitalString()},
},
}
}
Expand Down

0 comments on commit b6cec37

Please sign in to comment.