Skip to content

Commit

Permalink
Merge pull request #28 from yaronius/master
Browse files Browse the repository at this point in the history
Pass logger options
  • Loading branch information
K-Phoen authored May 12, 2021
2 parents bc3e45a + 4911685 commit d51db3b
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 86 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ on: [pull_request]
jobs:
tests:
name: Tests
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
with:
depth: 1

- uses: actions/setup-go@v1
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.14'
go-version: '1.16'

- run: make test

Expand All @@ -27,8 +24,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v1
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.26
version: v1.29
args: -c .golangci.yaml
147 changes: 76 additions & 71 deletions coverage.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
mode: atomic
github.com/voi-oss/svc/config.go:10.44,11.42 1 0
github.com/voi-oss/svc/config.go:14.2,14.55 1 0
github.com/voi-oss/svc/config.go:17.2,17.12 1 0
github.com/voi-oss/svc/config.go:11.42,13.3 1 0
github.com/voi-oss/svc/config.go:14.55,16.3 1 0
github.com/voi-oss/svc/http.go:21.87,31.2 2 0
github.com/voi-oss/svc/http.go:34.53,38.2 2 0
github.com/voi-oss/svc/http.go:41.38,43.2 1 0
github.com/voi-oss/svc/http.go:46.34,48.71 2 0
github.com/voi-oss/svc/http.go:51.2,51.12 1 0
github.com/voi-oss/svc/http.go:48.71,50.3 1 0
github.com/voi-oss/svc/http.go:55.40,57.2 1 0
github.com/voi-oss/svc/logger.go:13.102,28.2 5 2
github.com/voi-oss/svc/logger.go:13.102,28.2 5 5
github.com/voi-oss/svc/logger.go:32.30,33.28 1 0
github.com/voi-oss/svc/logger.go:33.28,41.59 2 0
github.com/voi-oss/svc/logger.go:45.3,46.42 1 0
Expand All @@ -17,19 +22,19 @@ github.com/voi-oss/svc/logger.go:51.5,52.15 2 0
github.com/voi-oss/svc/logger.go:48.19,50.6 1 0
github.com/voi-oss/svc/logger.go:59.66,60.28 1 0
github.com/voi-oss/svc/logger.go:60.28,62.3 1 0
github.com/voi-oss/svc/logger.go:67.37,68.28 1 2
github.com/voi-oss/svc/logger.go:68.28,75.3 3 2
github.com/voi-oss/svc/logger.go:80.36,81.28 1 0
github.com/voi-oss/svc/logger.go:81.28,88.3 3 0
github.com/voi-oss/svc/logger.go:93.52,94.28 1 0
github.com/voi-oss/svc/logger.go:94.28,103.3 4 0
github.com/voi-oss/svc/logger.go:108.56,109.28 1 0
github.com/voi-oss/svc/logger.go:109.28,116.3 3 0
github.com/voi-oss/svc/logger.go:119.75,121.16 2 2
github.com/voi-oss/svc/logger.go:124.2,125.16 2 2
github.com/voi-oss/svc/logger.go:129.2,134.12 5 2
github.com/voi-oss/svc/logger.go:121.16,123.3 1 0
github.com/voi-oss/svc/logger.go:67.55,68.28 1 4
github.com/voi-oss/svc/logger.go:68.28,76.3 4 4
github.com/voi-oss/svc/logger.go:81.54,82.28 1 0
github.com/voi-oss/svc/logger.go:82.28,90.3 4 0
github.com/voi-oss/svc/logger.go:95.72,96.28 1 0
github.com/voi-oss/svc/logger.go:96.28,106.3 5 0
github.com/voi-oss/svc/logger.go:111.76,112.28 1 0
github.com/voi-oss/svc/logger.go:112.28,120.3 4 0
github.com/voi-oss/svc/logger.go:123.75,125.16 2 5
github.com/voi-oss/svc/logger.go:128.2,129.16 2 5
github.com/voi-oss/svc/logger.go:133.2,138.12 5 5
github.com/voi-oss/svc/logger.go:125.16,127.3 1 0
github.com/voi-oss/svc/logger.go:129.16,131.3 1 0
github.com/voi-oss/svc/options.go:19.56,20.28 1 0
github.com/voi-oss/svc/options.go:20.28,24.3 2 0
github.com/voi-oss/svc/options.go:28.57,29.28 1 0
Expand Down Expand Up @@ -61,61 +66,61 @@ github.com/voi-oss/svc/options.go:136.41,138.7 1 0
github.com/voi-oss/svc/options.go:141.21,144.19 3 0
github.com/voi-oss/svc/options.go:148.5,150.22 3 0
github.com/voi-oss/svc/options.go:144.19,147.6 2 0
github.com/voi-oss/svc/svc.go:50.62,66.51 2 2
github.com/voi-oss/svc/svc.go:70.2,74.25 3 2
github.com/voi-oss/svc/svc.go:80.2,80.15 1 2
github.com/voi-oss/svc/svc.go:66.51,68.3 1 0
github.com/voi-oss/svc/svc.go:74.25,75.30 1 0
github.com/voi-oss/svc/svc.go:75.30,77.4 1 0
github.com/voi-oss/svc/svc.go:85.48,86.42 1 4
github.com/voi-oss/svc/svc.go:89.2,89.32 1 4
github.com/voi-oss/svc/svc.go:92.2,92.31 1 4
github.com/voi-oss/svc/svc.go:98.2,99.21 2 4
github.com/voi-oss/svc/svc.go:86.42,88.3 1 0
github.com/voi-oss/svc/svc.go:89.32,91.3 1 4
github.com/voi-oss/svc/svc.go:92.31,94.3 1 0
github.com/voi-oss/svc/svc.go:94.8,96.3 1 4
github.com/voi-oss/svc/svc.go:102.57,104.2 1 0
github.com/voi-oss/svc/svc.go:108.21,111.15 2 2
github.com/voi-oss/svc/svc.go:120.2,120.38 1 2
github.com/voi-oss/svc/svc.go:130.2,132.33 3 2
github.com/voi-oss/svc/svc.go:143.2,145.9 2 2
github.com/voi-oss/svc/svc.go:111.15,117.3 5 2
github.com/voi-oss/svc/svc.go:120.38,123.54 3 4
github.com/voi-oss/svc/svc.go:127.3,127.60 1 4
github.com/voi-oss/svc/svc.go:123.54,126.4 2 0
github.com/voi-oss/svc/svc.go:132.33,134.34 2 4
github.com/voi-oss/svc/svc.go:134.34,136.34 2 4
github.com/voi-oss/svc/svc.go:136.34,139.5 2 0
github.com/voi-oss/svc/svc.go:146.21,147.60 1 0
github.com/voi-oss/svc/svc.go:148.26,149.69 1 1
github.com/voi-oss/svc/svc.go:150.30,151.45 1 1
github.com/voi-oss/svc/svc.go:160.26,162.2 1 1
github.com/voi-oss/svc/svc.go:165.39,166.16 1 0
github.com/voi-oss/svc/svc.go:173.2,173.10 1 0
github.com/voi-oss/svc/svc.go:166.16,167.34 1 0
github.com/voi-oss/svc/svc.go:170.3,171.13 2 0
github.com/voi-oss/svc/svc.go:167.34,168.14 1 0
github.com/voi-oss/svc/svc.go:177.36,179.2 1 0
github.com/voi-oss/svc/svc.go:181.34,187.12 4 2
github.com/voi-oss/svc/svc.go:202.2,203.41 2 2
github.com/voi-oss/svc/svc.go:187.12,190.45 3 2
github.com/voi-oss/svc/svc.go:190.45,191.28 1 4
github.com/voi-oss/svc/svc.go:191.28,193.41 2 4
github.com/voi-oss/svc/svc.go:198.5,198.67 1 4
github.com/voi-oss/svc/svc.go:193.41,197.6 1 0
github.com/voi-oss/svc/svc.go:206.60,207.9 1 2
github.com/voi-oss/svc/svc.go:208.29,208.29 0 2
github.com/voi-oss/svc/svc.go:209.23,209.23 0 0
github.com/voi-oss/svc/svc.go:213.58,215.12 2 4
github.com/voi-oss/svc/svc.go:219.2,219.10 1 4
github.com/voi-oss/svc/svc.go:215.12,218.3 2 4
github.com/voi-oss/svc/svc.go:222.81,224.30 2 4
github.com/voi-oss/svc/svc.go:224.30,225.31 1 0
github.com/voi-oss/svc/svc.go:225.31,229.4 2 0
github.com/voi-oss/svc/svc.go:229.9,231.4 1 0
github.com/voi-oss/svc/config.go:10.44,11.42 1 0
github.com/voi-oss/svc/config.go:14.2,14.55 1 0
github.com/voi-oss/svc/config.go:17.2,17.12 1 0
github.com/voi-oss/svc/config.go:11.42,13.3 1 0
github.com/voi-oss/svc/config.go:14.55,16.3 1 0
github.com/voi-oss/svc/svc.go:54.62,70.51 2 4
github.com/voi-oss/svc/svc.go:74.2,78.25 3 4
github.com/voi-oss/svc/svc.go:84.2,84.15 1 4
github.com/voi-oss/svc/svc.go:70.51,72.3 1 0
github.com/voi-oss/svc/svc.go:78.25,79.30 1 0
github.com/voi-oss/svc/svc.go:79.30,81.4 1 0
github.com/voi-oss/svc/svc.go:89.48,90.42 1 5
github.com/voi-oss/svc/svc.go:93.2,93.32 1 5
github.com/voi-oss/svc/svc.go:96.2,96.31 1 5
github.com/voi-oss/svc/svc.go:102.2,103.21 2 5
github.com/voi-oss/svc/svc.go:90.42,92.3 1 0
github.com/voi-oss/svc/svc.go:93.32,95.3 1 5
github.com/voi-oss/svc/svc.go:96.31,98.3 1 0
github.com/voi-oss/svc/svc.go:98.8,100.3 1 5
github.com/voi-oss/svc/svc.go:106.57,109.2 2 0
github.com/voi-oss/svc/svc.go:113.21,116.15 2 3
github.com/voi-oss/svc/svc.go:125.2,125.38 1 3
github.com/voi-oss/svc/svc.go:135.2,137.33 3 3
github.com/voi-oss/svc/svc.go:148.2,150.9 2 3
github.com/voi-oss/svc/svc.go:116.15,122.3 5 3
github.com/voi-oss/svc/svc.go:125.38,128.54 3 5
github.com/voi-oss/svc/svc.go:132.3,132.60 1 5
github.com/voi-oss/svc/svc.go:128.54,131.4 2 0
github.com/voi-oss/svc/svc.go:137.33,139.34 2 5
github.com/voi-oss/svc/svc.go:139.34,141.34 2 5
github.com/voi-oss/svc/svc.go:141.34,144.5 2 1
github.com/voi-oss/svc/svc.go:151.21,152.40 1 1
github.com/voi-oss/svc/svc.go:155.3,155.59 1 1
github.com/voi-oss/svc/svc.go:156.26,157.69 1 1
github.com/voi-oss/svc/svc.go:158.30,159.45 1 1
github.com/voi-oss/svc/svc.go:152.40,154.4 1 0
github.com/voi-oss/svc/svc.go:168.26,170.2 1 1
github.com/voi-oss/svc/svc.go:173.39,174.16 1 0
github.com/voi-oss/svc/svc.go:181.2,181.10 1 0
github.com/voi-oss/svc/svc.go:174.16,175.34 1 0
github.com/voi-oss/svc/svc.go:178.3,179.13 2 0
github.com/voi-oss/svc/svc.go:175.34,176.14 1 0
github.com/voi-oss/svc/svc.go:185.36,187.2 1 0
github.com/voi-oss/svc/svc.go:189.34,195.12 4 3
github.com/voi-oss/svc/svc.go:210.2,211.41 2 3
github.com/voi-oss/svc/svc.go:195.12,198.45 3 3
github.com/voi-oss/svc/svc.go:198.45,199.28 1 5
github.com/voi-oss/svc/svc.go:199.28,201.41 2 5
github.com/voi-oss/svc/svc.go:206.5,206.67 1 5
github.com/voi-oss/svc/svc.go:201.41,205.6 1 0
github.com/voi-oss/svc/svc.go:214.60,215.9 1 3
github.com/voi-oss/svc/svc.go:216.29,216.29 0 3
github.com/voi-oss/svc/svc.go:217.23,217.23 0 0
github.com/voi-oss/svc/svc.go:221.58,223.12 2 6
github.com/voi-oss/svc/svc.go:227.2,227.10 1 6
github.com/voi-oss/svc/svc.go:223.12,226.3 2 6
github.com/voi-oss/svc/svc.go:230.81,232.30 2 5
github.com/voi-oss/svc/svc.go:232.30,233.31 1 0
github.com/voi-oss/svc/svc.go:233.31,237.4 2 0
github.com/voi-oss/svc/svc.go:237.9,239.4 1 0
github.com/voi-oss/svc/svc.go:243.70,244.25 1 0
github.com/voi-oss/svc/svc.go:247.2,247.30 1 0
github.com/voi-oss/svc/svc.go:244.25,246.3 1 0
16 changes: 10 additions & 6 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ func (s *SVC) newLogger(level zapcore.Level, encoder zapcore.Encoder) (*zap.Logg
atom := zap.NewAtomicLevel()
atom.SetLevel(level)

zapOpts := append(s.zapOpts, zap.ErrorOutput(zapcore.Lock(os.Stderr)), zap.AddCaller())
s.zapOpts = append(s.zapOpts, zap.ErrorOutput(zapcore.Lock(os.Stderr)), zap.AddCaller())

logger := zap.New(zapcore.NewSamplerWithOptions(zapcore.NewCore(
encoder,
zapcore.Lock(os.Stdout),
atom,
), time.Second, 100, 10),
zapOpts...,
s.zapOpts...,
)

return logger, atom
Expand Down Expand Up @@ -64,8 +64,9 @@ func WithLogger(logger *zap.Logger, atom zap.AtomicLevel) Option {

// WithDevelopmentLogger is an option that uses a zap Logger with
// configurations set meant to be used for development.
func WithDevelopmentLogger() Option {
func WithDevelopmentLogger(opts ...zap.Option) Option {
return func(s *SVC) error {
s.zapOpts = append(s.zapOpts, opts...)
logger, atom := s.newLogger(
zapcore.DebugLevel,
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
Expand All @@ -77,8 +78,9 @@ func WithDevelopmentLogger() Option {

// WithProductionLogger is an option that uses a zap Logger with configurations
// set meant to be used for production.
func WithProductionLogger() Option {
func WithProductionLogger(opts ...zap.Option) Option {
return func(s *SVC) error {
s.zapOpts = append(s.zapOpts, opts...)
logger, atom := s.newLogger(
zapcore.InfoLevel,
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
Expand All @@ -90,10 +92,11 @@ func WithProductionLogger() Option {

// WithConsoleLogger is an option that uses a zap Logger with configurations
// set meant to be used for debugging in the console.
func WithConsoleLogger(level zapcore.Level) Option {
func WithConsoleLogger(level zapcore.Level, opts ...zap.Option) Option {
return func(s *SVC) error {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = zapcore.RFC3339TimeEncoder
s.zapOpts = append(s.zapOpts, opts...)

logger, atom := s.newLogger(
level,
Expand All @@ -105,8 +108,9 @@ func WithConsoleLogger(level zapcore.Level) Option {

// WithStackdriverLogger is an option that uses a zap Logger with configurations
// set meant to be used for production and is compliant with the GCP/Stackdriver format.
func WithStackdriverLogger(level zapcore.Level) Option {
func WithStackdriverLogger(level zapcore.Level, opts ...zap.Option) Option {
return func(s *SVC) error {
s.zapOpts = append(s.zapOpts, opts...)
logger, atom := s.newLogger(
level,
zapcore.NewJSONEncoder(zapdriver.NewProductionEncoderConfig()),
Expand Down
61 changes: 61 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package svc

import (
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestNewLogger(t *testing.T) {
tests := []struct {
name string
serviceOption Option
}{
{
name: "console logger",
serviceOption: WithConsoleLogger(zap.InfoLevel),
},
{
name: "console logger with options",
serviceOption: WithConsoleLogger(zap.DebugLevel, zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return core
})),
},
{
name: "development logger",
serviceOption: WithDevelopmentLogger(),
},
{
name: "development logger with options",
serviceOption: WithDevelopmentLogger(zap.Development()),
},
{
name: "production logger",
serviceOption: WithProductionLogger(),
},
{
name: "production logger with options",
serviceOption: WithProductionLogger(zap.WithCaller(true)),
},
{
name: "stackdriver logger",
serviceOption: WithStackdriverLogger(zap.WarnLevel),
},
{
name: "stackdriver logger with options",
serviceOption: WithStackdriverLogger(zap.WarnLevel, zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return core
}), zap.AddCaller()),
},
}

for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
_, err := New("dummy-name", "dummy-version", tc.serviceOption)
require.NoError(t, err)
})
}
}

0 comments on commit d51db3b

Please sign in to comment.