Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move addFields to ObjectEncoder to let logger#With be extendible #685

Closed
wants to merge 1 commit into from
Closed

Move addFields to ObjectEncoder to let logger#With be extendible #685

wants to merge 1 commit into from

Conversation

lysu
Copy link

@lysu lysu commented Mar 4, 2019

  1. What did you do?

we write a TextEncoder to let log be formated as

[2019/03/04 23:00:05.943 +08:00] [ERROR] [log_test.go:209] [common=comval] [k=v]

but found that With method cannot add [ ]

newLogger := logger.With(zap.String("c", "c1"))
newLogger.Error("", zap.String("k", "v"))

will got

[2019/03/04 23:00:05.943 +08:00] [ERROR] [log_test.go:209],c=c1 [k=v]
  1. What's the problem

found that addFields at

https://github.com/uber-go/zap/blob/master/zapcore/field.go#L197

maybe better move to objectEncoder at

https://github.com/uber-go/zap/blob/master/zapcore/encoder.go#L247

so transfer the responsibility of how to print multiple field to objectEncoder, so other people can customize it, eg. we can add [ ] sourrounding each field even them add via logger#With

  1. What is changed

move addFields to ObjectEncoder and let current implements it.

ref pingcap/log#3, pingcap/tidb#9548

@CLAassistant
Copy link

CLAassistant commented Mar 4, 2019

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Mar 4, 2019

Codecov Report

Merging #685 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #685      +/-   ##
==========================================
+ Coverage   97.38%   97.38%   +<.01%     
==========================================
  Files          40       40              
  Lines        2102     2105       +3     
==========================================
+ Hits         2047     2050       +3     
  Misses         47       47              
  Partials        8        8
Impacted Files Coverage Δ
zapcore/field.go 100% <ø> (ø) ⬆️
zapcore/encoder.go 83.13% <ø> (ø) ⬆️
zapcore/console_encoder.go 96.49% <100%> (ø) ⬆️
zapcore/core.go 94.59% <100%> (ø) ⬆️
zapcore/memory_encoder.go 98.71% <100%> (+0.05%) ⬆️
zapcore/json_encoder.go 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d2a364d...c7d6dba. Read the comment docs.

@lysu
Copy link
Author

lysu commented Mar 5, 2019

@akshayjshah PTAL thx~

@prashantv
Copy link
Collaborator

prashantv commented Mar 5, 2019

Thanks for the contribution @lysu.

Unfortunately this change is not backwards compatible, since you're modifying the interface, so we won't be able to accept this change in zap 1.x

@lysu
Copy link
Author

lysu commented Mar 5, 2019

@prashantv we will take another temp way to solve this problem in current and wait for the 2.x, thx :D

@prashantv prashantv closed this Mar 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants