Skip to content

Commit d0cc0f5

Browse files
authored
Add control for zap options (#4)
1 parent 815fd23 commit d0cc0f5

File tree

9 files changed

+71
-16
lines changed

9 files changed

+71
-16
lines changed

.github/workflows/bench.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# This script is provided by github.com/bool64/dev.
12
name: bench
23
on:
34
pull_request:
@@ -62,14 +63,13 @@ jobs:
6263
uses: actions/checkout@v2
6364
with:
6465
ref: ${{ (github.event.pull_request.base.sha != '' ) && github.event.pull_request.base.sha || github.event.inputs.old }}
65-
path: __base
66+
path: zapctxd
6667
- name: Run base benchmark
6768
if: env.RUN_BASE_BENCHMARK == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '')
6869
run: |
6970
export REF_NAME=master
70-
cd __base
71-
BENCH_COUNT=5 make bench-run bench-stat
72-
cp bench-master.txt ../bench-master.txt
71+
cd zapctxd
72+
make | grep bench-run && (BENCH_COUNT=5 make bench-run bench-stat && cp bench-master.txt ../bench-master.txt) || echo "No benchmarks in base"
7373
- name: Benchmark
7474
id: bench
7575
run: |

.github/workflows/cloc.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This script is provided by github.com/bool64/dev.
2+
name: cloc
3+
on:
4+
pull_request:
5+
jobs:
6+
cloc:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v2
11+
with:
12+
path: pr
13+
- name: Checkout base code
14+
uses: actions/checkout@v2
15+
with:
16+
ref: ${{ github.event.pull_request.base.sha }}
17+
path: base
18+
- name: Count Lines Of Code
19+
id: loc
20+
run: |
21+
curl -OL https://github.com/vearutop/builds/releases/download/sccdiff-v0/sccdiff && chmod +x sccdiff
22+
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
23+
OUTPUT="${OUTPUT//'%'/'%25'}"
24+
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
25+
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
26+
echo "::set-output name=diff::$OUTPUT"
27+
28+
- name: Comment Code Lines
29+
uses: marocchino/sticky-pull-request-comment@v2
30+
with:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
header: LOC
33+
message: |
34+
### Lines Of Code
35+
36+
${{ steps.loc.outputs.diff }}

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# This script is provided by github.com/bool64/dev.
12
name: lint
23
on:
34
push:

.github/workflows/test-unit.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# This script is provided by github.com/bool64/dev.
12
name: test-unit
23
on:
34
push:
@@ -51,8 +52,7 @@ jobs:
5152
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 != ''
5253
run: |
5354
cd zapctxd
54-
make test-unit
55-
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
55+
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"
5656
- name: Test
5757
id: test
5858
run: |

.golangci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ linters:
2828
- paralleltest
2929
- forbidigo
3030
- exhaustivestruct
31-
- interfacer
31+
- interfacer # deprecated
3232
- forcetypeassert
33-
- scopelint
34-
- ifshort
33+
- scopelint # deprecated
34+
- ifshort # too many false positives
3535

3636
issues:
3737
exclude-use-default: false

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/bool64/zapctxd
33
go 1.13
44

55
require (
6-
github.com/bool64/ctxd v0.1.4
7-
github.com/bool64/dev v0.1.26
6+
github.com/bool64/ctxd v0.1.5
7+
github.com/bool64/dev v0.1.30
88
github.com/stretchr/testify v1.7.0
99
github.com/swaggest/assertjson v1.6.4
1010
go.uber.org/zap v1.16.0

go.sum

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3-
github.com/bool64/ctxd v0.1.4 h1:UxL3FCpnLP/h6cZFKBGxAL9TXMd45l8QZQdG35D7BlU=
4-
github.com/bool64/ctxd v0.1.4/go.mod h1:vbCBsEfD4TGXGTPEEQwjB4M5Ny2MC8r+9N2JSP3yJPY=
3+
github.com/bool64/ctxd v0.1.5 h1:ilZo2AgQ+NG9NyusoYEdcMcGgleAFDnrju+5KMxwUbk=
4+
github.com/bool64/ctxd v0.1.5/go.mod h1:+rjDVFNOJeO+xlvMqQfG0p53CzuRB7FhPSo5nWSkpQ0=
55
github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
6-
github.com/bool64/dev v0.1.26 h1:9RppeANjTKsF0ZEROkgh0z8qKTvpNeVmKnz0uuCAkS4=
7-
github.com/bool64/dev v0.1.26/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
6+
github.com/bool64/dev v0.1.28/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
7+
github.com/bool64/dev v0.1.30 h1:eVn3uMVUj8PoVydfs3D2TaVtbiJVwv3WpEgPnpC3vEk=
8+
github.com/bool64/dev v0.1.30/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
89
github.com/bool64/shared v0.1.3 h1:gj7XZPYa1flQsCg3q9AIju+W2A1jaexK0fdFu2XtaG0=
910
github.com/bool64/shared v0.1.3/go.mod h1:RF1p1Oi29ofgOvinBpetbF5mceOUP3kpMkvLbWOmtm0=
1011
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

logger.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ type Config struct {
3131
DevMode bool `split_words:"true"`
3232
FieldNames ctxd.FieldNames `split_words:"true"`
3333
Output io.Writer
34+
ZapOptions []zap.Option
3435

3536
// StripTime disables time variance in logger.
3637
StripTime bool
3738
}
3839

3940
// New creates contextualized logger with zap backend.
40-
func New(cfg Config) Logger {
41+
func New(cfg Config, options ...zap.Option) Logger {
4142
level := zap.InfoLevel
4243

4344
if cfg.Level != 0 {
@@ -62,6 +63,7 @@ func New(cfg Config) Logger {
6263
l := Logger{
6364
AtomicLevel: zap.NewAtomicLevelAt(level),
6465
out: out,
66+
options: append(cfg.ZapOptions, options...),
6567
}
6668

6769
if cfg.DevMode {

logger_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,18 @@ func TestLogger_SkipCaller(t *testing.T) {
233233
<stripped> INFO zapctxd/logger_test.go:230 world {"k": "v"}
234234
`, w.String())
235235
}
236+
237+
func TestNew_zapOptions(t *testing.T) {
238+
w := bytes.NewBuffer(nil)
239+
240+
c := zapctxd.New(zapctxd.Config{
241+
Level: zap.InfoLevel,
242+
StripTime: true,
243+
Output: w,
244+
ZapOptions: []zap.Option{zap.Fields(zap.String("config", "foo"))},
245+
}, zap.Fields(zap.String("constructor", "bar")))
246+
247+
c.Info(context.Background(), "hello", "k", "v")
248+
assert.Equal(t, `{"level":"info","time":"<stripped>","msg":"hello","config":"foo","constructor":"bar","k":"v"}
249+
`, w.String())
250+
}

0 commit comments

Comments
 (0)