Skip to content

Commit

Permalink
Merge pull request #39 from xmidt-org/denopink/refactoring/archive-we…
Browse files Browse the repository at this point in the history
…bpa-common/logging

Migrate Useful `webpa-common/logging` Utilities
  • Loading branch information
denopink committed Nov 3, 2022
2 parents 7dccc60 + b85c841 commit f3715ed
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 44 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ jobs:
ci:
uses: xmidt-org/.github/.github/workflows/go-ci.yml@go-ci-v1
with:
lint-skip: true
style-skip: true
secrets: inherit
12 changes: 5 additions & 7 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
linters-settings:
misspell:
locale: US

errorlint:
# Report non-wrapping error creation using fmt.Errorf
errorf: false

linters:
enable:
- bodyclose
- dupl
- errorlint
- funlen
# - funlen
- goconst
- gosec
- misspell
Expand All @@ -24,8 +27,3 @@ issues:
linters:
- dupl
- funlen

linters-settings:
errorlint:
# Report non-wrapping error creation using fmt.Errorf
errorf: false
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- [Migrate Useful webpa-common/logging Utilities #37](https://github.com/xmidt-org/sallust/issues/37)
- [Enable & Fix Linter #36](https://github.com/xmidt-org/sallust/issues/36)

## [v0.1.6]

### Fixed
- levelEncoder, timeEncoder, durationEncoder, callerEncoder, nameEncoder marshalling
- levelEncoder, timeEncoder, durationEncoder, callerEncoder, nameEncoder marshaling
due to upstream viper change to support mapstructure.
### Added
- Bootstrapping a zap logger for a fx application, including the fxevent.Logger.
Expand Down
3 changes: 2 additions & 1 deletion captureCore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func TestCaptureCore(t *testing.T) {
var (
assert = assert.New(t)
require = require.New(t)
enc = zapcore.NewJSONEncoder(zapcore.EncoderConfig{
// nolint:staticcheck
enc = zapcore.NewJSONEncoder(zapcore.EncoderConfig{
MessageKey: "msg",
EncodeLevel: zapcore.LowercaseLevelEncoder,
})
Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ type EncoderConfig struct {
// StringDurationEncoder is used.
//
// See: https://pkg.go.dev/go.uber.org/zap/zapcore#StringDurationEncoder
EncodeDuration string `json:"durationEncoder" yaml:"durationEncoder"mapstructure:"durationEncoder"`
EncodeDuration string `json:"durationEncoder" yaml:"durationEncoder" mapstructure:"durationEncoder"`

// EncodeCaller determines how callers are represented. If unset,
// FullCallerEncoder is used.
//
// See: https://pkg.go.dev/go.uber.org/zap/zapcore#FullCallerEncoder
EncodeCaller string `json:"callerEncoder" yaml:"callerEncoder"mapstructure:"callerEncoder"`
EncodeCaller string `json:"callerEncoder" yaml:"callerEncoder" mapstructure:"callerEncoder"`

// EncodeName determines how logger names are represented. If unset,
// FullNameEncoder is used.
//
// See: https://pkg.go.dev/go.uber.org/zap/zapcore#FullNameEncoder
EncodeName string `json:"nameEncoder" yaml:"nameEncoder"mapstructure:"nameEncoder"`
EncodeName string `json:"nameEncoder" yaml:"nameEncoder" mapstructure:"nameEncoder"`

// Configures the field separator used by the console encoder. Defaults
// to tab.
Expand Down
14 changes: 14 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
// within a context.Context instance
type contextKey struct{}

// GetLoggerFunc is the function used to get a request-specific logger from
// its context.
type GetLoggerFunc func(context.Context) *zap.Logger

// defaultLogger is used when no logger exists in the context
var defaultLogger *zap.Logger = zap.NewNop()

Expand Down Expand Up @@ -62,3 +66,13 @@ func GetDefault(ctx context.Context, def *zap.Logger) *zap.Logger {

return Default()
}

// GetDefaultLogger returns the default logger, which doesn't do anything.
func GetDefaultLogger(_ context.Context) *zap.Logger {
return Default()
}

// GetNilLogger returns nil.
func GetNilLogger(_ context.Context) *zap.Logger {
return nil
}
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ go 1.18

require (
github.com/go-kit/log v0.2.1
github.com/gorilla/mux v1.8.0
github.com/stretchr/testify v1.8.1
go.uber.org/fx v1.18.2
go.uber.org/zap v1.23.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
golang.org/x/sys v0.1.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -9,12 +9,14 @@ github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -29,8 +31,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/dig v1.15.0 h1:vq3YWr8zRj1eFGC7Gvf907hE0eRjPTZ1d3xHadD6liE=
go.uber.org/dig v1.15.0/go.mod h1:pKHs0wMynzL6brANhB2hLMro+zalv1osARTviTcqHLM=
go.uber.org/fx v1.18.2 h1:bUNI6oShr+OVFQeU8cDNbnN7VFsu+SsjHzUF51V/GAU=
Expand All @@ -40,11 +42,11 @@ go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
38 changes: 38 additions & 0 deletions log.go
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,
)
}
24 changes: 24 additions & 0 deletions log_test.go
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)
}
}
2 changes: 1 addition & 1 deletion lumberjack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func testNewLumberjackSinkSuccess(t *testing.T) {
actual, ok := s.(Lumberjack)
require.True(ok)

// we only want to compare the public fields, so use JSON marshalling as a filter
// we only want to compare the public fields, so use JSON marshaling as a filter
expectedJSON, err := json.Marshal(record.expected)
require.NoError(err)

Expand Down
24 changes: 24 additions & 0 deletions sallusthttp/log.go
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...,
)
}
}
47 changes: 47 additions & 0 deletions sallusthttp/log_test.go
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)
}
Loading

0 comments on commit f3715ed

Please sign in to comment.