A wrapper library based on go.uber.org/zap
.
pingcap / log Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
As https://github.com/pingcap/log/blob/master/log.go#L53, if we don't customize cfg.File
, normal log will be written into stdout, which pollutes the stdout of applications in the downstream.
I see this library distinguishes normal log and error log, and they are designed to be written into different places. So I propose two solutions:
... as v1
See here:
Line 127 in 8f1e84a
// Sync flushes any buffered log entries.
func Sync() error {
err := L().Sync()
if err != nil {
return err
}
return S().Sync()
}
这里没有必要调用S().Sync()
,因为 *SugaredLogger.Sync()
的实现如下:
There is no need to call S().Sync()
because *SugaredLogger.Sync()
which is implemented as:
type SugaredLogger struct {
base *Logger
}
// Sync flushes any buffered log entries.
func (s *SugaredLogger) Sync() error {
return s.base.Sync()
}
因此,这和已经调用的L().Sync()
等价,SugaredLogger
没有独立的Sync()
方法,它依赖于Logger
的Sync()
。
It's equivalent to L().Sync()
, which has been called.
The ReplaceGlobals
sets a new global logger. However it doesn't provide ways to recover back the original logger. This makes it hard to be used in scenarios like tests, where a "SetGlobal" - "RevertGlobal" is a common pattern.
Lines 66 to 70 in ca23291
log.With
shouldn't add caller skip, since it just returns a new logger without calling any function to print logs.Use --log-format=xml
.
Either an XML formatted log or an error saying only text and json are supported
[dvaneeden@dve-carbon test_db]$ tiup br backup full -s local:///tmp/tidb_backup_003 --log-format=xml
Starting component `br`: /home/dvaneeden/.tiup/components/br/v5.0.1/br backup full -s local:///tmp/tidb_backup_003 --log-format=xml
Detail BR log in /tmp/br.log.2021-05-04T16.48.04+0200
panic: unsupport log format: xml
goroutine 1 [running]:
github.com/pingcap/log.NewTextEncoder(0xc0008383c0, 0xc0008441e0, 0x4)
github.com/pingcap/[email protected]/zap_text_encoder.go:168 +0x3fb
github.com/pingcap/log.newZapTextEncoder(...)
github.com/pingcap/[email protected]/config.go:78
github.com/pingcap/log.InitLoggerWithWriteSyncer(0xc0008383c0, 0x3a4b860, 0xc00084e0b0, 0x0, 0x0, 0x0, 0xc0006bba30, 0x1, 0x1, 0x37)
github.com/pingcap/[email protected]/log.go:63 +0xc0
github.com/pingcap/log.InitLogger(0xc0008383c0, 0x0, 0x0, 0x0, 0x1, 0xc0007b3380, 0x37, 0x0)
github.com/pingcap/[email protected]/log.go:53 +0xa7
main.Init.func1()
github.com/pingcap/br@/cmd/br/cmd.go:115 +0x260
sync.(*Once).doSlow(0x57d9a78, 0xc0006bbb80)
sync/once.go:66 +0xe3
sync.(*Once).Do(...)
sync/once.go:57
main.Init(0xc000840dc0, 0x0, 0x0)
github.com/pingcap/br@/cmd/br/cmd.go:89 +0xbc
main.NewBackupCommand.func1(0xc000840dc0, 0xc00084a2d0, 0x0, 0x3, 0x0, 0x0)
github.com/pingcap/br@/cmd/br/backup.go:74 +0x2f
github.com/spf13/cobra.(*Command).execute(0xc000840dc0, 0xc0000f2150, 0x3, 0x3, 0xc000840dc0, 0xc0000f2150)
github.com/spf13/[email protected]/command.go:821 +0x56b
github.com/spf13/cobra.(*Command).ExecuteC(0xc000205340, 0xc0006bbe38, 0x3, 0x3)
github.com/spf13/[email protected]/command.go:950 +0x349
github.com/spf13/cobra.(*Command).Execute(...)
github.com/spf13/[email protected]/command.go:887
main.main()
github.com/pingcap/br@/cmd/br/main.go:56 +0x35c
Error: run `/home/dvaneeden/.tiup/components/br/v5.0.1/br` (wd:/home/dvaneeden/.tiup/data/SWSD09N) failed: exit status 2
[dvaneeden@dve-carbon test_db]$ tiup br --version
Starting component `br`: /home/dvaneeden/.tiup/components/br/v5.0.1/br --version
Release Version: v5.0.1
Git Commit Hash: b3bd5495369244cfb1d3f2c49763a7fdb55d4ecd
Git Branch: heads/refs/tags/v5.0.1
Go Version: go1.13
UTC Build Time: 2021-04-23 06:02:57
Race Enabled: false
Improvements:
--log-format
--help
outputThis is a mirror issue from pingcap/tidb#26956 to bring the benefit to pingcap/log.
#29 moved zap.AddCallerSkip(1)
to the L()
function. However, the global logger may be replaced after first call to L()
.
As a result, some complements that depend on this log library print wrong file name and line number.
tiflow-master
tidb-server
func (s *testLogSuite) TestContextLogger(c *C) {
l, _, err := zaplog.InitLogger(&zaplog.Config{DisableStacktrace:true})
if err != nil {
panic(err)
}
l2 := l.With(zap.String("c", "c1"))
l2.Error("", zap.String("k", "v"), zap.String("k2", "v2"))
}
[2019/03/04 23:00:05.943 +08:00] [ERROR] [log_test.go:209] [c=c1] [k=v] [k2=v2]
[2019/03/04 23:00:05.943 +08:00] [ERROR] [log_test.go:209],c=c1 [k=v] [k2=v2]
the function WithOptions
will use clone, it is too expensive
some related code:
Lines 27 to 29 in 1b12a1f
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.