From 88e1666ac644614a57eccf3ae91b14b191a25594 Mon Sep 17 00:00:00 2001 From: Ewan Chou Date: Fri, 8 Sep 2017 13:45:07 +0800 Subject: [PATCH] *: change the log package to logrus (#4452) --- .../src/github.com/coreos/go-systemd/LICENSE | 191 ++++++++ .../coreos/go-systemd/journal/journal.go | 179 ++++++++ .../{ngaut/systimemon => coreos/pkg}/LICENSE | 3 +- _vendor/src/github.com/coreos/pkg/NOTICE | 5 + .../coreos/pkg/capnslog/formatters.go | 157 +++++++ .../coreos/pkg/capnslog/glog_formatter.go | 96 ++++ .../github.com/coreos/pkg/capnslog/init.go | 49 ++ .../coreos/pkg/capnslog/init_windows.go | 25 ++ .../coreos/pkg/capnslog/journald_formatter.go | 68 +++ .../coreos/pkg/capnslog/log_hijack.go | 39 ++ .../github.com/coreos/pkg/capnslog/logmap.go | 240 ++++++++++ .../coreos/pkg/capnslog/pkg_logger.go | 177 ++++++++ .../coreos/pkg/capnslog/syslog_formatter.go | 65 +++ .../ngaut/systimemon/systime_mon.go | 24 - .../github.com/pingcap/pd/pkg/logutil/log.go | 240 ++++++++++ .../gopkg.in/natefinch/lumberjack.v2/LICENSE | 21 + .../gopkg.in/natefinch/lumberjack.v2/chown.go | 11 + .../natefinch/lumberjack.v2/chown_linux.go | 19 + .../natefinch/lumberjack.v2/lumberjack.go | 425 ++++++++++++++++++ bench_test.go | 4 +- bootstrap.go | 2 +- cmd/benchdb/main.go | 7 +- cmd/benchfilesort/main.go | 7 +- cmd/benchkv/main.go | 4 +- cmd/benchraw/main.go | 4 +- ddl/column.go | 2 +- ddl/ddl.go | 2 +- ddl/ddl_test.go | 7 +- ddl/ddl_worker.go | 2 +- ddl/delete_range.go | 2 +- ddl/index.go | 2 +- ddl/reorg.go | 2 +- ddl/syncer.go | 2 +- domain/domain.go | 2 +- domain/schema_validator.go | 2 +- executor/adapter.go | 2 +- executor/analyze.go | 2 +- executor/compiler.go | 2 +- executor/distsql.go | 2 +- executor/executor_test.go | 6 +- executor/metrics.go | 2 +- executor/new_distsql.go | 2 +- executor/set.go | 2 +- executor/simple.go | 2 +- executor/write.go | 2 +- expression/aggregation.go | 2 +- expression/builtin_string.go | 2 +- expression/builtin_time.go | 2 +- expression/column.go | 2 +- expression/constant_fold.go | 2 +- expression/constant_propagation.go | 2 +- expression/expr_to_pb.go | 2 +- glide.lock | 19 +- glide.yaml | 6 +- inspectkv/inspectkv.go | 2 +- kv/txn.go | 2 +- kv/union_iter.go | 2 +- meta/autoid/autoid.go | 2 +- owner/manager.go | 2 +- perfschema/statement.go | 2 +- plan/column_pruning.go | 2 +- plan/join_reorder.go | 2 +- plan/new_physical_plan_builder.go | 2 +- plan/physical_plan_builder.go | 2 +- plan/stats.go | 2 +- privilege/privileges/cache.go | 2 +- privilege/privileges/privileges.go | 2 +- server/conn.go | 2 +- server/http_status.go | 2 +- server/region_handler.go | 2 +- server/server.go | 2 +- server/server_test.go | 2 +- server/tidb_test.go | 4 +- session.go | 2 +- sessionctx/binloginfo/binloginfo.go | 2 +- sessionctx/binloginfo/binloginfo_test.go | 6 +- statistics/ddl.go | 2 +- statistics/handle.go | 2 +- statistics/table.go | 2 +- statistics/update.go | 2 +- store/localstore/compactor.go | 2 +- store/localstore/kv.go | 2 +- store/localstore/local_version_provider.go | 2 +- store/localstore/txn.go | 2 +- store/store_test.go | 6 +- store/tikv/2pc.go | 2 +- store/tikv/backoff.go | 4 +- store/tikv/coprocessor.go | 2 +- store/tikv/gc_worker.go | 2 +- store/tikv/kv.go | 2 +- store/tikv/lock_resolver.go | 2 +- store/tikv/mock-tikv/mvcc_leveldb.go | 2 +- store/tikv/oracle/oracles/pd.go | 2 +- store/tikv/region_cache.go | 2 +- store/tikv/region_request.go | 2 +- store/tikv/scan.go | 2 +- store/tikv/snapshot.go | 2 +- store/tikv/snapshot_test.go | 2 +- store/tikv/txn.go | 2 +- table/column.go | 2 +- table/tables/memory_tables.go | 2 +- table/tables/tables.go | 2 +- terror/terror.go | 2 +- tidb-server/main.go | 29 +- tidb.go | 2 +- tidb_test.go | 6 +- util/printer/printer.go | 2 +- util/systimemon/systime_mon.go | 37 ++ util/systimemon/systime_mon_test.go | 41 ++ util/types/time.go | 2 +- x-server/conn.go | 2 +- x-server/server.go | 2 +- 112 files changed, 2237 insertions(+), 146 deletions(-) create mode 100644 _vendor/src/github.com/coreos/go-systemd/LICENSE create mode 100644 _vendor/src/github.com/coreos/go-systemd/journal/journal.go rename _vendor/src/github.com/{ngaut/systimemon => coreos/pkg}/LICENSE (99%) create mode 100644 _vendor/src/github.com/coreos/pkg/NOTICE create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/formatters.go create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/glog_formatter.go create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/init.go create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/init_windows.go create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/journald_formatter.go create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/log_hijack.go create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/logmap.go create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/pkg_logger.go create mode 100644 _vendor/src/github.com/coreos/pkg/capnslog/syslog_formatter.go delete mode 100644 _vendor/src/github.com/ngaut/systimemon/systime_mon.go create mode 100644 _vendor/src/github.com/pingcap/pd/pkg/logutil/log.go create mode 100644 _vendor/src/gopkg.in/natefinch/lumberjack.v2/LICENSE create mode 100644 _vendor/src/gopkg.in/natefinch/lumberjack.v2/chown.go create mode 100644 _vendor/src/gopkg.in/natefinch/lumberjack.v2/chown_linux.go create mode 100644 _vendor/src/gopkg.in/natefinch/lumberjack.v2/lumberjack.go create mode 100644 util/systimemon/systime_mon.go create mode 100644 util/systimemon/systime_mon_test.go diff --git a/_vendor/src/github.com/coreos/go-systemd/LICENSE b/_vendor/src/github.com/coreos/go-systemd/LICENSE new file mode 100644 index 0000000000000..37ec93a14fdcd --- /dev/null +++ b/_vendor/src/github.com/coreos/go-systemd/LICENSE @@ -0,0 +1,191 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets "[]" replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same "printed page" as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/_vendor/src/github.com/coreos/go-systemd/journal/journal.go b/_vendor/src/github.com/coreos/go-systemd/journal/journal.go new file mode 100644 index 0000000000000..7f434990d25b4 --- /dev/null +++ b/_vendor/src/github.com/coreos/go-systemd/journal/journal.go @@ -0,0 +1,179 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package journal provides write bindings to the local systemd journal. +// It is implemented in pure Go and connects to the journal directly over its +// unix socket. +// +// To read from the journal, see the "sdjournal" package, which wraps the +// sd-journal a C API. +// +// http://www.freedesktop.org/software/systemd/man/systemd-journald.service.html +package journal + +import ( + "bytes" + "encoding/binary" + "errors" + "fmt" + "io" + "io/ioutil" + "net" + "os" + "strconv" + "strings" + "syscall" +) + +// Priority of a journal message +type Priority int + +const ( + PriEmerg Priority = iota + PriAlert + PriCrit + PriErr + PriWarning + PriNotice + PriInfo + PriDebug +) + +var conn net.Conn + +func init() { + var err error + conn, err = net.Dial("unixgram", "/run/systemd/journal/socket") + if err != nil { + conn = nil + } +} + +// Enabled returns true if the local systemd journal is available for logging +func Enabled() bool { + return conn != nil +} + +// Send a message to the local systemd journal. vars is a map of journald +// fields to values. Fields must be composed of uppercase letters, numbers, +// and underscores, but must not start with an underscore. Within these +// restrictions, any arbitrary field name may be used. Some names have special +// significance: see the journalctl documentation +// (http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html) +// for more details. vars may be nil. +func Send(message string, priority Priority, vars map[string]string) error { + if conn == nil { + return journalError("could not connect to journald socket") + } + + data := new(bytes.Buffer) + appendVariable(data, "PRIORITY", strconv.Itoa(int(priority))) + appendVariable(data, "MESSAGE", message) + for k, v := range vars { + appendVariable(data, k, v) + } + + _, err := io.Copy(conn, data) + if err != nil && isSocketSpaceError(err) { + file, err := tempFd() + if err != nil { + return journalError(err.Error()) + } + defer file.Close() + _, err = io.Copy(file, data) + if err != nil { + return journalError(err.Error()) + } + + rights := syscall.UnixRights(int(file.Fd())) + + /* this connection should always be a UnixConn, but better safe than sorry */ + unixConn, ok := conn.(*net.UnixConn) + if !ok { + return journalError("can't send file through non-Unix connection") + } + unixConn.WriteMsgUnix([]byte{}, rights, nil) + } else if err != nil { + return journalError(err.Error()) + } + return nil +} + +// Print prints a message to the local systemd journal using Send(). +func Print(priority Priority, format string, a ...interface{}) error { + return Send(fmt.Sprintf(format, a...), priority, nil) +} + +func appendVariable(w io.Writer, name, value string) { + if !validVarName(name) { + journalError("variable name contains invalid character, ignoring") + } + if strings.ContainsRune(value, '\n') { + /* When the value contains a newline, we write: + * - the variable name, followed by a newline + * - the size (in 64bit little endian format) + * - the data, followed by a newline + */ + fmt.Fprintln(w, name) + binary.Write(w, binary.LittleEndian, uint64(len(value))) + fmt.Fprintln(w, value) + } else { + /* just write the variable and value all on one line */ + fmt.Fprintf(w, "%s=%s\n", name, value) + } +} + +func validVarName(name string) bool { + /* The variable name must be in uppercase and consist only of characters, + * numbers and underscores, and may not begin with an underscore. (from the docs) + */ + + valid := name[0] != '_' + for _, c := range name { + valid = valid && ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' + } + return valid +} + +func isSocketSpaceError(err error) bool { + opErr, ok := err.(*net.OpError) + if !ok { + return false + } + + sysErr, ok := opErr.Err.(syscall.Errno) + if !ok { + return false + } + + return sysErr == syscall.EMSGSIZE || sysErr == syscall.ENOBUFS +} + +func tempFd() (*os.File, error) { + file, err := ioutil.TempFile("/dev/shm/", "journal.XXXXX") + if err != nil { + return nil, err + } + syscall.Unlink(file.Name()) + if err != nil { + return nil, err + } + return file, nil +} + +func journalError(s string) error { + s = "journal error: " + s + fmt.Fprintln(os.Stderr, s) + return errors.New(s) +} diff --git a/_vendor/src/github.com/ngaut/systimemon/LICENSE b/_vendor/src/github.com/coreos/pkg/LICENSE similarity index 99% rename from _vendor/src/github.com/ngaut/systimemon/LICENSE rename to _vendor/src/github.com/coreos/pkg/LICENSE index 8dada3edaf50d..e06d2081865a7 100644 --- a/_vendor/src/github.com/ngaut/systimemon/LICENSE +++ b/_vendor/src/github.com/coreos/pkg/LICENSE @@ -1,4 +1,4 @@ - Apache License +Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ @@ -199,3 +199,4 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + diff --git a/_vendor/src/github.com/coreos/pkg/NOTICE b/_vendor/src/github.com/coreos/pkg/NOTICE new file mode 100644 index 0000000000000..b39ddfa5cbdea --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/NOTICE @@ -0,0 +1,5 @@ +CoreOS Project +Copyright 2014 CoreOS, Inc + +This product includes software developed at CoreOS, Inc. +(http://www.coreos.com/). diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/formatters.go b/_vendor/src/github.com/coreos/pkg/capnslog/formatters.go new file mode 100644 index 0000000000000..b305a845fb2bf --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/formatters.go @@ -0,0 +1,157 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package capnslog + +import ( + "bufio" + "fmt" + "io" + "log" + "runtime" + "strings" + "time" +) + +type Formatter interface { + Format(pkg string, level LogLevel, depth int, entries ...interface{}) + Flush() +} + +func NewStringFormatter(w io.Writer) Formatter { + return &StringFormatter{ + w: bufio.NewWriter(w), + } +} + +type StringFormatter struct { + w *bufio.Writer +} + +func (s *StringFormatter) Format(pkg string, l LogLevel, i int, entries ...interface{}) { + now := time.Now().UTC() + s.w.WriteString(now.Format(time.RFC3339)) + s.w.WriteByte(' ') + writeEntries(s.w, pkg, l, i, entries...) + s.Flush() +} + +func writeEntries(w *bufio.Writer, pkg string, _ LogLevel, _ int, entries ...interface{}) { + if pkg != "" { + w.WriteString(pkg + ": ") + } + str := fmt.Sprint(entries...) + endsInNL := strings.HasSuffix(str, "\n") + w.WriteString(str) + if !endsInNL { + w.WriteString("\n") + } +} + +func (s *StringFormatter) Flush() { + s.w.Flush() +} + +func NewPrettyFormatter(w io.Writer, debug bool) Formatter { + return &PrettyFormatter{ + w: bufio.NewWriter(w), + debug: debug, + } +} + +type PrettyFormatter struct { + w *bufio.Writer + debug bool +} + +func (c *PrettyFormatter) Format(pkg string, l LogLevel, depth int, entries ...interface{}) { + now := time.Now() + ts := now.Format("2006-01-02 15:04:05") + c.w.WriteString(ts) + ms := now.Nanosecond() / 1000 + c.w.WriteString(fmt.Sprintf(".%06d", ms)) + if c.debug { + _, file, line, ok := runtime.Caller(depth) // It's always the same number of frames to the user's call. + if !ok { + file = "???" + line = 1 + } else { + slash := strings.LastIndex(file, "/") + if slash >= 0 { + file = file[slash+1:] + } + } + if line < 0 { + line = 0 // not a real line number + } + c.w.WriteString(fmt.Sprintf(" [%s:%d]", file, line)) + } + c.w.WriteString(fmt.Sprint(" ", l.Char(), " | ")) + writeEntries(c.w, pkg, l, depth, entries...) + c.Flush() +} + +func (c *PrettyFormatter) Flush() { + c.w.Flush() +} + +// LogFormatter emulates the form of the traditional built-in logger. +type LogFormatter struct { + logger *log.Logger + prefix string +} + +// NewLogFormatter is a helper to produce a new LogFormatter struct. It uses the +// golang log package to actually do the logging work so that logs look similar. +func NewLogFormatter(w io.Writer, prefix string, flag int) Formatter { + return &LogFormatter{ + logger: log.New(w, "", flag), // don't use prefix here + prefix: prefix, // save it instead + } +} + +// Format builds a log message for the LogFormatter. The LogLevel is ignored. +func (lf *LogFormatter) Format(pkg string, _ LogLevel, _ int, entries ...interface{}) { + str := fmt.Sprint(entries...) + prefix := lf.prefix + if pkg != "" { + prefix = fmt.Sprintf("%s%s: ", prefix, pkg) + } + lf.logger.Output(5, fmt.Sprintf("%s%v", prefix, str)) // call depth is 5 +} + +// Flush is included so that the interface is complete, but is a no-op. +func (lf *LogFormatter) Flush() { + // noop +} + +// NilFormatter is a no-op log formatter that does nothing. +type NilFormatter struct { +} + +// NewNilFormatter is a helper to produce a new LogFormatter struct. It logs no +// messages so that you can cause part of your logging to be silent. +func NewNilFormatter() Formatter { + return &NilFormatter{} +} + +// Format does nothing. +func (_ *NilFormatter) Format(_ string, _ LogLevel, _ int, _ ...interface{}) { + // noop +} + +// Flush is included so that the interface is complete, but is a no-op. +func (_ *NilFormatter) Flush() { + // noop +} diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/glog_formatter.go b/_vendor/src/github.com/coreos/pkg/capnslog/glog_formatter.go new file mode 100644 index 0000000000000..426603ef305c7 --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/glog_formatter.go @@ -0,0 +1,96 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package capnslog + +import ( + "bufio" + "bytes" + "io" + "os" + "runtime" + "strconv" + "strings" + "time" +) + +var pid = os.Getpid() + +type GlogFormatter struct { + StringFormatter +} + +func NewGlogFormatter(w io.Writer) *GlogFormatter { + g := &GlogFormatter{} + g.w = bufio.NewWriter(w) + return g +} + +func (g GlogFormatter) Format(pkg string, level LogLevel, depth int, entries ...interface{}) { + g.w.Write(GlogHeader(level, depth+1)) + g.StringFormatter.Format(pkg, level, depth+1, entries...) +} + +func GlogHeader(level LogLevel, depth int) []byte { + // Lmmdd hh:mm:ss.uuuuuu threadid file:line] + now := time.Now().UTC() + _, file, line, ok := runtime.Caller(depth) // It's always the same number of frames to the user's call. + if !ok { + file = "???" + line = 1 + } else { + slash := strings.LastIndex(file, "/") + if slash >= 0 { + file = file[slash+1:] + } + } + if line < 0 { + line = 0 // not a real line number + } + buf := &bytes.Buffer{} + buf.Grow(30) + _, month, day := now.Date() + hour, minute, second := now.Clock() + buf.WriteString(level.Char()) + twoDigits(buf, int(month)) + twoDigits(buf, day) + buf.WriteByte(' ') + twoDigits(buf, hour) + buf.WriteByte(':') + twoDigits(buf, minute) + buf.WriteByte(':') + twoDigits(buf, second) + buf.WriteByte('.') + buf.WriteString(strconv.Itoa(now.Nanosecond() / 1000)) + buf.WriteByte('Z') + buf.WriteByte(' ') + buf.WriteString(strconv.Itoa(pid)) + buf.WriteByte(' ') + buf.WriteString(file) + buf.WriteByte(':') + buf.WriteString(strconv.Itoa(line)) + buf.WriteByte(']') + buf.WriteByte(' ') + return buf.Bytes() +} + +const digits = "0123456789" + +func twoDigits(b *bytes.Buffer, d int) { + c2 := digits[d%10] + d /= 10 + c1 := digits[d%10] + b.WriteByte(c1) + b.WriteByte(c2) +} diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/init.go b/_vendor/src/github.com/coreos/pkg/capnslog/init.go new file mode 100644 index 0000000000000..44b8cd361b0f7 --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/init.go @@ -0,0 +1,49 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +build !windows + +package capnslog + +import ( + "io" + "os" + "syscall" +) + +// Here's where the opinionation comes in. We need some sensible defaults, +// especially after taking over the log package. Your project (whatever it may +// be) may see things differently. That's okay; there should be no defaults in +// the main package that cannot be controlled or overridden programatically, +// otherwise it's a bug. Doing so is creating your own init_log.go file much +// like this one. + +func init() { + initHijack() + + // Go `log` pacakge uses os.Stderr. + SetFormatter(NewDefaultFormatter(os.Stderr)) + SetGlobalLogLevel(INFO) +} + +func NewDefaultFormatter(out io.Writer) Formatter { + if syscall.Getppid() == 1 { + // We're running under init, which may be systemd. + f, err := NewJournaldFormatter() + if err == nil { + return f + } + } + return NewPrettyFormatter(out, false) +} diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/init_windows.go b/_vendor/src/github.com/coreos/pkg/capnslog/init_windows.go new file mode 100644 index 0000000000000..45530506537ff --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/init_windows.go @@ -0,0 +1,25 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package capnslog + +import "os" + +func init() { + initHijack() + + // Go `log` package uses os.Stderr. + SetFormatter(NewPrettyFormatter(os.Stderr, false)) + SetGlobalLogLevel(INFO) +} diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/journald_formatter.go b/_vendor/src/github.com/coreos/pkg/capnslog/journald_formatter.go new file mode 100644 index 0000000000000..72e05207c52ce --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/journald_formatter.go @@ -0,0 +1,68 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +build !windows + +package capnslog + +import ( + "errors" + "fmt" + "os" + "path/filepath" + + "github.com/coreos/go-systemd/journal" +) + +func NewJournaldFormatter() (Formatter, error) { + if !journal.Enabled() { + return nil, errors.New("No systemd detected") + } + return &journaldFormatter{}, nil +} + +type journaldFormatter struct{} + +func (j *journaldFormatter) Format(pkg string, l LogLevel, _ int, entries ...interface{}) { + var pri journal.Priority + switch l { + case CRITICAL: + pri = journal.PriCrit + case ERROR: + pri = journal.PriErr + case WARNING: + pri = journal.PriWarning + case NOTICE: + pri = journal.PriNotice + case INFO: + pri = journal.PriInfo + case DEBUG: + pri = journal.PriDebug + case TRACE: + pri = journal.PriDebug + default: + panic("Unhandled loglevel") + } + msg := fmt.Sprint(entries...) + tags := map[string]string{ + "PACKAGE": pkg, + "SYSLOG_IDENTIFIER": filepath.Base(os.Args[0]), + } + err := journal.Send(msg, pri, tags) + if err != nil { + fmt.Fprintln(os.Stderr, err) + } +} + +func (j *journaldFormatter) Flush() {} diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/log_hijack.go b/_vendor/src/github.com/coreos/pkg/capnslog/log_hijack.go new file mode 100644 index 0000000000000..970086b9f9730 --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/log_hijack.go @@ -0,0 +1,39 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package capnslog + +import ( + "log" +) + +func initHijack() { + pkg := NewPackageLogger("log", "") + w := packageWriter{pkg} + log.SetFlags(0) + log.SetPrefix("") + log.SetOutput(w) +} + +type packageWriter struct { + pl *PackageLogger +} + +func (p packageWriter) Write(b []byte) (int, error) { + if p.pl.level < INFO { + return 0, nil + } + p.pl.internalLog(calldepth+2, INFO, string(b)) + return len(b), nil +} diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/logmap.go b/_vendor/src/github.com/coreos/pkg/capnslog/logmap.go new file mode 100644 index 0000000000000..84954488308db --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/logmap.go @@ -0,0 +1,240 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package capnslog + +import ( + "errors" + "strings" + "sync" +) + +// LogLevel is the set of all log levels. +type LogLevel int8 + +const ( + // CRITICAL is the lowest log level; only errors which will end the program will be propagated. + CRITICAL LogLevel = iota - 1 + // ERROR is for errors that are not fatal but lead to troubling behavior. + ERROR + // WARNING is for errors which are not fatal and not errors, but are unusual. Often sourced from misconfigurations. + WARNING + // NOTICE is for normal but significant conditions. + NOTICE + // INFO is a log level for common, everyday log updates. + INFO + // DEBUG is the default hidden level for more verbose updates about internal processes. + DEBUG + // TRACE is for (potentially) call by call tracing of programs. + TRACE +) + +// Char returns a single-character representation of the log level. +func (l LogLevel) Char() string { + switch l { + case CRITICAL: + return "C" + case ERROR: + return "E" + case WARNING: + return "W" + case NOTICE: + return "N" + case INFO: + return "I" + case DEBUG: + return "D" + case TRACE: + return "T" + default: + panic("Unhandled loglevel") + } +} + +// String returns a multi-character representation of the log level. +func (l LogLevel) String() string { + switch l { + case CRITICAL: + return "CRITICAL" + case ERROR: + return "ERROR" + case WARNING: + return "WARNING" + case NOTICE: + return "NOTICE" + case INFO: + return "INFO" + case DEBUG: + return "DEBUG" + case TRACE: + return "TRACE" + default: + panic("Unhandled loglevel") + } +} + +// Update using the given string value. Fulfills the flag.Value interface. +func (l *LogLevel) Set(s string) error { + value, err := ParseLevel(s) + if err != nil { + return err + } + + *l = value + return nil +} + +// ParseLevel translates some potential loglevel strings into their corresponding levels. +func ParseLevel(s string) (LogLevel, error) { + switch s { + case "CRITICAL", "C": + return CRITICAL, nil + case "ERROR", "0", "E": + return ERROR, nil + case "WARNING", "1", "W": + return WARNING, nil + case "NOTICE", "2", "N": + return NOTICE, nil + case "INFO", "3", "I": + return INFO, nil + case "DEBUG", "4", "D": + return DEBUG, nil + case "TRACE", "5", "T": + return TRACE, nil + } + return CRITICAL, errors.New("couldn't parse log level " + s) +} + +type RepoLogger map[string]*PackageLogger + +type loggerStruct struct { + sync.Mutex + repoMap map[string]RepoLogger + formatter Formatter +} + +// logger is the global logger +var logger = new(loggerStruct) + +// SetGlobalLogLevel sets the log level for all packages in all repositories +// registered with capnslog. +func SetGlobalLogLevel(l LogLevel) { + logger.Lock() + defer logger.Unlock() + for _, r := range logger.repoMap { + r.setRepoLogLevelInternal(l) + } +} + +// GetRepoLogger may return the handle to the repository's set of packages' loggers. +func GetRepoLogger(repo string) (RepoLogger, error) { + logger.Lock() + defer logger.Unlock() + r, ok := logger.repoMap[repo] + if !ok { + return nil, errors.New("no packages registered for repo " + repo) + } + return r, nil +} + +// MustRepoLogger returns the handle to the repository's packages' loggers. +func MustRepoLogger(repo string) RepoLogger { + r, err := GetRepoLogger(repo) + if err != nil { + panic(err) + } + return r +} + +// SetRepoLogLevel sets the log level for all packages in the repository. +func (r RepoLogger) SetRepoLogLevel(l LogLevel) { + logger.Lock() + defer logger.Unlock() + r.setRepoLogLevelInternal(l) +} + +func (r RepoLogger) setRepoLogLevelInternal(l LogLevel) { + for _, v := range r { + v.level = l + } +} + +// ParseLogLevelConfig parses a comma-separated string of "package=loglevel", in +// order, and returns a map of the results, for use in SetLogLevel. +func (r RepoLogger) ParseLogLevelConfig(conf string) (map[string]LogLevel, error) { + setlist := strings.Split(conf, ",") + out := make(map[string]LogLevel) + for _, setstring := range setlist { + setting := strings.Split(setstring, "=") + if len(setting) != 2 { + return nil, errors.New("oddly structured `pkg=level` option: " + setstring) + } + l, err := ParseLevel(setting[1]) + if err != nil { + return nil, err + } + out[setting[0]] = l + } + return out, nil +} + +// SetLogLevel takes a map of package names within a repository to their desired +// loglevel, and sets the levels appropriately. Unknown packages are ignored. +// "*" is a special package name that corresponds to all packages, and will be +// processed first. +func (r RepoLogger) SetLogLevel(m map[string]LogLevel) { + logger.Lock() + defer logger.Unlock() + if l, ok := m["*"]; ok { + r.setRepoLogLevelInternal(l) + } + for k, v := range m { + l, ok := r[k] + if !ok { + continue + } + l.level = v + } +} + +// SetFormatter sets the formatting function for all logs. +func SetFormatter(f Formatter) { + logger.Lock() + defer logger.Unlock() + logger.formatter = f +} + +// NewPackageLogger creates a package logger object. +// This should be defined as a global var in your package, referencing your repo. +func NewPackageLogger(repo string, pkg string) (p *PackageLogger) { + logger.Lock() + defer logger.Unlock() + if logger.repoMap == nil { + logger.repoMap = make(map[string]RepoLogger) + } + r, rok := logger.repoMap[repo] + if !rok { + logger.repoMap[repo] = make(RepoLogger) + r = logger.repoMap[repo] + } + p, pok := r[pkg] + if !pok { + r[pkg] = &PackageLogger{ + pkg: pkg, + level: INFO, + } + p = r[pkg] + } + return +} diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/pkg_logger.go b/_vendor/src/github.com/coreos/pkg/capnslog/pkg_logger.go new file mode 100644 index 0000000000000..612d55c66c802 --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/pkg_logger.go @@ -0,0 +1,177 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package capnslog + +import ( + "fmt" + "os" +) + +type PackageLogger struct { + pkg string + level LogLevel +} + +const calldepth = 2 + +func (p *PackageLogger) internalLog(depth int, inLevel LogLevel, entries ...interface{}) { + logger.Lock() + defer logger.Unlock() + if inLevel != CRITICAL && p.level < inLevel { + return + } + if logger.formatter != nil { + logger.formatter.Format(p.pkg, inLevel, depth+1, entries...) + } +} + +func (p *PackageLogger) LevelAt(l LogLevel) bool { + logger.Lock() + defer logger.Unlock() + return p.level >= l +} + +// Log a formatted string at any level between ERROR and TRACE +func (p *PackageLogger) Logf(l LogLevel, format string, args ...interface{}) { + p.internalLog(calldepth, l, fmt.Sprintf(format, args...)) +} + +// Log a message at any level between ERROR and TRACE +func (p *PackageLogger) Log(l LogLevel, args ...interface{}) { + p.internalLog(calldepth, l, fmt.Sprint(args...)) +} + +// log stdlib compatibility + +func (p *PackageLogger) Println(args ...interface{}) { + p.internalLog(calldepth, INFO, fmt.Sprintln(args...)) +} + +func (p *PackageLogger) Printf(format string, args ...interface{}) { + p.Logf(INFO, format, args...) +} + +func (p *PackageLogger) Print(args ...interface{}) { + p.internalLog(calldepth, INFO, fmt.Sprint(args...)) +} + +// Panic and fatal + +func (p *PackageLogger) Panicf(format string, args ...interface{}) { + s := fmt.Sprintf(format, args...) + p.internalLog(calldepth, CRITICAL, s) + panic(s) +} + +func (p *PackageLogger) Panic(args ...interface{}) { + s := fmt.Sprint(args...) + p.internalLog(calldepth, CRITICAL, s) + panic(s) +} + +func (p *PackageLogger) Fatalf(format string, args ...interface{}) { + p.Logf(CRITICAL, format, args...) + os.Exit(1) +} + +func (p *PackageLogger) Fatal(args ...interface{}) { + s := fmt.Sprint(args...) + p.internalLog(calldepth, CRITICAL, s) + os.Exit(1) +} + +func (p *PackageLogger) Fatalln(args ...interface{}) { + s := fmt.Sprintln(args...) + p.internalLog(calldepth, CRITICAL, s) + os.Exit(1) +} + +// Error Functions + +func (p *PackageLogger) Errorf(format string, args ...interface{}) { + p.Logf(ERROR, format, args...) +} + +func (p *PackageLogger) Error(entries ...interface{}) { + p.internalLog(calldepth, ERROR, entries...) +} + +// Warning Functions + +func (p *PackageLogger) Warningf(format string, args ...interface{}) { + p.Logf(WARNING, format, args...) +} + +func (p *PackageLogger) Warning(entries ...interface{}) { + p.internalLog(calldepth, WARNING, entries...) +} + +// Notice Functions + +func (p *PackageLogger) Noticef(format string, args ...interface{}) { + p.Logf(NOTICE, format, args...) +} + +func (p *PackageLogger) Notice(entries ...interface{}) { + p.internalLog(calldepth, NOTICE, entries...) +} + +// Info Functions + +func (p *PackageLogger) Infof(format string, args ...interface{}) { + p.Logf(INFO, format, args...) +} + +func (p *PackageLogger) Info(entries ...interface{}) { + p.internalLog(calldepth, INFO, entries...) +} + +// Debug Functions + +func (p *PackageLogger) Debugf(format string, args ...interface{}) { + if p.level < DEBUG { + return + } + p.Logf(DEBUG, format, args...) +} + +func (p *PackageLogger) Debug(entries ...interface{}) { + if p.level < DEBUG { + return + } + p.internalLog(calldepth, DEBUG, entries...) +} + +// Trace Functions + +func (p *PackageLogger) Tracef(format string, args ...interface{}) { + if p.level < TRACE { + return + } + p.Logf(TRACE, format, args...) +} + +func (p *PackageLogger) Trace(entries ...interface{}) { + if p.level < TRACE { + return + } + p.internalLog(calldepth, TRACE, entries...) +} + +func (p *PackageLogger) Flush() { + logger.Lock() + defer logger.Unlock() + logger.formatter.Flush() +} diff --git a/_vendor/src/github.com/coreos/pkg/capnslog/syslog_formatter.go b/_vendor/src/github.com/coreos/pkg/capnslog/syslog_formatter.go new file mode 100644 index 0000000000000..4be5a1f2de395 --- /dev/null +++ b/_vendor/src/github.com/coreos/pkg/capnslog/syslog_formatter.go @@ -0,0 +1,65 @@ +// Copyright 2015 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +build !windows + +package capnslog + +import ( + "fmt" + "log/syslog" +) + +func NewSyslogFormatter(w *syslog.Writer) Formatter { + return &syslogFormatter{w} +} + +func NewDefaultSyslogFormatter(tag string) (Formatter, error) { + w, err := syslog.New(syslog.LOG_DEBUG, tag) + if err != nil { + return nil, err + } + return NewSyslogFormatter(w), nil +} + +type syslogFormatter struct { + w *syslog.Writer +} + +func (s *syslogFormatter) Format(pkg string, l LogLevel, _ int, entries ...interface{}) { + for _, entry := range entries { + str := fmt.Sprint(entry) + switch l { + case CRITICAL: + s.w.Crit(str) + case ERROR: + s.w.Err(str) + case WARNING: + s.w.Warning(str) + case NOTICE: + s.w.Notice(str) + case INFO: + s.w.Info(str) + case DEBUG: + s.w.Debug(str) + case TRACE: + s.w.Debug(str) + default: + panic("Unhandled loglevel") + } + } +} + +func (s *syslogFormatter) Flush() { +} diff --git a/_vendor/src/github.com/ngaut/systimemon/systime_mon.go b/_vendor/src/github.com/ngaut/systimemon/systime_mon.go deleted file mode 100644 index c5d08a738daad..0000000000000 --- a/_vendor/src/github.com/ngaut/systimemon/systime_mon.go +++ /dev/null @@ -1,24 +0,0 @@ -package systimemon - -import ( - "time" - - "github.com/ngaut/log" -) - -// Call systimeErrHandler if system time jump backward. -func StartMonitor(now func() time.Time, systimeErrHandler func()) { - log.Info("start system time monitor") - tick := time.NewTicker(100 * time.Millisecond) - defer tick.Stop() - for { - last := now() - select { - case <-tick.C: - if now().Sub(last) < 0 { - log.Errorf("system time jump backward, last:%v", last) - systimeErrHandler() - } - } - } -} diff --git a/_vendor/src/github.com/pingcap/pd/pkg/logutil/log.go b/_vendor/src/github.com/pingcap/pd/pkg/logutil/log.go new file mode 100644 index 0000000000000..42609921712a0 --- /dev/null +++ b/_vendor/src/github.com/pingcap/pd/pkg/logutil/log.go @@ -0,0 +1,240 @@ +// Copyright 2017 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package logutil + +import ( + "bytes" + "fmt" + "os" + "path" + "runtime" + "strings" + + log "github.com/Sirupsen/logrus" + "github.com/coreos/pkg/capnslog" + "github.com/juju/errors" + lumberjack "gopkg.in/natefinch/lumberjack.v2" +) + +const ( + defaultLogTimeFormat = "2006/01/02 15:04:05.000" + defaultLogMaxSize = 300 // MB + defaultLogFormat = "text" + defaultLogLevel = log.InfoLevel +) + +// 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 int `toml:"max-size" json:"max-size"` + // Max log keep days, default is never deleting. + MaxDays int `toml:"max-days" json:"max-days"` + // Maximum number of old log files to retain. + MaxBackups int `toml:"max-backups" json:"max-backups"` +} + +// 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"` +} + +// redirectFormatter will redirect etcd logs to logrus logs. +type redirectFormatter struct{} + +// Format implements capnslog.Formatter hook. +func (rf *redirectFormatter) Format(pkg string, level capnslog.LogLevel, depth int, entries ...interface{}) { + if pkg != "" { + pkg = fmt.Sprint(pkg, ": ") + } + + logStr := fmt.Sprint(pkg, entries) + + switch level { + case capnslog.CRITICAL: + log.Fatalf(logStr) + case capnslog.ERROR: + log.Errorf(logStr) + case capnslog.WARNING: + log.Warningf(logStr) + case capnslog.NOTICE: + log.Infof(logStr) + case capnslog.INFO: + log.Infof(logStr) + case capnslog.DEBUG, capnslog.TRACE: + log.Debugf(logStr) + } +} + +// Flush only for implementing Formatter. +func (rf *redirectFormatter) Flush() {} + +// isSKippedPackageName tests wether path name is on log library calling stack. +func isSkippedPackageName(name string) bool { + return strings.Contains(name, "github.com/Sirupsen/logrus") || + strings.Contains(name, "github.com/coreos/pkg/capnslog") +} + +// modifyHook injects file name and line pos into log entry. +type contextHook struct{} + +// Fire implements logrus.Hook interface +// https://github.com/sirupsen/logrus/issues/63 +func (hook *contextHook) Fire(entry *log.Entry) error { + pc := make([]uintptr, 3) + cnt := runtime.Callers(6, pc) + + for i := 0; i < cnt; i++ { + fu := runtime.FuncForPC(pc[i] - 1) + name := fu.Name() + if !isSkippedPackageName(name) { + file, line := fu.FileLine(pc[i] - 1) + entry.Data["file"] = path.Base(file) + entry.Data["line"] = line + break + } + } + return nil +} + +// Levels implements logrus.Hook interface. +func (hook *contextHook) Levels() []log.Level { + return log.AllLevels +} + +func stringToLogLevel(level string) log.Level { + switch strings.ToLower(level) { + case "fatal": + return log.FatalLevel + case "error": + return log.ErrorLevel + case "warn", "warning": + return log.WarnLevel + case "debug": + return log.DebugLevel + case "info": + return log.InfoLevel + } + return defaultLogLevel +} + +// textFormatter is for compatability with ngaut/log +type textFormatter struct { + DisableTimestamp bool +} + +// Format implements logrus.Formatter +func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) { + var b *bytes.Buffer + if entry.Buffer != nil { + b = entry.Buffer + } else { + b = &bytes.Buffer{} + } + if !f.DisableTimestamp { + fmt.Fprintf(b, "%s ", entry.Time.Format(defaultLogTimeFormat)) + } + if file, ok := entry.Data["file"]; ok { + fmt.Fprintf(b, "%s:%v:", file, entry.Data["line"]) + } + fmt.Fprintf(b, " [%s] %s", entry.Level.String(), entry.Message) + for k, v := range entry.Data { + if k != "file" && k != "line" { + fmt.Fprintf(b, " %v=%v", k, v) + } + } + b.WriteByte('\n') + return b.Bytes(), nil +} + +func stringToLogFormatter(format string, disableTimestamp bool) log.Formatter { + switch strings.ToLower(format) { + case "text": + return &textFormatter{ + DisableTimestamp: disableTimestamp, + } + case "json": + return &log.JSONFormatter{ + TimestampFormat: defaultLogTimeFormat, + DisableTimestamp: disableTimestamp, + } + case "console": + return &log.TextFormatter{ + FullTimestamp: true, + TimestampFormat: defaultLogTimeFormat, + DisableTimestamp: disableTimestamp, + } + default: + return &textFormatter{} + } +} + +// InitFileLog initializes file based logging options. +func InitFileLog(cfg *FileLogConfig) error { + if st, err := os.Stat(cfg.Filename); err == nil { + if st.IsDir() { + return errors.New("can't use directory as log file name") + } + } + if cfg.MaxSize == 0 { + cfg.MaxSize = defaultLogMaxSize + } + + // use lumberjack to logrotate + output := &lumberjack.Logger{ + Filename: cfg.Filename, + MaxSize: cfg.MaxSize, + MaxBackups: cfg.MaxBackups, + MaxAge: cfg.MaxDays, + LocalTime: true, + } + + log.SetOutput(output) + return nil +} + +// InitLogger initalizes PD's logger. +func InitLogger(cfg *LogConfig) error { + log.SetLevel(stringToLogLevel(cfg.Level)) + log.AddHook(&contextHook{}) + + if cfg.Format == "" { + cfg.Format = defaultLogFormat + } + log.SetFormatter(stringToLogFormatter(cfg.Format, cfg.DisableTimestamp)) + + // etcd log + capnslog.SetFormatter(&redirectFormatter{}) + + if len(cfg.File.Filename) == 0 { + return nil + } + + err := InitFileLog(&cfg.File) + if err != nil { + return errors.Trace(err) + } + + return nil +} diff --git a/_vendor/src/gopkg.in/natefinch/lumberjack.v2/LICENSE b/_vendor/src/gopkg.in/natefinch/lumberjack.v2/LICENSE new file mode 100644 index 0000000000000..c3d4cc307d291 --- /dev/null +++ b/_vendor/src/gopkg.in/natefinch/lumberjack.v2/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Nate Finch + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/_vendor/src/gopkg.in/natefinch/lumberjack.v2/chown.go b/_vendor/src/gopkg.in/natefinch/lumberjack.v2/chown.go new file mode 100644 index 0000000000000..11d0669723285 --- /dev/null +++ b/_vendor/src/gopkg.in/natefinch/lumberjack.v2/chown.go @@ -0,0 +1,11 @@ +// +build !linux + +package lumberjack + +import ( + "os" +) + +func chown(_ string, _ os.FileInfo) error { + return nil +} diff --git a/_vendor/src/gopkg.in/natefinch/lumberjack.v2/chown_linux.go b/_vendor/src/gopkg.in/natefinch/lumberjack.v2/chown_linux.go new file mode 100644 index 0000000000000..2758ec9cedd6e --- /dev/null +++ b/_vendor/src/gopkg.in/natefinch/lumberjack.v2/chown_linux.go @@ -0,0 +1,19 @@ +package lumberjack + +import ( + "os" + "syscall" +) + +// os_Chown is a var so we can mock it out during tests. +var os_Chown = os.Chown + +func chown(name string, info os.FileInfo) error { + f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode()) + if err != nil { + return err + } + f.Close() + stat := info.Sys().(*syscall.Stat_t) + return os_Chown(name, int(stat.Uid), int(stat.Gid)) +} diff --git a/_vendor/src/gopkg.in/natefinch/lumberjack.v2/lumberjack.go b/_vendor/src/gopkg.in/natefinch/lumberjack.v2/lumberjack.go new file mode 100644 index 0000000000000..b4bb54f828241 --- /dev/null +++ b/_vendor/src/gopkg.in/natefinch/lumberjack.v2/lumberjack.go @@ -0,0 +1,425 @@ +// Package lumberjack provides a rolling logger. +// +// Note that this is v2.0 of lumberjack, and should be imported using gopkg.in +// thusly: +// +// import "gopkg.in/natefinch/lumberjack.v2" +// +// The package name remains simply lumberjack, and the code resides at +// https://github.com/natefinch/lumberjack under the v2.0 branch. +// +// Lumberjack is intended to be one part of a logging infrastructure. +// It is not an all-in-one solution, but instead is a pluggable +// component at the bottom of the logging stack that simply controls the files +// to which logs are written. +// +// Lumberjack plays well with any logging package that can write to an +// io.Writer, including the standard library's log package. +// +// Lumberjack assumes that only one process is writing to the output files. +// Using the same lumberjack configuration from multiple processes on the same +// machine will result in improper behavior. +package lumberjack + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" + "sort" + "strings" + "sync" + "time" +) + +const ( + backupTimeFormat = "2006-01-02T15-04-05.000" + defaultMaxSize = 100 +) + +// ensure we always implement io.WriteCloser +var _ io.WriteCloser = (*Logger)(nil) + +// Logger is an io.WriteCloser that writes to the specified filename. +// +// Logger opens or creates the logfile on first Write. If the file exists and +// is less than MaxSize megabytes, lumberjack will open and append to that file. +// If the file exists and its size is >= MaxSize megabytes, the file is renamed +// by putting the current time in a timestamp in the name immediately before the +// file's extension (or the end of the filename if there's no extension). A new +// log file is then created using original filename. +// +// Whenever a write would cause the current log file exceed MaxSize megabytes, +// the current file is closed, renamed, and a new log file created with the +// original name. Thus, the filename you give Logger is always the "current" log +// file. +// +// Backups use the log file name given to Logger, in the form +// `name-timestamp.ext` where name is the filename without the extension, +// timestamp is the time at which the log was rotated formatted with the +// time.Time format of `2006-01-02T15-04-05.000` and the extension is the +// original extension. For example, if your Logger.Filename is +// `/var/log/foo/server.log`, a backup created at 6:30pm on Nov 11 2016 would +// use the filename `/var/log/foo/server-2016-11-04T18-30-00.000.log` +// +// Cleaning Up Old Log Files +// +// Whenever a new logfile gets created, old log files may be deleted. The most +// recent files according to the encoded timestamp will be retained, up to a +// number equal to MaxBackups (or all of them if MaxBackups is 0). Any files +// with an encoded timestamp older than MaxAge days are deleted, regardless of +// MaxBackups. Note that the time encoded in the timestamp is the rotation +// time, which may differ from the last time that file was written to. +// +// If MaxBackups and MaxAge are both 0, no old log files will be deleted. +type Logger struct { + // Filename is the file to write logs to. Backup log files will be retained + // in the same directory. It uses -lumberjack.log in + // os.TempDir() if empty. + Filename string `json:"filename" yaml:"filename"` + + // MaxSize is the maximum size in megabytes of the log file before it gets + // rotated. It defaults to 100 megabytes. + MaxSize int `json:"maxsize" yaml:"maxsize"` + + // MaxAge is the maximum number of days to retain old log files based on the + // timestamp encoded in their filename. Note that a day is defined as 24 + // hours and may not exactly correspond to calendar days due to daylight + // savings, leap seconds, etc. The default is not to remove old log files + // based on age. + MaxAge int `json:"maxage" yaml:"maxage"` + + // MaxBackups is the maximum number of old log files to retain. The default + // is to retain all old log files (though MaxAge may still cause them to get + // deleted.) + MaxBackups int `json:"maxbackups" yaml:"maxbackups"` + + // LocalTime determines if the time used for formatting the timestamps in + // backup files is the computer's local time. The default is to use UTC + // time. + LocalTime bool `json:"localtime" yaml:"localtime"` + + size int64 + file *os.File + mu sync.Mutex +} + +var ( + // currentTime exists so it can be mocked out by tests. + currentTime = time.Now + + // os_Stat exists so it can be mocked out by tests. + os_Stat = os.Stat + + // megabyte is the conversion factor between MaxSize and bytes. It is a + // variable so tests can mock it out and not need to write megabytes of data + // to disk. + megabyte = 1024 * 1024 +) + +// Write implements io.Writer. If a write would cause the log file to be larger +// than MaxSize, the file is closed, renamed to include a timestamp of the +// current time, and a new log file is created using the original log file name. +// If the length of the write is greater than MaxSize, an error is returned. +func (l *Logger) Write(p []byte) (n int, err error) { + l.mu.Lock() + defer l.mu.Unlock() + + writeLen := int64(len(p)) + if writeLen > l.max() { + return 0, fmt.Errorf( + "write length %d exceeds maximum file size %d", writeLen, l.max(), + ) + } + + if l.file == nil { + if err = l.openExistingOrNew(len(p)); err != nil { + return 0, err + } + } + + if l.size+writeLen > l.max() { + if err := l.rotate(); err != nil { + return 0, err + } + } + + n, err = l.file.Write(p) + l.size += int64(n) + + return n, err +} + +// Close implements io.Closer, and closes the current logfile. +func (l *Logger) Close() error { + l.mu.Lock() + defer l.mu.Unlock() + return l.close() +} + +// close closes the file if it is open. +func (l *Logger) close() error { + if l.file == nil { + return nil + } + err := l.file.Close() + l.file = nil + return err +} + +// Rotate causes Logger to close the existing log file and immediately create a +// new one. This is a helper function for applications that want to initiate +// rotations outside of the normal rotation rules, such as in response to +// SIGHUP. After rotating, this initiates a cleanup of old log files according +// to the normal rules. +func (l *Logger) Rotate() error { + l.mu.Lock() + defer l.mu.Unlock() + return l.rotate() +} + +// rotate closes the current file, moves it aside with a timestamp in the name, +// (if it exists), opens a new file with the original filename, and then runs +// cleanup. +func (l *Logger) rotate() error { + if err := l.close(); err != nil { + return err + } + + if err := l.openNew(); err != nil { + return err + } + return l.cleanup() +} + +// openNew opens a new log file for writing, moving any old log file out of the +// way. This methods assumes the file has already been closed. +func (l *Logger) openNew() error { + err := os.MkdirAll(l.dir(), 0744) + if err != nil { + return fmt.Errorf("can't make directories for new logfile: %s", err) + } + + name := l.filename() + mode := os.FileMode(0644) + info, err := os_Stat(name) + if err == nil { + // Copy the mode off the old logfile. + mode = info.Mode() + // move the existing file + newname := backupName(name, l.LocalTime) + if err := os.Rename(name, newname); err != nil { + return fmt.Errorf("can't rename log file: %s", err) + } + + // this is a no-op anywhere but linux + if err := chown(name, info); err != nil { + return err + } + } + + // we use truncate here because this should only get called when we've moved + // the file ourselves. if someone else creates the file in the meantime, + // just wipe out the contents. + f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode) + if err != nil { + return fmt.Errorf("can't open new logfile: %s", err) + } + l.file = f + l.size = 0 + return nil +} + +// backupName creates a new filename from the given name, inserting a timestamp +// between the filename and the extension, using the local time if requested +// (otherwise UTC). +func backupName(name string, local bool) string { + dir := filepath.Dir(name) + filename := filepath.Base(name) + ext := filepath.Ext(filename) + prefix := filename[:len(filename)-len(ext)] + t := currentTime() + if !local { + t = t.UTC() + } + + timestamp := t.Format(backupTimeFormat) + return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext)) +} + +// openExistingOrNew opens the logfile if it exists and if the current write +// would not put it over MaxSize. If there is no such file or the write would +// put it over the MaxSize, a new file is created. +func (l *Logger) openExistingOrNew(writeLen int) error { + filename := l.filename() + info, err := os_Stat(filename) + if os.IsNotExist(err) { + return l.openNew() + } + if err != nil { + return fmt.Errorf("error getting log file info: %s", err) + } + + if info.Size()+int64(writeLen) >= l.max() { + return l.rotate() + } + + file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + // if we fail to open the old log file for some reason, just ignore + // it and open a new log file. + return l.openNew() + } + l.file = file + l.size = info.Size() + return nil +} + +// genFilename generates the name of the logfile from the current time. +func (l *Logger) filename() string { + if l.Filename != "" { + return l.Filename + } + name := filepath.Base(os.Args[0]) + "-lumberjack.log" + return filepath.Join(os.TempDir(), name) +} + +// cleanup deletes old log files, keeping at most l.MaxBackups files, as long as +// none of them are older than MaxAge. +func (l *Logger) cleanup() error { + if l.MaxBackups == 0 && l.MaxAge == 0 { + return nil + } + + files, err := l.oldLogFiles() + if err != nil { + return err + } + + var deletes []logInfo + + if l.MaxBackups > 0 && l.MaxBackups < len(files) { + deletes = files[l.MaxBackups:] + files = files[:l.MaxBackups] + } + if l.MaxAge > 0 { + diff := time.Duration(int64(24*time.Hour) * int64(l.MaxAge)) + + cutoff := currentTime().Add(-1 * diff) + + for _, f := range files { + if f.timestamp.Before(cutoff) { + deletes = append(deletes, f) + } + } + } + + if len(deletes) == 0 { + return nil + } + + go deleteAll(l.dir(), deletes) + + return nil +} + +func deleteAll(dir string, files []logInfo) { + // remove files on a separate goroutine + for _, f := range files { + // what am I going to do, log this? + _ = os.Remove(filepath.Join(dir, f.Name())) + } +} + +// oldLogFiles returns the list of backup log files stored in the same +// directory as the current log file, sorted by ModTime +func (l *Logger) oldLogFiles() ([]logInfo, error) { + files, err := ioutil.ReadDir(l.dir()) + if err != nil { + return nil, fmt.Errorf("can't read log file directory: %s", err) + } + logFiles := []logInfo{} + + prefix, ext := l.prefixAndExt() + + for _, f := range files { + if f.IsDir() { + continue + } + name := l.timeFromName(f.Name(), prefix, ext) + if name == "" { + continue + } + t, err := time.Parse(backupTimeFormat, name) + if err == nil { + logFiles = append(logFiles, logInfo{t, f}) + } + // error parsing means that the suffix at the end was not generated + // by lumberjack, and therefore it's not a backup file. + } + + sort.Sort(byFormatTime(logFiles)) + + return logFiles, nil +} + +// timeFromName extracts the formatted time from the filename by stripping off +// the filename's prefix and extension. This prevents someone's filename from +// confusing time.parse. +func (l *Logger) timeFromName(filename, prefix, ext string) string { + if !strings.HasPrefix(filename, prefix) { + return "" + } + filename = filename[len(prefix):] + + if !strings.HasSuffix(filename, ext) { + return "" + } + filename = filename[:len(filename)-len(ext)] + return filename +} + +// max returns the maximum size in bytes of log files before rolling. +func (l *Logger) max() int64 { + if l.MaxSize == 0 { + return int64(defaultMaxSize * megabyte) + } + return int64(l.MaxSize) * int64(megabyte) +} + +// dir returns the directory for the current filename. +func (l *Logger) dir() string { + return filepath.Dir(l.filename()) +} + +// prefixAndExt returns the filename part and extension part from the Logger's +// filename. +func (l *Logger) prefixAndExt() (prefix, ext string) { + filename := filepath.Base(l.filename()) + ext = filepath.Ext(filename) + prefix = filename[:len(filename)-len(ext)] + "-" + return prefix, ext +} + +// logInfo is a convenience struct to return the filename and its embedded +// timestamp. +type logInfo struct { + timestamp time.Time + os.FileInfo +} + +// byFormatTime sorts by newest time formatted in the name. +type byFormatTime []logInfo + +func (b byFormatTime) Less(i, j int) bool { + return b[i].timestamp.After(b[j].timestamp) +} + +func (b byFormatTime) Swap(i, j int) { + b[i], b[j] = b[j], b[i] +} + +func (b byFormatTime) Len() int { + return len(b) +} diff --git a/bench_test.go b/bench_test.go index cdd861552d2d1..5d51a04b7058e 100644 --- a/bench_test.go +++ b/bench_test.go @@ -19,7 +19,7 @@ import ( "testing" "time" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/ast" ) @@ -31,7 +31,7 @@ func prepareBenchSession() Session { if err != nil { log.Fatal(err) } - log.SetLevel(log.LOG_LEVEL_ERROR) + log.SetLevel(log.ErrorLevel) se, err := CreateSession(store) if err != nil { log.Fatal(err) diff --git a/bootstrap.go b/bootstrap.go index 6c9f448aad42b..9666c9ae0fbb0 100644 --- a/bootstrap.go +++ b/bootstrap.go @@ -25,8 +25,8 @@ import ( "strings" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/mysql" diff --git a/cmd/benchdb/main.go b/cmd/benchdb/main.go index f3a15bf47ebe4..3c6ea783ca5a3 100644 --- a/cmd/benchdb/main.go +++ b/cmd/benchdb/main.go @@ -21,7 +21,8 @@ import ( "strings" "time" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/tidb" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/tikv" @@ -50,7 +51,9 @@ var ( func main() { flag.Parse() flag.PrintDefaults() - log.SetLevelByString(*logLevel) + logutil.InitLogger(&logutil.LogConfig{ + Level: *logLevel, + }) tidb.RegisterStore("tikv", tikv.Driver{}) ut := newBenchDB() works := strings.Split(*runJobs, "|") diff --git a/cmd/benchfilesort/main.go b/cmd/benchfilesort/main.go index 0e63e87c07551..a9df9e13ae07c 100644 --- a/cmd/benchfilesort/main.go +++ b/cmd/benchfilesort/main.go @@ -24,8 +24,9 @@ import ( "runtime/pprof" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/util/codec" "github.com/pingcap/tidb/util/filesort" @@ -392,7 +393,9 @@ func driveRunCmd() { } func init() { - log.SetLevelByString(logLevel) + logutil.InitLogger(&logutil.LogConfig{ + Level: logLevel, + }) cwd, err := os.Getwd() if err != nil { diff --git a/cmd/benchkv/main.go b/cmd/benchkv/main.go index df166ac82c598..f6ad63750da99 100644 --- a/cmd/benchkv/main.go +++ b/cmd/benchkv/main.go @@ -24,7 +24,7 @@ import ( _ "github.com/go-sql-driver/mysql" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/tikv" "github.com/prometheus/client_golang/prometheus" @@ -113,7 +113,7 @@ func batchRW(value []byte) { func main() { flag.Parse() - log.SetLevelByString("error") + log.SetLevel(log.ErrorLevel) Init() value := make([]byte, *valueSize) diff --git a/cmd/benchraw/main.go b/cmd/benchraw/main.go index 958dff5daf3a4..48e7dbf078ab4 100644 --- a/cmd/benchraw/main.go +++ b/cmd/benchraw/main.go @@ -22,8 +22,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/store/tikv" ) @@ -63,7 +63,7 @@ func batchRawPut(value []byte) { func main() { flag.Parse() - log.SetLevelByString("warn") + log.SetLevel(log.WarnLevel) go http.ListenAndServe(":9191", nil) value := make([]byte, *valueSize) diff --git a/ddl/column.go b/ddl/column.go index a88b74b19234a..1cf142ecb5e87 100644 --- a/ddl/column.go +++ b/ddl/column.go @@ -16,8 +16,8 @@ package ddl import ( "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/infoschema" diff --git a/ddl/ddl.go b/ddl/ddl.go index b224ce1846af0..e4131a6bbc575 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -22,9 +22,9 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/ngaut/pools" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index 051b86b488ecc..df070e8dc6758 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -20,8 +20,8 @@ import ( "time" "github.com/coreos/etcd/clientv3" - "github.com/ngaut/log" . "github.com/pingcap/check" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/infoschema" @@ -146,6 +146,7 @@ func testDropIndex(c *C, ctx context.Context, d *ddl, dbInfo *model.DBInfo, tblI func init() { logLevel := os.Getenv("log_level") - log.SetLevelByString(logLevel) - log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile) + logutil.InitLogger(&logutil.LogConfig{ + Level: logLevel, + }) } diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index ed1b2b3beb048..ce94af8ad0383 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -16,8 +16,8 @@ package ddl import ( "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" diff --git a/ddl/delete_range.go b/ddl/delete_range.go index 376a9b542352e..413cdd78f4bd1 100644 --- a/ddl/delete_range.go +++ b/ddl/delete_range.go @@ -19,8 +19,8 @@ import ( "fmt" "math" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/ngaut/pools" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/kv" diff --git a/ddl/index.go b/ddl/index.go index f78eaea871ed6..b694962ec128f 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -18,8 +18,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" diff --git a/ddl/reorg.go b/ddl/reorg.go index e4342705e1122..21adc17dc375b 100644 --- a/ddl/reorg.go +++ b/ddl/reorg.go @@ -17,8 +17,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" diff --git a/ddl/syncer.go b/ddl/syncer.go index fa265d200ed7f..2bb924f021be5 100644 --- a/ddl/syncer.go +++ b/ddl/syncer.go @@ -19,10 +19,10 @@ import ( "strconv" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/clientv3/concurrency" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/owner" goctx "golang.org/x/net/context" ) diff --git a/domain/domain.go b/domain/domain.go index 1fb59d39bdd2f..6f50406d849e8 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -19,9 +19,9 @@ import ( "time" "unsafe" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/ngaut/pools" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/ddl" diff --git a/domain/schema_validator.go b/domain/schema_validator.go index a493909706506..0b77efc107f98 100644 --- a/domain/schema_validator.go +++ b/domain/schema_validator.go @@ -17,7 +17,7 @@ import ( "sync" "time" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" ) // SchemaValidator is the interface for checking the validity of schema version. diff --git a/executor/adapter.go b/executor/adapter.go index 2fc35b453ae2a..7cad27523cfcd 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -18,8 +18,8 @@ import ( "math" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/context" diff --git a/executor/analyze.go b/executor/analyze.go index 4416a8bab2042..d19d928bfeac1 100644 --- a/executor/analyze.go +++ b/executor/analyze.go @@ -17,8 +17,8 @@ import ( "strconv" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/model" diff --git a/executor/compiler.go b/executor/compiler.go index 872eafe89ebca..39040c6843e0d 100644 --- a/executor/compiler.go +++ b/executor/compiler.go @@ -14,8 +14,8 @@ package executor import ( + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/infoschema" diff --git a/executor/distsql.go b/executor/distsql.go index bc165e24c5558..b69f72e8b26b9 100644 --- a/executor/distsql.go +++ b/executor/distsql.go @@ -20,8 +20,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/distsql" diff --git a/executor/executor_test.go b/executor/executor_test.go index fe8e70fabf343..f44c442de02b2 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -23,9 +23,9 @@ import ( "time" "github.com/juju/errors" - "github.com/ngaut/log" . "github.com/pingcap/check" pb "github.com/pingcap/kvproto/pkg/kvrpcpb" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/tidb" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/domain" @@ -91,7 +91,9 @@ func (s *testSuite) SetUpSuite(c *C) { _, err := tidb.BootstrapSession(s.store) c.Assert(err, IsNil) logLevel := os.Getenv("log_level") - log.SetLevelByString(logLevel) + logutil.InitLogger(&logutil.LogConfig{ + Level: logLevel, + }) } func (s *testSuite) TearDownSuite(c *C) { diff --git a/executor/metrics.go b/executor/metrics.go index 3905bbae5f961..b22988b3528a5 100644 --- a/executor/metrics.go +++ b/executor/metrics.go @@ -17,7 +17,7 @@ import ( "fmt" "strings" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/model" "github.com/pingcap/tidb/mysql" diff --git a/executor/new_distsql.go b/executor/new_distsql.go index 8b71ab30d0089..275899cc91ab9 100644 --- a/executor/new_distsql.go +++ b/executor/new_distsql.go @@ -18,8 +18,8 @@ import ( "sync" "sync/atomic" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/distsql" "github.com/pingcap/tidb/expression" diff --git a/executor/set.go b/executor/set.go index 1f6c3e31228d1..39e6c3c7fd2f4 100644 --- a/executor/set.go +++ b/executor/set.go @@ -18,8 +18,8 @@ import ( "strings" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/expression" diff --git a/executor/simple.go b/executor/simple.go index 1c7d9497a6e70..82404a2ef86ae 100644 --- a/executor/simple.go +++ b/executor/simple.go @@ -17,8 +17,8 @@ import ( "fmt" "strings" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/ddl" diff --git a/executor/write.go b/executor/write.go index f69855780f972..3db8c8ce2f856 100644 --- a/executor/write.go +++ b/executor/write.go @@ -18,8 +18,8 @@ import ( "fmt" "strings" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/expression" diff --git a/expression/aggregation.go b/expression/aggregation.go index be284368a57aa..2327f11089de6 100644 --- a/expression/aggregation.go +++ b/expression/aggregation.go @@ -19,8 +19,8 @@ import ( "fmt" "strings" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/mysql" diff --git a/expression/builtin_string.go b/expression/builtin_string.go index 5df453a213b5d..a45d9c141bbd5 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -27,8 +27,8 @@ import ( "strings" "unicode/utf8" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/mysql" diff --git a/expression/builtin_time.go b/expression/builtin_time.go index 4ead905fd4cdf..a916006d02c72 100644 --- a/expression/builtin_time.go +++ b/expression/builtin_time.go @@ -24,9 +24,9 @@ import ( "strings" "time" + log "github.com/Sirupsen/logrus" "github.com/cznic/mathutil" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/mysql" diff --git a/expression/column.go b/expression/column.go index e6f60dbf0c858..b360fad6d6fa4 100644 --- a/expression/column.go +++ b/expression/column.go @@ -17,8 +17,8 @@ import ( "bytes" "fmt" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/model" "github.com/pingcap/tidb/sessionctx/variable" diff --git a/expression/constant_fold.go b/expression/constant_fold.go index 00f4a0d7b410d..da1f531fc6990 100644 --- a/expression/constant_fold.go +++ b/expression/constant_fold.go @@ -14,7 +14,7 @@ package expression import ( - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" ) // FoldConstant does constant folding optimization on an expression. diff --git a/expression/constant_propagation.go b/expression/constant_propagation.go index affa4440e34be..bbb564f5d9645 100644 --- a/expression/constant_propagation.go +++ b/expression/constant_propagation.go @@ -14,7 +14,7 @@ package expression import ( - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/mysql" diff --git a/expression/expr_to_pb.go b/expression/expr_to_pb.go index 06d2870be5764..ea8bf99e0e2f3 100644 --- a/expression/expr_to_pb.go +++ b/expression/expr_to_pb.go @@ -16,7 +16,7 @@ package expression import ( "time" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/mysql" diff --git a/glide.lock b/glide.lock index edce9b11fd5e9..6fe242948f420 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 572342fd3964f6673d6c6c6832d66eb7a8ead6469e3a59404a2e8cb99be54e03 -updated: 2017-09-07T11:17:12.175998262+08:00 +hash: 12d6d4f999def45ff7abfba9826f38d0931b5611050b74b25c2ff25daa89d093 +updated: 2017-09-06T16:45:49.726923497+08:00 imports: - name: bitbucket.org/ww/goautoneg version: 75cd24fc2f2c2a2088577d12123ddee5f54e0675 @@ -23,6 +23,14 @@ imports: - mvcc/mvccpb - pkg/monotime - pkg/tlsutil +- name: github.com/coreos/go-systemd + version: 48702e0da86bd25e76cfef347e2adeb434a0d0a6 + subpackages: + - journal +- name: github.com/coreos/pkg + version: 3ac0863d7acf3bc44daf49afef8919af12f704ef + subpackages: + - capnslog - name: github.com/cznic/golex version: da5a7153a51074477ecac5c45a7e5182a0c72448 subpackages: @@ -83,14 +91,10 @@ imports: - pbutil - name: github.com/ngaut/deadline version: fae8f9dfd7048de16575b9d4c255278e38c28a4f -- name: github.com/ngaut/log - version: cec23d3e10b016363780d894a0eb732a12c06e02 - name: github.com/ngaut/pools version: 6352e005618615ffaf1cb1c6622b8e91435751fe - name: github.com/ngaut/sync2 version: 7a24ed77b2efb460c1468b7dc917821c66e80e55 -- name: github.com/ngaut/systimemon - version: c2ca1c75c6af5556eb6ce67994120adf12f2ccef - name: github.com/petar/GoLLRB version: 53be0d36a84c2a886ca057d34b6aa4468df9ccb4 subpackages: @@ -129,6 +133,7 @@ imports: version: 38eb8a2e70dd39668a62d24a79f30596f204df14 subpackages: - pd-client + - pkg/logutil - name: github.com/pingcap/tipb version: 0edd80335ab0c044ddcd9de57b63026ab7a209fa subpackages: @@ -223,4 +228,6 @@ imports: - status - tap - transport +- name: gopkg.in/natefinch/lumberjack.v2 + version: dd45e6a67c53f673bb49ca8a001fd3a63ceb640e testImports: [] diff --git a/glide.yaml b/glide.yaml index aad5167413e3d..cf5266362c4f2 100644 --- a/glide.yaml +++ b/glide.yaml @@ -37,8 +37,6 @@ import: version: b2c7a7da5b2995941048f60146e67702a292e468 - package: github.com/ngaut/deadline version: fae8f9dfd7048de16575b9d4c255278e38c28a4f -- package: github.com/ngaut/log - version: cec23d3e10b016363780d894a0eb732a12c06e02 - package: github.com/ngaut/pools version: 6352e005618615ffaf1cb1c6622b8e91435751fe - package: github.com/ngaut/sync2 @@ -66,6 +64,8 @@ import: - leveldb/storage - leveldb/table - leveldb/util +- package: github.com/Sirupsen/logrus + version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f - package: github.com/twinj/uuid version: 70cac2bcd273ef6a371bb96cde363d28b68734c3 - package: golang.org/x/net @@ -91,8 +91,6 @@ import: - naming - peer - transport -- package: github.com/ngaut/systimemon - version: c2ca1c75c6af5556eb6ce67994120adf12f2ccef - package: github.com/cznic/mathutil version: 78ad7f262603437f0ecfebc835d80094f89c8f54 - package: github.com/cznic/parser diff --git a/inspectkv/inspectkv.go b/inspectkv/inspectkv.go index 6be01c0176156..21498d30b5c3c 100644 --- a/inspectkv/inspectkv.go +++ b/inspectkv/inspectkv.go @@ -18,8 +18,8 @@ import ( "reflect" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/model" diff --git a/kv/txn.go b/kv/txn.go index 358b38c042028..59b74e9f27ca3 100644 --- a/kv/txn.go +++ b/kv/txn.go @@ -18,8 +18,8 @@ import ( "math/rand" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" ) // RunInNewTxn will run the f in a new transaction environment. diff --git a/kv/union_iter.go b/kv/union_iter.go index 99b1d5d362ea0..6b50b48c4d9d2 100644 --- a/kv/union_iter.go +++ b/kv/union_iter.go @@ -14,8 +14,8 @@ package kv import ( + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" ) // UnionIter is the iterator on an UnionStore. diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index 7ddb06e1a38b5..6376da208e554 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -18,8 +18,8 @@ import ( "sync" "sync/atomic" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/terror" diff --git a/owner/manager.go b/owner/manager.go index 2720d04e5e88b..3da570c1e8b47 100644 --- a/owner/manager.go +++ b/owner/manager.go @@ -21,12 +21,12 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/clientv3/concurrency" "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" "github.com/coreos/etcd/mvcc/mvccpb" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/terror" goctx "golang.org/x/net/context" ) diff --git a/perfschema/statement.go b/perfschema/statement.go index 1e261a72818f2..2b655d3209d86 100644 --- a/perfschema/statement.go +++ b/perfschema/statement.go @@ -20,8 +20,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/util/types" ) diff --git a/plan/column_pruning.go b/plan/column_pruning.go index 59a6b0d085cab..f0a36bff95f9c 100644 --- a/plan/column_pruning.go +++ b/plan/column_pruning.go @@ -14,7 +14,7 @@ package plan import ( - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/expression" diff --git a/plan/join_reorder.go b/plan/join_reorder.go index 9667d3f061d12..261e462fd6123 100644 --- a/plan/join_reorder.go +++ b/plan/join_reorder.go @@ -16,7 +16,7 @@ package plan import ( "sort" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/expression" diff --git a/plan/new_physical_plan_builder.go b/plan/new_physical_plan_builder.go index 34e73c3932da7..ef0b893761cd4 100644 --- a/plan/new_physical_plan_builder.go +++ b/plan/new_physical_plan_builder.go @@ -16,8 +16,8 @@ package plan import ( "math" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/expression" diff --git a/plan/physical_plan_builder.go b/plan/physical_plan_builder.go index f1ea08bf5cc3b..fa1fb0429ed0b 100644 --- a/plan/physical_plan_builder.go +++ b/plan/physical_plan_builder.go @@ -16,8 +16,8 @@ package plan import ( "math" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/infoschema" diff --git a/plan/stats.go b/plan/stats.go index 904bff3190497..6b82ec5937cd9 100644 --- a/plan/stats.go +++ b/plan/stats.go @@ -16,7 +16,7 @@ package plan import ( "math" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/expression" ) diff --git a/privilege/privileges/cache.go b/privilege/privileges/cache.go index 5fca3cfc44cf6..9b68e2c70830a 100644 --- a/privilege/privileges/cache.go +++ b/privilege/privileges/cache.go @@ -19,8 +19,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/mysql" diff --git a/privilege/privileges/privileges.go b/privilege/privileges/privileges.go index 1fd2b58e1b11a..2b89a6240a691 100644 --- a/privilege/privileges/privileges.go +++ b/privilege/privileges/privileges.go @@ -16,7 +16,7 @@ package privileges import ( "strings" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/privilege" diff --git a/server/conn.go b/server/conn.go index 5241f887e1b0f..07fca410e4ba2 100644 --- a/server/conn.go +++ b/server/conn.go @@ -46,8 +46,8 @@ import ( "strings" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/executor" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/mysql" diff --git a/server/http_status.go b/server/http_status.go index 026bb4fcc459b..6675b4b68d7f3 100644 --- a/server/http_status.go +++ b/server/http_status.go @@ -18,8 +18,8 @@ import ( "net/http" "sync" + log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" - "github.com/ngaut/log" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/util/printer" "github.com/prometheus/client_golang/prometheus" diff --git a/server/region_handler.go b/server/region_handler.go index 498c16ff6599f..da1237fd4e28a 100644 --- a/server/region_handler.go +++ b/server/region_handler.go @@ -21,9 +21,9 @@ import ( "strconv" "time" + log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/tidb" diff --git a/server/server.go b/server/server.go index 7f39dc063f226..c6ce181dbff40 100644 --- a/server/server.go +++ b/server/server.go @@ -40,8 +40,8 @@ import ( // For pprof _ "net/http/pprof" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/sessionctx/variable" diff --git a/server/server_test.go b/server/server_test.go index 8e61595c7a02d..2d1a6aec9f928 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -25,8 +25,8 @@ import ( "testing" "time" + log "github.com/Sirupsen/logrus" "github.com/go-sql-driver/mysql" - "github.com/ngaut/log" . "github.com/pingcap/check" "github.com/pingcap/tidb/executor" tmysql "github.com/pingcap/tidb/mysql" diff --git a/server/tidb_test.go b/server/tidb_test.go index 52a10b8265824..77a084003a3f2 100644 --- a/server/tidb_test.go +++ b/server/tidb_test.go @@ -26,9 +26,9 @@ import ( "os" "time" + log "github.com/Sirupsen/logrus" "github.com/go-sql-driver/mysql" "github.com/juju/errors" - "github.com/ngaut/log" . "github.com/pingcap/check" "github.com/pingcap/tidb" "github.com/pingcap/tidb/config" @@ -43,7 +43,7 @@ var suite = new(TidbTestSuite) var _ = Suite(suite) func (ts *TidbTestSuite) SetUpSuite(c *C) { - log.SetLevelByString("error") + log.SetLevel(log.ErrorLevel) store, err := tidb.NewStore("memory:///tmp/tidb") c.Assert(err, IsNil) _, err = tidb.BootstrapSession(store) diff --git a/session.go b/session.go index 9db7f38f3cdd0..3b53c58b8e799 100644 --- a/session.go +++ b/session.go @@ -27,8 +27,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/ngaut/pools" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" diff --git a/sessionctx/binloginfo/binloginfo.go b/sessionctx/binloginfo/binloginfo.go index f623fb43f9b79..34872d47471bb 100644 --- a/sessionctx/binloginfo/binloginfo.go +++ b/sessionctx/binloginfo/binloginfo.go @@ -17,8 +17,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/terror" diff --git a/sessionctx/binloginfo/binloginfo_test.go b/sessionctx/binloginfo/binloginfo_test.go index 5de07860d2efa..6d990a3d56aa6 100644 --- a/sessionctx/binloginfo/binloginfo_test.go +++ b/sessionctx/binloginfo/binloginfo_test.go @@ -21,8 +21,8 @@ import ( "testing" "time" - "github.com/ngaut/log" . "github.com/pingcap/check" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/tidb" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/ddl" @@ -74,7 +74,9 @@ type testBinlogSuite struct { func (s *testBinlogSuite) SetUpSuite(c *C) { logLevel := os.Getenv("log_level") - log.SetLevelByString(logLevel) + logutil.InitLogger(&logutil.LogConfig{ + Level: logLevel, + }) store, err := tikv.NewMockTikvStore() c.Assert(err, IsNil) s.store = store diff --git a/statistics/ddl.go b/statistics/ddl.go index 856d1c2185af2..14758eb085e16 100644 --- a/statistics/ddl.go +++ b/statistics/ddl.go @@ -16,8 +16,8 @@ package statistics import ( "fmt" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/model" diff --git a/statistics/handle.go b/statistics/handle.go index fa8c2fe90f9af..7159548effc56 100644 --- a/statistics/handle.go +++ b/statistics/handle.go @@ -18,8 +18,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/infoschema" diff --git a/statistics/table.go b/statistics/table.go index 7ca1a61ef9991..dc68d00d9a0e6 100644 --- a/statistics/table.go +++ b/statistics/table.go @@ -18,8 +18,8 @@ import ( "math" "strings" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/model" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/util/sqlexec" diff --git a/statistics/update.go b/statistics/update.go index b80bde4c9ef2f..2f076743f1285 100644 --- a/statistics/update.go +++ b/statistics/update.go @@ -18,8 +18,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/model" "github.com/pingcap/tidb/sessionctx/variable" diff --git a/store/localstore/compactor.go b/store/localstore/compactor.go index 03370eacac102..ed69fa24a6767 100644 --- a/store/localstore/compactor.go +++ b/store/localstore/compactor.go @@ -17,8 +17,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/localstore/engine" "github.com/pingcap/tidb/terror" diff --git a/store/localstore/kv.go b/store/localstore/kv.go index 844137b7d83b5..73a643153fc3b 100644 --- a/store/localstore/kv.go +++ b/store/localstore/kv.go @@ -20,8 +20,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/localstore/engine" "github.com/pingcap/tidb/store/tikv/oracle" diff --git a/store/localstore/local_version_provider.go b/store/localstore/local_version_provider.go index 6a2cdb8cd4d20..45ea74aae64b5 100644 --- a/store/localstore/local_version_provider.go +++ b/store/localstore/local_version_provider.go @@ -5,7 +5,7 @@ import ( "sync" "time" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/kv" ) diff --git a/store/localstore/txn.go b/store/localstore/txn.go index ddbc2ed738168..586a2df12c727 100644 --- a/store/localstore/txn.go +++ b/store/localstore/txn.go @@ -16,8 +16,8 @@ package localstore import ( "fmt" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/kv" ) diff --git a/store/store_test.go b/store/store_test.go index 1e67ee2f8e06d..c609fcd73000d 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -22,8 +22,8 @@ import ( "sync/atomic" "testing" - "github.com/ngaut/log" . "github.com/pingcap/check" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/tidb" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/localstore" @@ -62,7 +62,9 @@ func (s *testKVSuite) SetUpSuite(c *C) { cacheS, _ := tidb.NewStore(fmt.Sprintf("%s://%s", *testStore, *testStorePath)) c.Assert(cacheS, Equals, store) logLevel := os.Getenv("log_level") - log.SetLevelByString(logLevel) + logutil.InitLogger(&logutil.LogConfig{ + Level: logLevel, + }) } func (s *testKVSuite) TearDownSuite(c *C) { diff --git a/store/tikv/2pc.go b/store/tikv/2pc.go index c821a8fd764dd..b68a5f69b40af 100644 --- a/store/tikv/2pc.go +++ b/store/tikv/2pc.go @@ -20,9 +20,9 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/pkg/monotime" "github.com/juju/errors" - "github.com/ngaut/log" pb "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/sessionctx/binloginfo" diff --git a/store/tikv/backoff.go b/store/tikv/backoff.go index 25e8b734fdd4e..0ab1f1a368162 100644 --- a/store/tikv/backoff.go +++ b/store/tikv/backoff.go @@ -19,8 +19,8 @@ import ( "math/rand" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" goctx "golang.org/x/net/context" ) @@ -179,7 +179,7 @@ func (b *Backoffer) Backoff(typ backoffType, err error) error { errMsg := fmt.Sprintf("backoffer.maxSleep %dms is exceeded, errors:", b.maxSleep) for i, err := range b.errors { // Print only last 3 errors for non-DEBUG log levels. - if log.GetLogLevel() >= log.LOG_LEVEL_DEBUG || i >= len(b.errors)-3 { + if log.GetLevel() == log.DebugLevel || i >= len(b.errors)-3 { errMsg += "\n" + err.Error() } } diff --git a/store/tikv/coprocessor.go b/store/tikv/coprocessor.go index bfb26b91bc777..00776eee67634 100644 --- a/store/tikv/coprocessor.go +++ b/store/tikv/coprocessor.go @@ -19,8 +19,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/coprocessor" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/tikv/tikvrpc" diff --git a/store/tikv/gc_worker.go b/store/tikv/gc_worker.go index 8a6affb20d6e6..0b8c2ab8ef756 100644 --- a/store/tikv/gc_worker.go +++ b/store/tikv/gc_worker.go @@ -21,8 +21,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb" "github.com/pingcap/tidb/ddl" diff --git a/store/tikv/kv.go b/store/tikv/kv.go index 95b6cfccdde0c..203b6a4fe39d3 100644 --- a/store/tikv/kv.go +++ b/store/tikv/kv.go @@ -21,8 +21,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/pd/pd-client" "github.com/pingcap/tidb/kv" diff --git a/store/tikv/lock_resolver.go b/store/tikv/lock_resolver.go index f43199626b852..94ae162337f57 100644 --- a/store/tikv/lock_resolver.go +++ b/store/tikv/lock_resolver.go @@ -18,8 +18,8 @@ import ( "fmt" "sync" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/pd/pd-client" "github.com/pingcap/tidb/store/tikv/tikvrpc" diff --git a/store/tikv/mock-tikv/mvcc_leveldb.go b/store/tikv/mock-tikv/mvcc_leveldb.go index 8595c16978688..b4c931f6d70ac 100644 --- a/store/tikv/mock-tikv/mvcc_leveldb.go +++ b/store/tikv/mock-tikv/mvcc_leveldb.go @@ -18,8 +18,8 @@ import ( "math" "sync" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/goleveldb/leveldb" "github.com/pingcap/goleveldb/leveldb/iterator" "github.com/pingcap/goleveldb/leveldb/opt" diff --git a/store/tikv/oracle/oracles/pd.go b/store/tikv/oracle/oracles/pd.go index 248393667ccef..262740166cbb2 100644 --- a/store/tikv/oracle/oracles/pd.go +++ b/store/tikv/oracle/oracles/pd.go @@ -17,8 +17,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/pd/pd-client" "github.com/pingcap/tidb/store/tikv/oracle" goctx "golang.org/x/net/context" diff --git a/store/tikv/region_cache.go b/store/tikv/region_cache.go index 16f1b1464b380..a168df77c83ec 100644 --- a/store/tikv/region_cache.go +++ b/store/tikv/region_cache.go @@ -17,8 +17,8 @@ import ( "bytes" "sync" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/petar/GoLLRB/llrb" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/kvproto/pkg/metapb" diff --git a/store/tikv/region_request.go b/store/tikv/region_request.go index eb3bac4d11312..e1d18a91a402d 100644 --- a/store/tikv/region_request.go +++ b/store/tikv/region_request.go @@ -16,8 +16,8 @@ package tikv import ( "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/errorpb" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb/kv" diff --git a/store/tikv/scan.go b/store/tikv/scan.go index de722cfc3d3d9..45778011adfa3 100644 --- a/store/tikv/scan.go +++ b/store/tikv/scan.go @@ -14,8 +14,8 @@ package tikv import ( + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" pb "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/tikv/tikvrpc" diff --git a/store/tikv/snapshot.go b/store/tikv/snapshot.go index 0145f8e6a6093..36d5cc6fa2803 100644 --- a/store/tikv/snapshot.go +++ b/store/tikv/snapshot.go @@ -18,8 +18,8 @@ import ( "time" "unsafe" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" pb "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/tikv/tikvrpc" diff --git a/store/tikv/snapshot_test.go b/store/tikv/snapshot_test.go index 0ffbf00464a88..b3d323585772d 100644 --- a/store/tikv/snapshot_test.go +++ b/store/tikv/snapshot_test.go @@ -17,7 +17,7 @@ import ( "fmt" "time" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" . "github.com/pingcap/check" "github.com/pingcap/tidb/kv" ) diff --git a/store/tikv/txn.go b/store/tikv/txn.go index 5958148be0a92..8aafd16e74bf4 100644 --- a/store/tikv/txn.go +++ b/store/tikv/txn.go @@ -17,9 +17,9 @@ import ( "fmt" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/pkg/monotime" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/kv" "github.com/pingcap/tipb/go-binlog" goctx "golang.org/x/net/context" diff --git a/table/column.go b/table/column.go index 3edfcf1c00ded..406cf7c73e926 100644 --- a/table/column.go +++ b/table/column.go @@ -21,8 +21,8 @@ import ( "strings" "unicode/utf8" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/expression" diff --git a/table/tables/memory_tables.go b/table/tables/memory_tables.go index 2c27d7e28bd4e..a30c20b6dcb6b 100644 --- a/table/tables/memory_tables.go +++ b/table/tables/memory_tables.go @@ -16,8 +16,8 @@ package tables import ( "sync" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/petar/GoLLRB/llrb" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/kv" diff --git a/table/tables/tables.go b/table/tables/tables.go index 2d2c1ebe2a083..91c5fb2b7d888 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -20,8 +20,8 @@ package tables import ( "strings" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta/autoid" diff --git a/terror/terror.go b/terror/terror.go index 3f272b9cfb786..a1093e7d1c41f 100644 --- a/terror/terror.go +++ b/terror/terror.go @@ -19,8 +19,8 @@ import ( "runtime" "strconv" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/mysql" ) diff --git a/tidb-server/main.go b/tidb-server/main.go index 49fc4a65dad03..bfa2bc688fa0f 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -23,9 +23,9 @@ import ( "syscall" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" - "github.com/ngaut/systimemon" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/tidb" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" @@ -38,6 +38,7 @@ import ( "github.com/pingcap/tidb/store/localstore/boltdb" "github.com/pingcap/tidb/store/tikv" "github.com/pingcap/tidb/util/printer" + "github.com/pingcap/tidb/util/systimemon" "github.com/pingcap/tidb/x-server" "github.com/pingcap/tipb/go-binlog" "github.com/prometheus/client_golang/prometheus" @@ -134,22 +135,30 @@ func main() { } // set log options + logConf := &logutil.LogConfig{ + Level: *logLevel, + } if len(*logFile) > 0 { - err := log.SetOutputByName(*logFile) - if err != nil { - log.Fatal(errors.ErrorStack(err)) + logConf.File = logutil.FileLogConfig{ + Filename: *logFile, + LogRotate: true, } - log.SetRotateByDay() - log.SetHighlighting(false) } + err := logutil.InitLogger(logConf) + if err != nil { + log.Fatal(err) + } + + // Make sure the TiDB info is always printed. + level := log.GetLevel() + log.SetLevel(log.InfoLevel) + printer.PrintTiDBInfo() + log.SetLevel(level) if joinCon != nil && *joinCon > 0 { plan.JoinConcurrency = *joinCon } plan.AllowCartesianProduct = *crossJoin - // Call this before setting log level to make sure that TiDB info could be printed. - printer.PrintTiDBInfo() - log.SetLevelByString(cfg.LogLevel) store := createStore() diff --git a/tidb.go b/tidb.go index 2adbfab6929d6..ae5224d5ba7ac 100644 --- a/tidb.go +++ b/tidb.go @@ -23,8 +23,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/domain" diff --git a/tidb_test.go b/tidb_test.go index 68c1f2321fc1e..9ffe1fbf44b25 100644 --- a/tidb_test.go +++ b/tidb_test.go @@ -24,8 +24,8 @@ import ( "time" "github.com/juju/errors" - "github.com/ngaut/log" . "github.com/pingcap/check" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/tidb/ast" "github.com/pingcap/tidb/context" "github.com/pingcap/tidb/domain" @@ -41,7 +41,9 @@ var store = flag.String("store", "memory", "registered store name, [memory, gole func TestT(t *testing.T) { logLevel := os.Getenv("log_level") - log.SetLevelByString(logLevel) + logutil.InitLogger(&logutil.LogConfig{ + Level: logLevel, + }) CustomVerboseFlag = true TestingT(t) } diff --git a/util/printer/printer.go b/util/printer/printer.go index 1d02245ea42b9..815a9c2e1789d 100644 --- a/util/printer/printer.go +++ b/util/printer/printer.go @@ -17,7 +17,7 @@ import ( "bytes" "fmt" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/tidb/mysql" ) diff --git a/util/systimemon/systime_mon.go b/util/systimemon/systime_mon.go new file mode 100644 index 0000000000000..6b146b42e40a1 --- /dev/null +++ b/util/systimemon/systime_mon.go @@ -0,0 +1,37 @@ +// Copyright 2017 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package systimemon + +import ( + "time" + + log "github.com/Sirupsen/logrus" +) + +// StartMonitor calls systimeErrHandler if system time jump backward. +func StartMonitor(now func() time.Time, systimeErrHandler func()) { + log.Info("start system time monitor") + tick := time.NewTicker(100 * time.Millisecond) + defer tick.Stop() + for { + last := now() + select { + case <-tick.C: + if now().Sub(last) < 0 { + log.Errorf("system time jump backward, last:%v", last) + systimeErrHandler() + } + } + } +} diff --git a/util/systimemon/systime_mon_test.go b/util/systimemon/systime_mon_test.go new file mode 100644 index 0000000000000..870a8dc84bc50 --- /dev/null +++ b/util/systimemon/systime_mon_test.go @@ -0,0 +1,41 @@ +// Copyright 2017 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package systimemon + +import ( + "testing" + "time" +) + +func TestSystimeMonitor(t *testing.T) { + jumpForward := false + trigged := false + go StartMonitor( + func() time.Time { + if !trigged { + trigged = true + return time.Now() + } + + return time.Now().Add(-2 * time.Second) + }, func() { + jumpForward = true + }) + + time.Sleep(1 * time.Second) + + if !jumpForward { + t.Error("should detect time error") + } +} diff --git a/util/types/time.go b/util/types/time.go index 1b23ac3fd3234..ef5410b8b34ef 100644 --- a/util/types/time.go +++ b/util/types/time.go @@ -23,8 +23,8 @@ import ( gotime "time" "unicode" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/mysql" ) diff --git a/x-server/conn.go b/x-server/conn.go index 2b52abcdc9a70..e7d6aacc23355 100644 --- a/x-server/conn.go +++ b/x-server/conn.go @@ -17,8 +17,8 @@ import ( "io" "net" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/terror" "github.com/pingcap/tidb/util/arena" ) diff --git a/x-server/server.go b/x-server/server.go index e16408d59ae17..05af1f5401e0e 100644 --- a/x-server/server.go +++ b/x-server/server.go @@ -20,8 +20,8 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/util"