This commit is contained in:
Jeffrey Richter 2017-11-15 15:39:42 -08:00
Родитель 7b53d87fe8
Коммит 703a33df7c
5 изменённых файлов: 37 добавлений и 59 удалений

Просмотреть файл

@ -1,9 +1,7 @@
package pipeline
import (
"bytes"
"context"
"fmt"
"net"
"net/http"
"os"
@ -39,7 +37,7 @@ type Options struct {
// whose severity is at as as severe as what it was told to log. See the Log* constants.
// For example, if a logger is configured with LogError severity, then LogError, LogPanic,
// and LogFatal entries will be logged; less severe entries will be ignored.
type LogSeverity int
type LogSeverity uint32
const (
// LogNone tells a logger not to log any entries passed to it.
@ -157,8 +155,8 @@ func (n *Node) Do(ctx context.Context, request Request) (Response, error) {
return n.next.Do(ctx, request)
}
// WouldLog returns true if the specified severity level would be logged.
func (n *Node) WouldLog(severity LogSeverity) bool {
// ShouldLog returns true if the specified severity level should be logged.
func (n *Node) ShouldLog(severity LogSeverity) bool {
minimum := LogNone
if n.pipeline.options.Log.MinimumSeverityToLog != nil {
minimum = n.pipeline.options.Log.MinimumSeverityToLog()
@ -168,14 +166,16 @@ func (n *Node) WouldLog(severity LogSeverity) bool {
// Log logs a string to the Pipeline's Logger.
func (n *Node) Log(severity LogSeverity, msg string) {
if !n.WouldLog(severity) {
if !n.ShouldLog(severity) {
return // Short circuit message formatting if we're not logging it
}
// We are logging it, ensure trailing newline
if len(msg) == 0 || msg[len(msg)-1] != '\n' {
msg += "\n" // Ensure trailing newline
}
defaultLog(severity, msg)
n.pipeline.options.Log.Log(severity, msg)
// If logger doesn't handle fatal/panic, we'll do it here.
if severity == LogFatal {
os.Exit(1)
@ -184,19 +184,6 @@ func (n *Node) Log(severity LogSeverity, msg string) {
}
}
// Logf logs a string to the Pipeline's Logger.
func (n *Node) Logf(severity LogSeverity, format string, v ...interface{}) {
if !n.WouldLog(severity) {
return // Short circuit message formatting if we're not logging it
}
b := &bytes.Buffer{}
fmt.Fprintf(b, format, v...)
if b.Len() == 0 || b.Bytes()[b.Len()-1] != '\n' {
b.WriteRune('\n') // Ensure trailing newline
}
n.Log(severity, b.String())
}
var pipelineHTTPClient = newDefaultHTTPClient()
func newDefaultHTTPClient() *http.Client {

Просмотреть файл

@ -7,30 +7,23 @@ import (
"log/syslog"
)
func defaultLog(severity LogSeverity, format string, a ...interface{}) {
// ForceLog should rarely be used. It forceable logs an entry to the
// Windows Event Log (on Windows) or to the SysLog (on Linux)
func ForceLog(severity LogSeverity, msg string) {
if defaultLogger == nil {
return // Return fast if we failed to create the logger.
}
// We are logging it, ensure trailing newline
if len(msg) == 0 || msg[len(msg)-1] != '\n' {
msg += "\n" // Ensure trailing newline
}
switch severity {
case LogFatal:
if format == "" {
defaultLogger.Fatal(a...)
} else {
defaultLogger.Fatalf(format, a...)
}
defaultLogger.Fatal(msg)
case LogPanic:
if format == "" {
defaultLogger.Panic(a...)
} else {
defaultLogger.Panicf(format, a...)
}
case LogError, LogWarning:
if format == "" {
defaultLogger.Print(a...)
} else {
defaultLogger.Printf(format, a...)
}
default: // Do not log less severe entries
defaultLogger.Panic(msg)
case LogError, LogWarning, LogInfo:
defaultLogger.Print(msg)
}
}

Просмотреть файл

@ -1,31 +1,28 @@
package pipeline
import (
"fmt"
"os"
"syscall"
"unsafe"
)
func defaultLog(level LogSeverity, format string, a ...interface{}) {
// ForceLog should rarely be used. It forceable logs an entry to the
// Windows Event Log (on Windows) or to the SysLog (on Linux)
func ForceLog(level LogSeverity, msg string) {
var el eventType
switch level {
case LogError, LogFatal, LogPanic:
s := ""
if format == "" {
s = fmt.Sprint(a...)
} else {
s = fmt.Sprintf(format, a...)
}
reportEvent(elError, 0, s)
el = elError
case LogWarning:
s := ""
if format == "" {
s = fmt.Sprint(a...)
} else {
s = fmt.Sprintf(format, a...)
}
reportEvent(elWarning, 0, s)
el = elWarning
case LogInfo:
el = elInfo
}
// We are logging it, ensure trailing newline
if len(msg) == 0 || msg[len(msg)-1] != '\n' {
msg += "\n" // Ensure trailing newline
}
reportEvent(el, 0, msg)
}
type eventType int16

Просмотреть файл

@ -2,7 +2,7 @@ package pipeline
import "io"
// ********** The following types are common between the request body AND the response body.
// ********** The following is common between the request body AND the response body.
// ProgressReceiver defines the signature of a callback function invoked as progress is reported.
type ProgressReceiver func(bytesTransferred int64)

Просмотреть файл

@ -40,9 +40,9 @@ func WriteRequest(b *bytes.Buffer, request *http.Request) {
writeHeader(b, request.Header)
}
// WriteResponseWithRequest appends a formatted HTTP response with its initiating request into a Buffer.
func WriteResponseWithRequest(b *bytes.Buffer, response *http.Response) {
WriteRequest(b, response.Request) // Write the request first followed by the response.
// WriteRequestWithResponse appends a formatted HTTP response with its initiating request into a Buffer.
func WriteRequestWithResponse(b *bytes.Buffer, request *http.Request, response *http.Response) {
WriteRequest(b, request) // Write the request first followed by the response.
fmt.Fprintln(b, " --------------------------------------------------------------------------------")
fmt.Fprintf(b, " RESPONSE Status: %s\n", response.Status)
writeHeader(b, response.Header)
@ -61,7 +61,8 @@ func writeHeader(b *bytes.Buffer, header map[string][]string) {
}
sort.Strings(keys)
for _, k := range keys {
value := interface{}("(redacted)")
// Redact the value of any Authorization header to prevent security information from persisting in logs
value := interface{}("REDACTED")
if !strings.EqualFold(k, "Authorization") {
value = header[k]
}