Merge pull request #367 from gianrubio/customize-logformat

BuildLogFormatUpstream was always using the default log-format
This commit is contained in:
Manuel Alejandro de Brito Fontes 2017-03-03 09:43:48 -03:00 committed by GitHub
commit 3b2f668f39
3 changed files with 16 additions and 6 deletions

View file

@ -264,7 +264,7 @@ func NewDefault() Configuration {
KeepAlive: 75, KeepAlive: 75,
LargeClientHeaderBuffers: "4 8k", LargeClientHeaderBuffers: "4 8k",
LogFormatStream: logFormatStream, LogFormatStream: logFormatStream,
LogFormatUpstream: BuildLogFormatUpstream(false), LogFormatUpstream: BuildLogFormatUpstream(false, ""),
MaxWorkerConnections: 16384, MaxWorkerConnections: 16384,
MapHashBucketSize: 64, MapHashBucketSize: 64,
ProxyRealIPCIDR: defIPCIDR, ProxyRealIPCIDR: defIPCIDR,
@ -308,7 +308,14 @@ func NewDefault() Configuration {
} }
// BuildLogFormatUpstream format the log_format upstream based on proxy_protocol // BuildLogFormatUpstream format the log_format upstream based on proxy_protocol
func BuildLogFormatUpstream(useProxyProtocol bool) string { func BuildLogFormatUpstream(useProxyProtocol bool, curLogFormatUpstream string) string {
// test if log_format comes from configmap
if curLogFormatUpstream != "" &&
curLogFormatUpstream != fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr") &&
curLogFormatUpstream != fmt.Sprintf(logFormatUpstream, "$remote_addr") {
return curLogFormatUpstream
}
if useProxyProtocol { if useProxyProtocol {
return fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr") return fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")

View file

@ -9,15 +9,18 @@ func TestBuildLogFormatUpstream(t *testing.T) {
testCases := []struct { testCases := []struct {
useProxyProtocol bool // use proxy protocol useProxyProtocol bool // use proxy protocol
curLogFormat string
expected string expected string
}{ }{
{true, fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")}, {true, "", fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")},
{false, fmt.Sprintf(logFormatUpstream, "$remote_addr")}, {false, "", fmt.Sprintf(logFormatUpstream, "$remote_addr")},
{true, "my-log-format", "my-log-format"},
{false, "john-log-format", "john-log-format"},
} }
for _, testCase := range testCases { for _, testCase := range testCases {
result := BuildLogFormatUpstream(testCase.useProxyProtocol) result := BuildLogFormatUpstream(testCase.useProxyProtocol, testCase.curLogFormat)
if result != testCase.expected { if result != testCase.expected {
t.Errorf(" expected %v but return %v", testCase.expected, result) t.Errorf(" expected %v but return %v", testCase.expected, result)

View file

@ -235,7 +235,7 @@ func buildLogFormatUpstream(input interface{}) string {
glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input) glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input)
} }
return nginxconfig.BuildLogFormatUpstream(config.UseProxyProtocol) return nginxconfig.BuildLogFormatUpstream(config.UseProxyProtocol, config.LogFormatUpstream)
} }