-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7748 from hashicorp/b-noisy-http-logs
agent: route http logs through hclog
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package agent | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
) | ||
|
||
func newHTTPServerLogger(logger hclog.Logger) *log.Logger { | ||
return log.New(&httpServerLoggerAdapter{logger}, "", 0) | ||
} | ||
|
||
// a logger adapter that forwards http server logs as a Trace level | ||
// hclog log entries. Logs related to panics are forwarded with Error level. | ||
// | ||
// HTTP server logs are typically spurious as they represent HTTP | ||
// client errors (e.g. TLS handshake failures). | ||
type httpServerLoggerAdapter struct { | ||
logger hclog.Logger | ||
} | ||
|
||
func (l *httpServerLoggerAdapter) Write(data []byte) (int, error) { | ||
if bytes.Contains(data, []byte("panic")) { | ||
str := string(bytes.TrimRight(data, " \t\n")) | ||
l.logger.Error(str) | ||
} else if l.logger.IsTrace() { | ||
str := string(bytes.TrimRight(data, " \t\n")) | ||
l.logger.Trace(str) | ||
} | ||
|
||
return len(data), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package agent | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHttpServerLoggerFilters_Level_Info(t *testing.T) { | ||
var buf bytes.Buffer | ||
hclogger := hclog.New(&hclog.LoggerOptions{ | ||
Name: "testlog", | ||
Output: &buf, | ||
Level: hclog.Info, | ||
}) | ||
|
||
stdlogger := newHTTPServerLogger(hclogger) | ||
|
||
// spurious logging would be filtered out | ||
stdlogger.Printf("spurious logging: %v", "arg") | ||
require.Empty(t, buf.String()) | ||
|
||
// panics are included | ||
stdlogger.Printf("panic while processing: %v", "endpoint") | ||
require.Contains(t, buf.String(), "[ERROR] testlog: panic while processing: endpoint") | ||
|
||
} | ||
|
||
func TestHttpServerLoggerFilters_Level_Trace(t *testing.T) { | ||
var buf bytes.Buffer | ||
hclogger := hclog.New(&hclog.LoggerOptions{ | ||
Name: "testlog", | ||
Output: &buf, | ||
Level: hclog.Trace, | ||
}) | ||
|
||
stdlogger := newHTTPServerLogger(hclogger) | ||
|
||
// spurious logging will be included as Trace level | ||
stdlogger.Printf("spurious logging: %v", "arg") | ||
require.Contains(t, buf.String(), "[TRACE] testlog: spurious logging: arg") | ||
|
||
stdlogger.Printf("panic while processing: %v", "endpoint") | ||
require.Contains(t, buf.String(), "[ERROR] testlog: panic while processing: endpoint") | ||
|
||
} |