Skip to content

Commit

Permalink
Add control for zap options (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored May 13, 2021
1 parent 815fd23 commit d0cc0f5
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 16 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: bench
on:
pull_request:
Expand Down Expand Up @@ -62,14 +63,13 @@ jobs:
uses: actions/checkout@v2
with:
ref: ${{ (github.event.pull_request.base.sha != '' ) && github.event.pull_request.base.sha || github.event.inputs.old }}
path: __base
path: zapctxd
- name: Run base benchmark
if: env.RUN_BASE_BENCHMARK == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '')
run: |
export REF_NAME=master
cd __base
BENCH_COUNT=5 make bench-run bench-stat
cp bench-master.txt ../bench-master.txt
cd zapctxd
make | grep bench-run && (BENCH_COUNT=5 make bench-run bench-stat && cp bench-master.txt ../bench-master.txt) || echo "No benchmarks in base"
- name: Benchmark
id: bench
run: |
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/cloc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This script is provided by github.com/bool64/dev.
name: cloc
on:
pull_request:
jobs:
cloc:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: pr
- name: Checkout base code
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Count Lines Of Code
id: loc
run: |
curl -OL https://github.com/vearutop/builds/releases/download/sccdiff-v0/sccdiff && chmod +x sccdiff
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "::set-output name=diff::$OUTPUT"
- name: Comment Code Lines
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: LOC
message: |
### Lines Of Code
${{ steps.loc.outputs.diff }}
1 change: 1 addition & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: lint
on:
push:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: test-unit
on:
push:
Expand Down Expand Up @@ -51,8 +52,7 @@ jobs:
if: matrix.go-version == '1.16.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
run: |
cd zapctxd
make test-unit
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
- name: Test
id: test
run: |
Expand Down
6 changes: 3 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ linters:
- paralleltest
- forbidigo
- exhaustivestruct
- interfacer
- interfacer # deprecated
- forcetypeassert
- scopelint
- ifshort
- scopelint # deprecated
- ifshort # too many false positives

issues:
exclude-use-default: false
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/bool64/zapctxd
go 1.13

require (
github.com/bool64/ctxd v0.1.4
github.com/bool64/dev v0.1.26
github.com/bool64/ctxd v0.1.5
github.com/bool64/dev v0.1.30
github.com/stretchr/testify v1.7.0
github.com/swaggest/assertjson v1.6.4
go.uber.org/zap v1.16.0
Expand Down
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/bool64/ctxd v0.1.4 h1:UxL3FCpnLP/h6cZFKBGxAL9TXMd45l8QZQdG35D7BlU=
github.com/bool64/ctxd v0.1.4/go.mod h1:vbCBsEfD4TGXGTPEEQwjB4M5Ny2MC8r+9N2JSP3yJPY=
github.com/bool64/ctxd v0.1.5 h1:ilZo2AgQ+NG9NyusoYEdcMcGgleAFDnrju+5KMxwUbk=
github.com/bool64/ctxd v0.1.5/go.mod h1:+rjDVFNOJeO+xlvMqQfG0p53CzuRB7FhPSo5nWSkpQ0=
github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.26 h1:9RppeANjTKsF0ZEROkgh0z8qKTvpNeVmKnz0uuCAkS4=
github.com/bool64/dev v0.1.26/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.28/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.30 h1:eVn3uMVUj8PoVydfs3D2TaVtbiJVwv3WpEgPnpC3vEk=
github.com/bool64/dev v0.1.30/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/shared v0.1.3 h1:gj7XZPYa1flQsCg3q9AIju+W2A1jaexK0fdFu2XtaG0=
github.com/bool64/shared v0.1.3/go.mod h1:RF1p1Oi29ofgOvinBpetbF5mceOUP3kpMkvLbWOmtm0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
4 changes: 3 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ type Config struct {
DevMode bool `split_words:"true"`
FieldNames ctxd.FieldNames `split_words:"true"`
Output io.Writer
ZapOptions []zap.Option

// StripTime disables time variance in logger.
StripTime bool
}

// New creates contextualized logger with zap backend.
func New(cfg Config) Logger {
func New(cfg Config, options ...zap.Option) Logger {
level := zap.InfoLevel

if cfg.Level != 0 {
Expand All @@ -62,6 +63,7 @@ func New(cfg Config) Logger {
l := Logger{
AtomicLevel: zap.NewAtomicLevelAt(level),
out: out,
options: append(cfg.ZapOptions, options...),
}

if cfg.DevMode {
Expand Down
15 changes: 15 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,18 @@ func TestLogger_SkipCaller(t *testing.T) {
<stripped> INFO zapctxd/logger_test.go:230 world {"k": "v"}
`, w.String())
}

func TestNew_zapOptions(t *testing.T) {
w := bytes.NewBuffer(nil)

c := zapctxd.New(zapctxd.Config{
Level: zap.InfoLevel,
StripTime: true,
Output: w,
ZapOptions: []zap.Option{zap.Fields(zap.String("config", "foo"))},
}, zap.Fields(zap.String("constructor", "bar")))

c.Info(context.Background(), "hello", "k", "v")
assert.Equal(t, `{"level":"info","time":"<stripped>","msg":"hello","config":"foo","constructor":"bar","k":"v"}
`, w.String())
}

0 comments on commit d0cc0f5

Please sign in to comment.