From 39bad64fb9214dfb1ca14cecc37efc1e9a56a593 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 15 Feb 2019 16:43:47 +0800 Subject: [PATCH] *: start replacing logger with zap logger (#9279) --- cmd/benchdb/main.go | 4 +- cmd/benchfilesort/main.go | 4 +- cmd/explaintest/main.go | 4 +- config/config.go | 17 +--- ddl/ddl_test.go | 5 +- distsql/request_builder_test.go | 4 +- executor/executor_test.go | 4 +- go.mod | 10 +-- go.sum | 32 ++++--- owner/fail_test.go | 5 +- server/http_handler.go | 8 ++ server/http_handler_test.go | 4 + server/server_test.go | 4 +- session/tidb_test.go | 4 +- sessionctx/binloginfo/binloginfo_test.go | 4 +- store/store_test.go | 4 +- tidb-server/main.go | 36 +++++--- util/logutil/log.go | 102 ++++++++++++++++++----- util/logutil/log_test.go | 56 ++++++++++++- util/memory/tracker_test.go | 4 +- 20 files changed, 211 insertions(+), 104 deletions(-) diff --git a/cmd/benchdb/main.go b/cmd/benchdb/main.go index 54dacd9bcfbc5..d5d4a98d45c7c 100644 --- a/cmd/benchdb/main.go +++ b/cmd/benchdb/main.go @@ -54,9 +54,7 @@ var ( func main() { flag.Parse() flag.PrintDefaults() - err := logutil.InitLogger(&logutil.LogConfig{ - Level: *logLevel, - }) + err := logutil.InitLogger(logutil.NewLogConfig(*logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) terror.MustNil(err) err = session.RegisterStore("tikv", tikv.Driver{}) terror.MustNil(err) diff --git a/cmd/benchfilesort/main.go b/cmd/benchfilesort/main.go index 08c16353a58fd..3c6f30827861d 100644 --- a/cmd/benchfilesort/main.go +++ b/cmd/benchfilesort/main.go @@ -382,9 +382,7 @@ func driveRunCmd() { } func init() { - err := logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + err := logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) terror.MustNil(err) cwd, err1 := os.Getwd() terror.MustNil(err1) diff --git a/cmd/explaintest/main.go b/cmd/explaintest/main.go index d3650f8a05eb1..921637fa92151 100644 --- a/cmd/explaintest/main.go +++ b/cmd/explaintest/main.go @@ -629,9 +629,7 @@ func openDBWithRetry(driverName, dataSourceName string) (mdb *sql.DB, err error) func main() { flag.Parse() - err := logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + err := logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) if err != nil { panic("init logger fail, " + err.Error()) } diff --git a/config/config.go b/config/config.go index 1d93690fd201c..506ca8c30df9f 100644 --- a/config/config.go +++ b/config/config.go @@ -271,12 +271,9 @@ var defaultConf = Config{ }, LowerCaseTableNames: 2, Log: Log{ - Level: "info", - Format: "text", - File: logutil.FileLogConfig{ - LogRotate: true, - MaxSize: logutil.DefaultLogMaxSize, - }, + Level: "info", + Format: "text", + File: logutil.NewFileLogConfig(true, logutil.DefaultLogMaxSize), SlowThreshold: logutil.DefaultSlowThreshold, ExpensiveThreshold: 10000, QueryLogMaxLen: logutil.DefaultQueryLogMaxLen, @@ -354,13 +351,7 @@ func (c *Config) Load(confFile string) error { // ToLogConfig converts *Log to *logutil.LogConfig. func (l *Log) ToLogConfig() *logutil.LogConfig { - return &logutil.LogConfig{ - Level: l.Level, - Format: l.Format, - DisableTimestamp: l.DisableTimestamp, - File: l.File, - SlowQueryFile: l.SlowQueryFile, - } + return logutil.NewLogConfig(l.Level, l.Format, l.SlowQueryFile, l.File, l.DisableTimestamp) } // ToTracingConfig converts *OpenTracing to *tracing.Configuration. diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index 80a1a57432267..7c43e3a6d4191 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -101,10 +101,7 @@ func (d *ddl) restartWorkers(ctx context.Context) { func TestT(t *testing.T) { CustomVerboseFlag = true logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - Format: "highlight", - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, "highlight", "", logutil.EmptyFileLogConfig, false)) TestingT(t) } diff --git a/distsql/request_builder_test.go b/distsql/request_builder_test.go index 640127f163594..b64dd63218892 100644 --- a/distsql/request_builder_test.go +++ b/distsql/request_builder_test.go @@ -37,9 +37,7 @@ var _ = Suite(&testSuite{}) func TestT(t *testing.T) { CustomVerboseFlag = true logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) TestingT(t) } diff --git a/executor/executor_test.go b/executor/executor_test.go index cd07756f560e3..79b3863867450 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -68,9 +68,7 @@ import ( func TestT(t *testing.T) { CustomVerboseFlag = true logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) TestingT(t) } diff --git a/go.mod b/go.mod index 3b24deaaab3e5..b4310496fbac7 100644 --- a/go.mod +++ b/go.mod @@ -43,30 +43,29 @@ require ( github.com/onsi/gomega v1.4.1 // indirect github.com/opentracing/basictracer-go v1.0.0 github.com/opentracing/opentracing-go v1.0.2 - github.com/pingcap/check v0.0.0-20171206051426-1c287c953996 + github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 github.com/pingcap/errors v0.11.0 github.com/pingcap/gofail v0.0.0-20181217135706-6a951c1e42c3 github.com/pingcap/goleveldb v0.0.0-20171020084629-8d44bfdf1030 github.com/pingcap/kvproto v0.0.0-20181109035735-8e3f33ac4929 + github.com/pingcap/log v0.0.0-20190214045112-b37da76f67a7 github.com/pingcap/parser v0.0.0-20190118033454-a52e5bde3bd2 github.com/pingcap/pd v2.1.0-rc.4+incompatible github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible github.com/pingcap/tipb v0.0.0-20180910045846-371b48b15d93 - github.com/pkg/errors v0.8.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v0.8.0 github.com/prometheus/client_model v0.0.0-20171117100541-99fa1f4be8e5 github.com/prometheus/common v0.0.0-20180426121432-d811d2e9bf89 // indirect github.com/prometheus/procfs v0.0.0-20180408092902-8b1c2da0d56d // indirect github.com/sirupsen/logrus v0.0.0-20170323161349-3bcb09397d6d github.com/spaolacci/murmur3 v0.0.0-20150829172844-0d12bf811670 - github.com/stretchr/testify v1.2.2 // indirect github.com/twinj/uuid v0.0.0-20150629100731-70cac2bcd273 github.com/uber/jaeger-client-go v2.8.0+incompatible github.com/uber/jaeger-lib v1.1.0 // indirect github.com/ugorji/go v1.1.1 // indirect github.com/unrolled/render v0.0.0-20171102162132-65450fb6b2d3 // indirect github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18 // indirect + go.uber.org/zap v1.9.1 golang.org/x/crypto v0.0.0-20180503215945-1f94bef427e3 // indirect golang.org/x/net v0.0.0-20180906233101-161cd47e91fd golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect @@ -76,7 +75,6 @@ require ( google.golang.org/genproto v0.0.0-20180427144745-86e600f69ee4 // indirect google.golang.org/grpc v1.12.0 gopkg.in/fsnotify.v1 v1.4.7 // indirect - gopkg.in/natefinch/lumberjack.v2 v2.0.0-20161104145732-dd45e6a67c53 + gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect - gopkg.in/yaml.v2 v2.2.1 // indirect ) diff --git a/go.sum b/go.sum index a16e06e013eb9..c4b41afc007f6 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,7 @@ github.com/cznic/mathutil v0.0.0-20160613104831-78ad7f262603 h1:hhR9hTi0ligs11Jj github.com/cznic/mathutil v0.0.0-20160613104831-78ad7f262603/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM= github.com/cznic/sortutil v0.0.0-20150617083342-4c7342852e65 h1:hxuZop6tSoOi0sxFzoGGYdRqNrPubyaIf9KoBG9tPiE= github.com/cznic/sortutil v0.0.0-20150617083342-4c7342852e65/go.mod h1:q2w6Bg5jeox1B+QkJ6Wp/+Vn0G/bo3f1uY7Fn3vivIQ= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= @@ -82,8 +83,8 @@ github.com/opentracing/basictracer-go v1.0.0 h1:YyUAhaEfjoWXclZVJ9sGoNct7j4TVk7l github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/pingcap/check v0.0.0-20171206051426-1c287c953996 h1:ZBdiJCMan6GSo/aPAM7gywcUKa0z58gczVrnG6TQnAQ= -github.com/pingcap/check v0.0.0-20171206051426-1c287c953996/go.mod h1:B1+S9LNcuMyLH/4HMTViQOJevkGiik3wW2AN9zb2fNQ= +github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 h1:USx2/E1bX46VG32FIw034Au6seQ2fY9NEILmNh/UlQg= +github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8/go.mod h1:B1+S9LNcuMyLH/4HMTViQOJevkGiik3wW2AN9zb2fNQ= github.com/pingcap/errors v0.11.0 h1:DCJQB8jrHbQ1VVlMFIrbj2ApScNNotVmkSNplu2yUt4= github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pingcap/gofail v0.0.0-20181217135706-6a951c1e42c3 h1:04yuCf5NMvLU8rB2m4Qs3rynH7EYpMno3lHkewIOdMo= @@ -92,6 +93,8 @@ github.com/pingcap/goleveldb v0.0.0-20171020084629-8d44bfdf1030 h1:XJLuW0lsP7vAt github.com/pingcap/goleveldb v0.0.0-20171020084629-8d44bfdf1030/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw= github.com/pingcap/kvproto v0.0.0-20181109035735-8e3f33ac4929 h1:NAq95+VGsS2G7SjzZ5LP9iUlCMNAs13QUzbNY3G90v8= github.com/pingcap/kvproto v0.0.0-20181109035735-8e3f33ac4929/go.mod h1:0gwbe1F2iBIjuQ9AH0DbQhL+Dpr5GofU8fgYyXk+ykk= +github.com/pingcap/log v0.0.0-20190214045112-b37da76f67a7 h1:kOHAMalwF69bJrtWrOdVaCSvZjLucrJhP4NQKIu6uM4= +github.com/pingcap/log v0.0.0-20190214045112-b37da76f67a7/go.mod h1:xsfkWVaFVV5B8e1K9seWfyJWFrIhbtUTAD8NV1Pq3+w= github.com/pingcap/parser v0.0.0-20190118033454-a52e5bde3bd2 h1:7YGx4hF6M0nlFJVZrLF3EbMRI+XOizL+9aB8Txe745U= github.com/pingcap/parser v0.0.0-20190118033454-a52e5bde3bd2/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= github.com/pingcap/pd v2.1.0-rc.4+incompatible h1:/buwGk04aHO5odk/+O8ZOXGs4qkUjYTJ2UpCJXna8NE= @@ -100,9 +103,11 @@ github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible/go.mod h1:XGdcy9+yqlDSEMTpOXnwf3hiTeqrV6MN/u1se9N8yIM= github.com/pingcap/tipb v0.0.0-20180910045846-371b48b15d93 h1:gI5bOzLMxjUq6ui+md/JnT4pYpkzrABJ/PeYORWiYYs= github.com/pingcap/tipb v0.0.0-20180910045846-371b48b15d93/go.mod h1:RtkHW8WbcNxj8lsbzjaILci01CtYnYbIkQhjyZWrWVI= -github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.8.0 h1:1921Yw9Gc3iSc4VQh3PIoOqgPCZS7G/4xQNVUp8Mda8= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -116,8 +121,9 @@ github.com/sirupsen/logrus v0.0.0-20170323161349-3bcb09397d6d h1:Am08dd2WIZhCQXE github.com/sirupsen/logrus v0.0.0-20170323161349-3bcb09397d6d/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/spaolacci/murmur3 v0.0.0-20150829172844-0d12bf811670 h1:hKP4ACPoBBCnBbhoiuJXiYlSDhAvC9s4lgzAPmtVdU0= github.com/spaolacci/murmur3 v0.0.0-20150829172844-0d12bf811670/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/twinj/uuid v0.0.0-20150629100731-70cac2bcd273 h1:YqFyfcgqxQqjpRr0SEG0Z555J/3kPqDL/xmRyeAaX/0= github.com/twinj/uuid v0.0.0-20150629100731-70cac2bcd273/go.mod h1:mMgcE1RHFUFqe5AfiwlINXisXfDGro23fWdPUfOMjRY= github.com/uber/jaeger-client-go v2.8.0+incompatible h1:7DGH8Hqk6PirD+GE+bvCf0cLnspLuae7N1NcwMeQcyg= @@ -130,6 +136,12 @@ github.com/unrolled/render v0.0.0-20171102162132-65450fb6b2d3 h1:ZsIlNwu/G0zbChI github.com/unrolled/render v0.0.0-20171102162132-65450fb6b2d3/go.mod h1:tu82oB5W2ykJRVioYsB+IQKcft7ryBr7w12qMBUPyXg= github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18 h1:MPPkRncZLN9Kh4MEFmbnK4h3BD7AUmskWv2+EeZJCCs= github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o= +go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20180503215945-1f94bef427e3 h1:+U/hI4i24Enhs+2BEMCN7wg16uT6entqa2mTg/PUKB8= golang.org/x/crypto v0.0.0-20180503215945-1f94bef427e3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= @@ -150,9 +162,9 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= -gopkg.in/natefinch/lumberjack.v2 v2.0.0-20161104145732-dd45e6a67c53 h1:Kjoe/521+yYTO0xv6VymPwLPVq3RES8170ZpJkrFE9Q= -gopkg.in/natefinch/lumberjack.v2 v2.0.0-20161104145732-dd45e6a67c53/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= +gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/owner/fail_test.go b/owner/fail_test.go index db1bdb6eace2b..a536e6fca7f80 100644 --- a/owner/fail_test.go +++ b/owner/fail_test.go @@ -33,10 +33,7 @@ import ( func TestT(t *testing.T) { CustomVerboseFlag = true logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - Format: "highlight", - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, "highlight", "", logutil.EmptyFileLogConfig, false)) TestingT(t) } diff --git a/server/http_handler.go b/server/http_handler.go index 6aeead35c5570..7121098e0018f 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -49,6 +49,7 @@ import ( "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/codec" + logutil "github.com/pingcap/tidb/util/logutil" log "github.com/sirupsen/logrus" "golang.org/x/net/context" ) @@ -544,12 +545,19 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { return } if levelStr := req.Form.Get("log_level"); levelStr != "" { + err1 := logutil.SetLevel(levelStr) + if err1 != nil { + writeError(w, err1) + return + } + l, err1 := log.ParseLevel(levelStr) if err1 != nil { writeError(w, err1) return } log.SetLevel(l) + config.GetGlobalConfig().Log.Level = levelStr } if generalLog := req.Form.Get("tidb_general_log"); generalLog != "" { diff --git a/server/http_handler_test.go b/server/http_handler_test.go index a43a3b5ddad7a..41ef068e0a7bb 100644 --- a/server/http_handler_test.go +++ b/server/http_handler_test.go @@ -30,6 +30,7 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/kvproto/pkg/kvrpcpb" + zaplog "github.com/pingcap/log" "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/config" @@ -48,6 +49,7 @@ import ( "github.com/pingcap/tidb/util/codec" "github.com/pingcap/tidb/util/printer" log "github.com/sirupsen/logrus" + "go.uber.org/zap" ) type HTTPHandlerTestSuite struct { @@ -610,6 +612,7 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) { c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusOK) c.Assert(log.GetLevel(), Equals, log.ErrorLevel) + c.Assert(zaplog.GetLevel(), Equals, zap.ErrorLevel) c.Assert(config.GetGlobalConfig().Log.Level, Equals, "error") c.Assert(atomic.LoadUint32(&variable.ProcessGeneralLog), Equals, uint32(1)) form = make(url.Values) @@ -620,6 +623,7 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) { c.Assert(resp.StatusCode, Equals, http.StatusOK) c.Assert(atomic.LoadUint32(&variable.ProcessGeneralLog), Equals, uint32(0)) c.Assert(log.GetLevel(), Equals, log.InfoLevel) + c.Assert(zaplog.GetLevel(), Equals, zap.InfoLevel) c.Assert(config.GetGlobalConfig().Log.Level, Equals, "info") // test ddl_slow_threshold diff --git a/server/server_test.go b/server/server_test.go index be202a06a1006..001f19ad34daa 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -37,9 +37,7 @@ import ( func TestT(t *testing.T) { CustomVerboseFlag = true logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) TestingT(t) } diff --git a/session/tidb_test.go b/session/tidb_test.go index c539b77044c62..4f78c91ac9305 100644 --- a/session/tidb_test.go +++ b/session/tidb_test.go @@ -36,9 +36,7 @@ import ( func TestT(t *testing.T) { logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) CustomVerboseFlag = true TestingT(t) } diff --git a/sessionctx/binloginfo/binloginfo_test.go b/sessionctx/binloginfo/binloginfo_test.go index 57ba7a9c8abb4..a9e8cbe85fbcf 100644 --- a/sessionctx/binloginfo/binloginfo_test.go +++ b/sessionctx/binloginfo/binloginfo_test.go @@ -44,9 +44,7 @@ import ( func TestT(t *testing.T) { CustomVerboseFlag = true logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) TestingT(t) } diff --git a/store/store_test.go b/store/store_test.go index 95ef4c131b4e9..b795b75ea131e 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -38,9 +38,7 @@ const ( func TestT(t *testing.T) { CustomVerboseFlag = true logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) TestingT(t) } diff --git a/tidb-server/main.go b/tidb-server/main.go index 623a184b2f77a..7411a60b64b7c 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -24,6 +24,7 @@ import ( "github.com/opentracing/opentracing-go" "github.com/pingcap/errors" + "github.com/pingcap/log" "github.com/pingcap/parser/mysql" "github.com/pingcap/parser/terror" "github.com/pingcap/pd/client" @@ -50,7 +51,7 @@ import ( "github.com/pingcap/tidb/x-server" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/push" - log "github.com/sirupsen/logrus" + "go.uber.org/zap" ) // Flag Names @@ -142,6 +143,14 @@ func main() { signal.SetupSignalHandler(serverShutdown) runServer() cleanup() + exit() +} + +func exit() { + if err := log.Sync(); err != nil { + fmt.Fprintln(os.Stderr, "sync log err:", err) + os.Exit(1) + } os.Exit(0) } @@ -199,7 +208,7 @@ func setupBinlogClient() { terror.MustNil(err) binloginfo.SetPumpsClient(client) - log.Infof("create pumps client success, ignore binlog error %v", cfg.Binlog.IgnoreError) + log.Info("tidb-server", zap.Bool("create pumps client success, ignore binlog error", cfg.Binlog.IgnoreError)) } // Prometheus push. @@ -211,7 +220,7 @@ func pushMetric(addr string, interval time.Duration) { log.Info("disable Prometheus push client") return } - log.Infof("start Prometheus push client with server addr %s and interval %s", addr, interval) + log.Info("start prometheus push client", zap.String("server addr", addr), zap.String("interval", interval.String())) go prometheusPushClient(addr, interval) } @@ -227,7 +236,7 @@ func prometheusPushClient(addr string, interval time.Duration) { prometheus.DefaultGatherer, ) if err != nil { - log.Errorf("could not push metrics to Prometheus Pushgateway: %v", err) + log.Error("could not push metrics to prometheus pushgateway", zap.String("err", err.Error())) } time.Sleep(interval) } @@ -248,7 +257,7 @@ func parseDuration(lease string) time.Duration { dur, err = time.ParseDuration(lease + "s") } if err != nil || dur < 0 { - log.Fatalf("invalid lease duration %s", lease) + log.Fatal("invalid lease duration", zap.String("lease", lease)) } return dur } @@ -365,15 +374,15 @@ func validateConfig() { nameList = append(nameList, k) } } - log.Errorf("\"store\" should be in [%s] only", strings.Join(nameList, ", ")) + log.Error("validate config", zap.Strings("valid storages", nameList)) os.Exit(-1) } if cfg.Store == "mocktikv" && cfg.RunDDL == false { - log.Errorf("can't disable DDL on mocktikv") + log.Error("can't disable DDL on mocktikv") os.Exit(-1) } if cfg.Log.File.MaxSize > config.MaxLogFileSize { - log.Errorf("log max-size should not be larger than %d MB", config.MaxLogFileSize) + log.Error("validate config", zap.Int("log max-size should not be larger than", config.MaxLogFileSize)) os.Exit(-1) } if cfg.XProtocol.XServer { @@ -384,7 +393,7 @@ func validateConfig() { // lower_case_table_names is allowed to be 0, 1, 2 if cfg.LowerCaseTableNames < 0 || cfg.LowerCaseTableNames > 2 { - log.Errorf("lower-case-table-names should be 0 or 1 or 2.") + log.Error("lower-case-table-names should be 0 or 1 or 2.") os.Exit(-1) } } @@ -424,14 +433,17 @@ func setGlobalVars() { } func setupLog() { - err := logutil.InitLogger(cfg.Log.ToLogConfig()) + err := logutil.InitZapLogger(cfg.Log.ToLogConfig()) + terror.MustNil(err) + + err = logutil.InitLogger(cfg.Log.ToLogConfig()) terror.MustNil(err) } func printInfo() { // Make sure the TiDB info is always printed. level := log.GetLevel() - log.SetLevel(log.InfoLevel) + log.SetLevel(zap.InfoLevel) printer.PrintTiDBInfo() log.SetLevel(level) } @@ -488,7 +500,7 @@ func setupTracing() { tracingCfg := cfg.OpenTracing.ToTracingConfig() tracer, _, err := tracingCfg.New("TiDB") if err != nil { - log.Fatal("cannot initialize Jaeger Tracer", err) + log.Fatal("setup jaeger tracer failed", zap.String("error message", err.Error())) } opentracing.SetGlobalTracer(tracer) } diff --git a/util/logutil/log.go b/util/logutil/log.go index 24b306e25887f..553f0c36bff9d 100644 --- a/util/logutil/log.go +++ b/util/logutil/log.go @@ -23,7 +23,9 @@ import ( "strings" "github.com/pingcap/errors" + zaplog "github.com/pingcap/log" log "github.com/sirupsen/logrus" + "go.uber.org/zap" "gopkg.in/natefinch/lumberjack.v2" ) @@ -31,42 +33,53 @@ const ( defaultLogTimeFormat = "2006/01/02 15:04:05.000" // DefaultLogMaxSize is the default size of log files. DefaultLogMaxSize = 300 // MB - defaultLogFormat = "text" - defaultLogLevel = log.InfoLevel + // DefaultLogFormat is the default format of the log. + DefaultLogFormat = "text" + defaultLogLevel = log.InfoLevel // DefaultSlowThreshold is the default slow log threshold in millisecond. DefaultSlowThreshold = 300 // DefaultQueryLogMaxLen is the default max length of the query in the log. DefaultQueryLogMaxLen = 2048 ) +// EmptyFileLogConfig is an empty FileLogConfig. +var EmptyFileLogConfig = FileLogConfig{} + // FileLogConfig serializes file log related config in toml/json. type FileLogConfig struct { - // Log filename, leave empty to disable file log. - Filename string `toml:"filename" json:"filename"` - // Is log rotate enabled. TODO. - LogRotate bool `toml:"log-rotate" json:"log-rotate"` - // Max size for a single file, in MB. - MaxSize uint `toml:"max-size" json:"max-size"` - // Max log keep days, default is never deleting. - MaxDays uint `toml:"max-days" json:"max-days"` - // Maximum number of old log files to retain. - MaxBackups uint `toml:"max-backups" json:"max-backups"` + zaplog.FileLogConfig +} + +// NewFileLogConfig creates a FileLogConfig. +func NewFileLogConfig(rotate bool, maxSize uint) FileLogConfig { + return FileLogConfig{FileLogConfig: zaplog.FileLogConfig{ + LogRotate: rotate, + MaxSize: int(maxSize), + }, + } } // LogConfig serializes log related config in toml/json. type LogConfig struct { - // Log level. - Level string `toml:"level" json:"level"` - // Log format. one of json, text, or console. - Format string `toml:"format" json:"format"` - // Disable automatic timestamps in output. - DisableTimestamp bool `toml:"disable-timestamp" json:"disable-timestamp"` - // File log config. - File FileLogConfig `toml:"file" json:"file"` + zaplog.Config + // SlowQueryFile filename, default to File log config on empty. SlowQueryFile string } +// NewLogConfig creates a LogConfig. +func NewLogConfig(level, format, slowQueryFile string, fileCfg FileLogConfig, disableTimestamp bool) *LogConfig { + return &LogConfig{ + Config: zaplog.Config{ + Level: level, + Format: format, + DisableTimestamp: disableTimestamp, + File: fileCfg.FileLogConfig, + }, + SlowQueryFile: slowQueryFile, + } +} + // isSKippedPackageName tests wether path name is on log library calling stack. func isSkippedPackageName(name string) bool { return strings.Contains(name, "github.com/sirupsen/logrus") || @@ -220,7 +233,7 @@ func stringToLogFormatter(format string, disableTimestamp bool) log.Formatter { } // initFileLog initializes file based logging options. -func initFileLog(cfg *FileLogConfig, logger *log.Logger) error { +func initFileLog(cfg *zaplog.FileLogConfig, logger *log.Logger) error { if st, err := os.Stat(cfg.Filename); err == nil { if st.IsDir() { return errors.New("can't use directory as log file name") @@ -250,13 +263,16 @@ func initFileLog(cfg *FileLogConfig, logger *log.Logger) error { // SlowQueryLogger is used to log slow query, InitLogger will modify it according to config file. var SlowQueryLogger = log.StandardLogger() +// SlowQueryZapLogger is used to log slow query, InitZapLogger will set it according to config file. +var SlowQueryZapLogger *zap.Logger + // InitLogger initializes PD's logger. func InitLogger(cfg *LogConfig) error { log.SetLevel(stringToLogLevel(cfg.Level)) log.AddHook(&contextHook{}) if cfg.Format == "" { - cfg.Format = defaultLogFormat + cfg.Format = DefaultLogFormat } formatter := stringToLogFormatter(cfg.Format, cfg.DisableTimestamp) log.SetFormatter(formatter) @@ -287,3 +303,45 @@ func InitLogger(cfg *LogConfig) error { return nil } + +// InitZapLogger initializes a zap logger with cfg. +func InitZapLogger(cfg *LogConfig) error { + gl, props, err := zaplog.InitLogger(&cfg.Config) + if err != nil { + return errors.Trace(err) + } + zaplog.ReplaceGlobals(gl, props) + + if len(cfg.SlowQueryFile) != 0 { + sqfCfg := zaplog.FileLogConfig{ + LogRotate: cfg.File.LogRotate, + MaxSize: cfg.File.MaxSize, + Filename: cfg.SlowQueryFile, + } + sqCfg := &zaplog.Config{ + Level: cfg.Level, + Format: cfg.Format, + DisableTimestamp: cfg.DisableTimestamp, + File: sqfCfg, + } + sqLogger, _, err := zaplog.InitLogger(sqCfg) + if err != nil { + return errors.Trace(err) + } + SlowQueryZapLogger = sqLogger + } else { + SlowQueryZapLogger = gl + } + + return nil +} + +// SetLevel sets the zap logger's level. +func SetLevel(level string) error { + l := zap.NewAtomicLevel() + if err := l.UnmarshalText([]byte(level)); err != nil { + return errors.Trace(err) + } + zaplog.SetLevel(l.Level()) + return nil +} diff --git a/util/logutil/log_test.go b/util/logutil/log_test.go index 5327bc5b8643b..a02d675a7d6a5 100644 --- a/util/logutil/log_test.go +++ b/util/logutil/log_test.go @@ -24,11 +24,16 @@ import ( "testing" . "github.com/pingcap/check" + zaplog "github.com/pingcap/log" log "github.com/sirupsen/logrus" + "go.uber.org/zap" ) const ( logPattern = `\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d\.\d\d\d ([\w_%!$@.,+~-]+|\\.)+:\d+: \[(fatal|error|warning|info|debug)\] .*?\n` + // zapLogPatern is used to match the zap log format, such as the following log: + // [2019/02/13 15:56:05.385 +08:00] [INFO] [log_test.go:167] ["info message"] ["str key"=val] ["int key"=123] + zapLogPattern = `\[\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d.\d\d\d\ \+\d\d:\d\d\] \[(FATAL|ERROR|WARN|INFO|DEBUG)\] \[([\w_%!$@.,+~-]+|\\.)+:\d+\] \[.*\] (\[.*=.*\]).*\n` ) func Test(t *testing.T) { @@ -61,7 +66,7 @@ func (s *testLogSuite) TestStringToLogLevel(c *C) { // TestLogging assure log format and log redirection works. func (s *testLogSuite) TestLogging(c *C) { - conf := &LogConfig{Level: "warn", File: FileLogConfig{}} + conf := NewLogConfig("warn", DefaultLogFormat, "", EmptyFileLogConfig, false) c.Assert(InitLogger(conf), IsNil) log.SetOutput(s.buf) @@ -84,7 +89,7 @@ func (s *testLogSuite) TestLogging(c *C) { func (s *testLogSuite) TestSlowQueryLogger(c *C) { fileName := "slow_query" - conf := &LogConfig{Level: "info", File: FileLogConfig{}, SlowQueryFile: fileName} + conf := NewLogConfig("info", DefaultLogFormat, fileName, EmptyFileLogConfig, false) err := InitLogger(conf) c.Assert(err, IsNil) defer os.Remove(fileName) @@ -113,7 +118,7 @@ func (s *testLogSuite) TestSlowQueryLogger(c *C) { func (s *testLogSuite) TestSlowQueryLoggerKeepOrder(c *C) { fileName := "slow_query" - conf := &LogConfig{Level: "warn", File: FileLogConfig{}, Format: "text", DisableTimestamp: true, SlowQueryFile: fileName} + conf := NewLogConfig("warn", DefaultLogFormat, fileName, EmptyFileLogConfig, true) c.Assert(InitLogger(conf), IsNil) defer os.Remove(fileName) ft, ok := SlowQueryLogger.Formatter.(*textFormatter) @@ -149,3 +154,48 @@ func (s *testLogSuite) TestSlowQueryLoggerKeepOrder(c *C) { expectMsg = fmt.Sprintf("log_test.go:%v: [warning] slow-query a=a b=b c=c d=d e=e f=f\n", line+1) c.Assert(s.buf.String(), Equals, expectMsg) } + +func (s *testLogSuite) TestSlowQueryZapLogger(c *C) { + fileName := "slow_query" + conf := NewLogConfig("info", DefaultLogFormat, fileName, EmptyFileLogConfig, false) + err := InitZapLogger(conf) + c.Assert(err, IsNil) + defer os.Remove(fileName) + + SlowQueryZapLogger.Debug("debug message", zap.String("str key", "val")) + SlowQueryZapLogger.Info("info message", zap.String("str key", "val")) + SlowQueryZapLogger.Warn("warn", zap.Int("int key", 123)) + SlowQueryZapLogger.Error("error message", zap.Bool("bool key", true)) + + f, err := os.Open(fileName) + c.Assert(err, IsNil) + defer f.Close() + + r := bufio.NewReader(f) + for { + var str string + str, err = r.ReadString('\n') + if err != nil { + break + } + c.Assert(str, Matches, zapLogPattern) + } + c.Assert(err, Equals, io.EOF) +} + +func (s *testLogSuite) TestSetLevel(c *C) { + conf := NewLogConfig("info", DefaultLogFormat, "", EmptyFileLogConfig, false) + err := InitZapLogger(conf) + c.Assert(err, IsNil) + + c.Assert(zaplog.GetLevel(), Equals, zap.InfoLevel) + err = SetLevel("warn") + c.Assert(err, IsNil) + c.Assert(zaplog.GetLevel(), Equals, zap.WarnLevel) + err = SetLevel("Error") + c.Assert(err, IsNil) + c.Assert(zaplog.GetLevel(), Equals, zap.ErrorLevel) + err = SetLevel("DEBUG") + c.Assert(err, IsNil) + c.Assert(zaplog.GetLevel(), Equals, zap.DebugLevel) +} diff --git a/util/memory/tracker_test.go b/util/memory/tracker_test.go index 65788551890be..2982fff0374d5 100644 --- a/util/memory/tracker_test.go +++ b/util/memory/tracker_test.go @@ -26,9 +26,7 @@ import ( func TestT(t *testing.T) { CustomVerboseFlag = true logLevel := os.Getenv("log_level") - logutil.InitLogger(&logutil.LogConfig{ - Level: logLevel, - }) + logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false)) TestingT(t) }