Skip to content

Commit

Permalink
Issue #80: Access logging
Browse files Browse the repository at this point in the history
Move log config parameters to global level:

 - log.access.format
 - log.access.target
 - log.routes.format
  • Loading branch information
magiconair committed Apr 4, 2017
1 parent 9d0ac03 commit d0ca576
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 49 deletions.
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Config struct {
Proxy Proxy
Registry Registry
Listen []Listen
Log Log
Metrics Metrics
UI UI
Runtime Runtime
Expand Down Expand Up @@ -56,8 +57,6 @@ type Proxy struct {
TLSHeader string
TLSHeaderValue string
GZIPContentTypes *regexp.Regexp
LogRoutes string
Log Log
}

type Runtime struct {
Expand All @@ -74,8 +73,9 @@ type Circonus struct {
}

type Log struct {
Target string
Format string
AccessFormat string
AccessTarget string
RoutesFormat string
}

type Metrics struct {
Expand Down
24 changes: 12 additions & 12 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ var defaultValues = struct {
}

var defaultConfig = &Config{
Log: Log{
AccessFormat: "common",
RoutesFormat: "delta",
},
Metrics: Metrics{
Prefix: "{{clean .Hostname}}.{{clean .Exec}}",
Names: "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}",
Interval: 30 * time.Second,
Circonus: Circonus{
APIApp: "fabio",
},
},
Proxy: Proxy{
MaxConn: 10000,
Strategy: "rnd",
Expand All @@ -25,10 +37,6 @@ var defaultConfig = &Config{
DialTimeout: 30 * time.Second,
FlushInterval: time.Second,
LocalIP: LocalIPString(),
LogRoutes: "delta",
Log: Log{
Format: "common",
},
},
Registry: Registry{
Backend: "consul",
Expand All @@ -55,12 +63,4 @@ var defaultConfig = &Config{
Addr: ":9998",
Color: "light-green",
},
Metrics: Metrics{
Prefix: "{{clean .Hostname}}.{{clean .Exec}}",
Names: "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}",
Interval: 30 * time.Second,
Circonus: Circonus{
APIApp: "fabio",
},
},
}
25 changes: 17 additions & 8 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
f.DurationVar(&readTimeout, "proxy.readtimeout", defaultValues.ReadTimeout, "read timeout for incoming requests")
f.DurationVar(&writeTimeout, "proxy.writetimeout", defaultValues.WriteTimeout, "write timeout for outgoing responses")
f.DurationVar(&cfg.Proxy.FlushInterval, "proxy.flushinterval", defaultConfig.Proxy.FlushInterval, "flush interval for streaming responses")
f.StringVar(&cfg.Proxy.LogRoutes, "proxy.log.routes", defaultConfig.Proxy.LogRoutes, "log format of routing table updates")
f.StringVar(&cfg.Proxy.Log.Format, "proxy.log.format", defaultConfig.Proxy.Log.Format, "access log format")
f.StringVar(&cfg.Proxy.Log.Target, "proxy.log.target", defaultConfig.Proxy.Log.Target, "access log target")
f.StringVar(&cfg.Log.AccessFormat, "log.access.format", defaultConfig.Log.AccessFormat, "access log format")
f.StringVar(&cfg.Log.AccessTarget, "log.access.target", defaultConfig.Log.AccessTarget, "access log target")
f.StringVar(&cfg.Log.RoutesFormat, "log.routes.format", defaultConfig.Log.RoutesFormat, "log format of routing table updates")
f.StringVar(&cfg.Metrics.Target, "metrics.target", defaultConfig.Metrics.Target, "metrics backend")
f.StringVar(&cfg.Metrics.Prefix, "metrics.prefix", defaultConfig.Metrics.Prefix, "prefix for reported metrics")
f.StringVar(&cfg.Metrics.Names, "metrics.names", defaultConfig.Metrics.Names, "route metric name template")
Expand Down Expand Up @@ -167,6 +167,10 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
f.StringVar(&cfg.UI.Color, "ui.color", defaultConfig.UI.Color, "background color of the UI")
f.StringVar(&cfg.UI.Title, "ui.title", defaultConfig.UI.Title, "optional title for the UI")

// deprecated flags
var proxyLogRoutes string
f.StringVar(&proxyLogRoutes, "proxy.log.routes", "", "deprecated. use log.routes.format instead")

var awsApiGWCertCN string
f.StringVar(&awsApiGWCertCN, "aws.apigw.cert.cn", "", "deprecated. use caupgcn=<CN> for cert source")

Expand Down Expand Up @@ -208,11 +212,16 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
}

// handle deprecations
// deprecate := func(name, msg string) {
// if f.IsSet(name) {
// log.Print("[WARN] ", msg)
// }
// }
deprecate := func(name, msg string) {
if f.IsSet(name) {
log.Print("[WARN] ", msg)
}
}
deprecate("proxy.log.routes", "proxy.log.routes has been deprecated. Please use 'log.routes.format' instead")

if proxyLogRoutes != "" {
cfg.Log.RoutesFormat = proxyLogRoutes
}

return cfg, nil
}
Expand Down
49 changes: 28 additions & 21 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,27 +309,6 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
args: []string{"-proxy.log.routes", "foobar"},
cfg: func(cfg *Config) *Config {
cfg.Proxy.LogRoutes = "foobar"
return cfg
},
},
{
args: []string{"-proxy.log.format", "foobar"},
cfg: func(cfg *Config) *Config {
cfg.Proxy.Log.Format = "foobar"
return cfg
},
},
{
args: []string{"-proxy.log.target", "foobar"},
cfg: func(cfg *Config) *Config {
cfg.Proxy.Log.Target = "foobar"
return cfg
},
},
{
args: []string{"-registry.backend", "value"},
cfg: func(cfg *Config) *Config {
Expand Down Expand Up @@ -467,6 +446,34 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
args: []string{"-log.access.format", "foobar"},
cfg: func(cfg *Config) *Config {
cfg.Log.AccessFormat = "foobar"
return cfg
},
},
{
args: []string{"-log.access.target", "foobar"},
cfg: func(cfg *Config) *Config {
cfg.Log.AccessTarget = "foobar"
return cfg
},
},
{
args: []string{"-log.routes.format", "foobar"},
cfg: func(cfg *Config) *Config {
cfg.Log.RoutesFormat = "foobar"
return cfg
},
},
{
args: []string{"-proxy.log.routes", "foobar"},
cfg: func(cfg *Config) *Config {
cfg.Log.RoutesFormat = "foobar"
return cfg
},
},
{
args: []string{"-metrics.target", "some-target"},
cfg: func(cfg *Config) *Config {
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ func main() {

func newHTTPProxy(cfg *config.Config) http.Handler {
var w io.Writer
switch cfg.Proxy.Log.Target {
switch cfg.Log.AccessTarget {
case "":
log.Printf("[INFO] Access logging disabled")
case "stdout":
log.Printf("[INFO] Writing access log to stdout")
w = os.Stdout
default:
log.Fatal("[FATAL] Invalid access log target ", cfg.Proxy.Log.Target)
log.Fatal("[FATAL] Invalid access log target ", cfg.Log.AccessTarget)
}

format := cfg.Proxy.Log.Format
format := cfg.Log.AccessFormat
switch format {
case "common":
format = logger.Common
Expand Down Expand Up @@ -302,7 +302,7 @@ func watchBackend(cfg *config.Config, first chan bool) {
continue
}
route.SetTable(t)
logRoutes(last, next, cfg.Proxy.LogRoutes)
logRoutes(last, next, cfg.Log.RoutesFormat)
last = next

once.Do(func() { close(first) })
Expand Down

0 comments on commit d0ca576

Please sign in to comment.