-
Notifications
You must be signed in to change notification settings - Fork 0
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 #39 from xmidt-org/denopink/refactoring/archive-we…
…bpa-common/logging Migrate Useful `webpa-common/logging` Utilities
- Loading branch information
Showing
17 changed files
with
374 additions
and
44 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
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
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,38 @@ | ||
package sallust | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// LoggerFunc is a strategy for adding key/value pairs (possibly) based on an HTTP request. | ||
// Functions of this type must append key/value pairs to the supplied slice and then return | ||
// the new slice. | ||
type LoggerFunc func([]zap.Field, *http.Request) []zap.Field | ||
|
||
func NewTestLogger(level zapcore.Level) (*bytes.Buffer, *zap.Logger) { | ||
b := new(bytes.Buffer) | ||
return b, zap.New(zapcore.NewCore( | ||
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), | ||
zap.CombineWriteSyncers(os.Stderr, zapcore.AddSync(b)), | ||
level, | ||
)) | ||
} | ||
|
||
// NewServerLogger creates a new zap.Logger appropriate for http.Server.ErrorLog | ||
func NewServerLogger(serverName string, logger *zap.Logger) *log.Logger { | ||
if logger == nil { | ||
logger = Default() | ||
} | ||
|
||
return log.New( | ||
zap.NewStdLog(logger).Writer(), | ||
serverName, | ||
log.LstdFlags|log.LUTC, | ||
) | ||
} |
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,24 @@ | ||
package sallust | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func TestNewServerLogger(t *testing.T) { | ||
testLog := "foobar" | ||
sn := "serverName" | ||
require := require.New(t) | ||
assert := assert.New(t) | ||
verify, logger := NewTestLogger(zapcore.InfoLevel) | ||
l := NewServerLogger(sn, logger) | ||
require.NotNil(l) | ||
l.Print(testLog) | ||
vstring := verify.String() | ||
for _, tlog := range []string{sn, testLog} { | ||
assert.Contains(vstring, tlog) | ||
} | ||
} |
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,24 @@ | ||
package sallusthttp | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func NewConnStateLogger(logger *zap.Logger, lvl zapcore.Level, fs ...zap.Field) func(net.Conn, http.ConnState) { | ||
return func(c net.Conn, cs http.ConnState) { | ||
fs = append(fs, zap.String("connState", cs.String())) | ||
if addr := c.LocalAddr(); addr != nil { | ||
fs = append(fs, zap.String("localAddress", addr.String())) | ||
} | ||
|
||
logger.Log( | ||
lvl, | ||
"connState", | ||
fs..., | ||
) | ||
} | ||
} |
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,47 @@ | ||
package sallusthttp | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xmidt-org/sallust" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func assertBufferContains(assert *assert.Assertions, verify *bytes.Buffer, values ...string) { | ||
text := verify.String() | ||
for _, value := range values { | ||
assert.Contains(text, value) | ||
} | ||
} | ||
|
||
func assertConnState(assert *assert.Assertions, verify *bytes.Buffer, connState func(net.Conn, http.ConnState)) { | ||
if assert.NotNil(connState) { | ||
conn1, conn2 := net.Pipe() | ||
defer conn1.Close() | ||
defer conn2.Close() | ||
|
||
assert.NotPanics(func() { | ||
connState(conn1, http.StateNew) | ||
}) | ||
if verify != nil { | ||
assertBufferContains(assert, verify, conn1.LocalAddr().String(), http.StateNew.String()) | ||
} | ||
} | ||
} | ||
|
||
func TestNewConnStateLogger(t *testing.T) { | ||
var ( | ||
assert = assert.New(t) | ||
require = require.New(t) | ||
v, l = sallust.NewTestLogger(zap.DebugLevel) | ||
connState = NewConnStateLogger(l, zap.DebugLevel) | ||
) | ||
|
||
require.NotNil(connState) | ||
assertConnState(assert, v, connState) | ||
} |
Oops, something went wrong.