From 18ba74fb8628ad0c7f1b75b70af8724bc3ba8840 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 12 Apr 2017 02:06:42 +0800 Subject: [PATCH 01/17] vendor: prepare vendor for logrus --- .../vendor/github.com/Sirupsen/logrus/LICENSE | 21 + .../github.com/Sirupsen/logrus/alt_exit.go | 64 +++ .../vendor/github.com/Sirupsen/logrus/doc.go | 26 ++ .../github.com/Sirupsen/logrus/entry.go | 275 ++++++++++++ .../github.com/Sirupsen/logrus/exported.go | 193 ++++++++ .../github.com/Sirupsen/logrus/formatter.go | 45 ++ .../github.com/Sirupsen/logrus/hooks.go | 34 ++ .../Sirupsen/logrus/json_formatter.go | 74 +++ .../github.com/Sirupsen/logrus/logger.go | 308 +++++++++++++ .../github.com/Sirupsen/logrus/logrus.go | 143 ++++++ .../Sirupsen/logrus/terminal_appengine.go | 10 + .../Sirupsen/logrus/terminal_bsd.go | 10 + .../Sirupsen/logrus/terminal_linux.go | 14 + .../Sirupsen/logrus/terminal_notwindows.go | 28 ++ .../Sirupsen/logrus/terminal_solaris.go | 21 + .../Sirupsen/logrus/terminal_windows.go | 33 ++ .../Sirupsen/logrus/text_formatter.go | 189 ++++++++ .../github.com/Sirupsen/logrus/writer.go | 62 +++ .../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 ++++++++++++++++++ 22 files changed, 2026 insertions(+) create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/LICENSE create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/alt_exit.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/doc.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/entry.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/exported.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/formatter.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/hooks.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/json_formatter.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/logger.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/logrus.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/terminal_appengine.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/terminal_bsd.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/terminal_linux.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/terminal_notwindows.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/terminal_solaris.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/terminal_windows.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/text_formatter.go create mode 100644 _vendor/vendor/github.com/Sirupsen/logrus/writer.go create mode 100644 _vendor/vendor/gopkg.in/natefinch/lumberjack.v2/LICENSE create mode 100644 _vendor/vendor/gopkg.in/natefinch/lumberjack.v2/chown.go create mode 100644 _vendor/vendor/gopkg.in/natefinch/lumberjack.v2/chown_linux.go create mode 100644 _vendor/vendor/gopkg.in/natefinch/lumberjack.v2/lumberjack.go diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/LICENSE b/_vendor/vendor/github.com/Sirupsen/logrus/LICENSE new file mode 100644 index 00000000000..f090cb42f37 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Simon Eskildsen + +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. diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/alt_exit.go b/_vendor/vendor/github.com/Sirupsen/logrus/alt_exit.go new file mode 100644 index 00000000000..b4c9e84754a --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/alt_exit.go @@ -0,0 +1,64 @@ +package logrus + +// The following code was sourced and modified from the +// https://bitbucket.org/tebeka/atexit package governed by the following license: +// +// Copyright (c) 2012 Miki Tebeka . +// +// 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. + +import ( + "fmt" + "os" +) + +var handlers = []func(){} + +func runHandler(handler func()) { + defer func() { + if err := recover(); err != nil { + fmt.Fprintln(os.Stderr, "Error: Logrus exit handler error:", err) + } + }() + + handler() +} + +func runHandlers() { + for _, handler := range handlers { + runHandler(handler) + } +} + +// Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code) +func Exit(code int) { + runHandlers() + os.Exit(code) +} + +// RegisterExitHandler adds a Logrus Exit handler, call logrus.Exit to invoke +// all handlers. The handlers will also be invoked when any Fatal log entry is +// made. +// +// This method is useful when a caller wishes to use logrus to log a fatal +// message but also needs to gracefully shutdown. An example usecase could be +// closing database connections, or sending a alert that the application is +// closing. +func RegisterExitHandler(handler func()) { + handlers = append(handlers, handler) +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/doc.go b/_vendor/vendor/github.com/Sirupsen/logrus/doc.go new file mode 100644 index 00000000000..dddd5f877bf --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/doc.go @@ -0,0 +1,26 @@ +/* +Package logrus is a structured logger for Go, completely API compatible with the standard library logger. + + +The simplest way to use Logrus is simply the package-level exported logger: + + package main + + import ( + log "github.com/Sirupsen/logrus" + ) + + func main() { + log.WithFields(log.Fields{ + "animal": "walrus", + "number": 1, + "size": 10, + }).Info("A walrus appears") + } + +Output: + time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10 + +For a full guide visit https://github.com/Sirupsen/logrus +*/ +package logrus diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/entry.go b/_vendor/vendor/github.com/Sirupsen/logrus/entry.go new file mode 100644 index 00000000000..4edbe7a2de6 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/entry.go @@ -0,0 +1,275 @@ +package logrus + +import ( + "bytes" + "fmt" + "os" + "sync" + "time" +) + +var bufferPool *sync.Pool + +func init() { + bufferPool = &sync.Pool{ + New: func() interface{} { + return new(bytes.Buffer) + }, + } +} + +// Defines the key when adding errors using WithError. +var ErrorKey = "error" + +// An entry is the final or intermediate Logrus logging entry. It contains all +// the fields passed with WithField{,s}. It's finally logged when Debug, Info, +// Warn, Error, Fatal or Panic is called on it. These objects can be reused and +// passed around as much as you wish to avoid field duplication. +type Entry struct { + Logger *Logger + + // Contains all the fields set by the user. + Data Fields + + // Time at which the log entry was created + Time time.Time + + // Level the log entry was logged at: Debug, Info, Warn, Error, Fatal or Panic + Level Level + + // Message passed to Debug, Info, Warn, Error, Fatal or Panic + Message string + + // When formatter is called in entry.log(), an Buffer may be set to entry + Buffer *bytes.Buffer +} + +func NewEntry(logger *Logger) *Entry { + return &Entry{ + Logger: logger, + // Default is three fields, give a little extra room + Data: make(Fields, 5), + } +} + +// Returns the string representation from the reader and ultimately the +// formatter. +func (entry *Entry) String() (string, error) { + serialized, err := entry.Logger.Formatter.Format(entry) + if err != nil { + return "", err + } + str := string(serialized) + return str, nil +} + +// Add an error as single field (using the key defined in ErrorKey) to the Entry. +func (entry *Entry) WithError(err error) *Entry { + return entry.WithField(ErrorKey, err) +} + +// Add a single field to the Entry. +func (entry *Entry) WithField(key string, value interface{}) *Entry { + return entry.WithFields(Fields{key: value}) +} + +// Add a map of fields to the Entry. +func (entry *Entry) WithFields(fields Fields) *Entry { + data := make(Fields, len(entry.Data)+len(fields)) + for k, v := range entry.Data { + data[k] = v + } + for k, v := range fields { + data[k] = v + } + return &Entry{Logger: entry.Logger, Data: data} +} + +// This function is not declared with a pointer value because otherwise +// race conditions will occur when using multiple goroutines +func (entry Entry) log(level Level, msg string) { + var buffer *bytes.Buffer + entry.Time = time.Now() + entry.Level = level + entry.Message = msg + + if err := entry.Logger.Hooks.Fire(level, &entry); err != nil { + entry.Logger.mu.Lock() + fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err) + entry.Logger.mu.Unlock() + } + buffer = bufferPool.Get().(*bytes.Buffer) + buffer.Reset() + defer bufferPool.Put(buffer) + entry.Buffer = buffer + serialized, err := entry.Logger.Formatter.Format(&entry) + entry.Buffer = nil + if err != nil { + entry.Logger.mu.Lock() + fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err) + entry.Logger.mu.Unlock() + } else { + entry.Logger.mu.Lock() + _, err = entry.Logger.Out.Write(serialized) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err) + } + entry.Logger.mu.Unlock() + } + + // To avoid Entry#log() returning a value that only would make sense for + // panic() to use in Entry#Panic(), we avoid the allocation by checking + // directly here. + if level <= PanicLevel { + panic(&entry) + } +} + +func (entry *Entry) Debug(args ...interface{}) { + if entry.Logger.Level >= DebugLevel { + entry.log(DebugLevel, fmt.Sprint(args...)) + } +} + +func (entry *Entry) Print(args ...interface{}) { + entry.Info(args...) +} + +func (entry *Entry) Info(args ...interface{}) { + if entry.Logger.Level >= InfoLevel { + entry.log(InfoLevel, fmt.Sprint(args...)) + } +} + +func (entry *Entry) Warn(args ...interface{}) { + if entry.Logger.Level >= WarnLevel { + entry.log(WarnLevel, fmt.Sprint(args...)) + } +} + +func (entry *Entry) Warning(args ...interface{}) { + entry.Warn(args...) +} + +func (entry *Entry) Error(args ...interface{}) { + if entry.Logger.Level >= ErrorLevel { + entry.log(ErrorLevel, fmt.Sprint(args...)) + } +} + +func (entry *Entry) Fatal(args ...interface{}) { + if entry.Logger.Level >= FatalLevel { + entry.log(FatalLevel, fmt.Sprint(args...)) + } + Exit(1) +} + +func (entry *Entry) Panic(args ...interface{}) { + if entry.Logger.Level >= PanicLevel { + entry.log(PanicLevel, fmt.Sprint(args...)) + } + panic(fmt.Sprint(args...)) +} + +// Entry Printf family functions + +func (entry *Entry) Debugf(format string, args ...interface{}) { + if entry.Logger.Level >= DebugLevel { + entry.Debug(fmt.Sprintf(format, args...)) + } +} + +func (entry *Entry) Infof(format string, args ...interface{}) { + if entry.Logger.Level >= InfoLevel { + entry.Info(fmt.Sprintf(format, args...)) + } +} + +func (entry *Entry) Printf(format string, args ...interface{}) { + entry.Infof(format, args...) +} + +func (entry *Entry) Warnf(format string, args ...interface{}) { + if entry.Logger.Level >= WarnLevel { + entry.Warn(fmt.Sprintf(format, args...)) + } +} + +func (entry *Entry) Warningf(format string, args ...interface{}) { + entry.Warnf(format, args...) +} + +func (entry *Entry) Errorf(format string, args ...interface{}) { + if entry.Logger.Level >= ErrorLevel { + entry.Error(fmt.Sprintf(format, args...)) + } +} + +func (entry *Entry) Fatalf(format string, args ...interface{}) { + if entry.Logger.Level >= FatalLevel { + entry.Fatal(fmt.Sprintf(format, args...)) + } + Exit(1) +} + +func (entry *Entry) Panicf(format string, args ...interface{}) { + if entry.Logger.Level >= PanicLevel { + entry.Panic(fmt.Sprintf(format, args...)) + } +} + +// Entry Println family functions + +func (entry *Entry) Debugln(args ...interface{}) { + if entry.Logger.Level >= DebugLevel { + entry.Debug(entry.sprintlnn(args...)) + } +} + +func (entry *Entry) Infoln(args ...interface{}) { + if entry.Logger.Level >= InfoLevel { + entry.Info(entry.sprintlnn(args...)) + } +} + +func (entry *Entry) Println(args ...interface{}) { + entry.Infoln(args...) +} + +func (entry *Entry) Warnln(args ...interface{}) { + if entry.Logger.Level >= WarnLevel { + entry.Warn(entry.sprintlnn(args...)) + } +} + +func (entry *Entry) Warningln(args ...interface{}) { + entry.Warnln(args...) +} + +func (entry *Entry) Errorln(args ...interface{}) { + if entry.Logger.Level >= ErrorLevel { + entry.Error(entry.sprintlnn(args...)) + } +} + +func (entry *Entry) Fatalln(args ...interface{}) { + if entry.Logger.Level >= FatalLevel { + entry.Fatal(entry.sprintlnn(args...)) + } + Exit(1) +} + +func (entry *Entry) Panicln(args ...interface{}) { + if entry.Logger.Level >= PanicLevel { + entry.Panic(entry.sprintlnn(args...)) + } +} + +// Sprintlnn => Sprint no newline. This is to get the behavior of how +// fmt.Sprintln where spaces are always added between operands, regardless of +// their type. Instead of vendoring the Sprintln implementation to spare a +// string allocation, we do the simplest thing. +func (entry *Entry) sprintlnn(args ...interface{}) string { + msg := fmt.Sprintln(args...) + return msg[:len(msg)-1] +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/exported.go b/_vendor/vendor/github.com/Sirupsen/logrus/exported.go new file mode 100644 index 00000000000..9a0120ac1dd --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/exported.go @@ -0,0 +1,193 @@ +package logrus + +import ( + "io" +) + +var ( + // std is the name of the standard logger in stdlib `log` + std = New() +) + +func StandardLogger() *Logger { + return std +} + +// SetOutput sets the standard logger output. +func SetOutput(out io.Writer) { + std.mu.Lock() + defer std.mu.Unlock() + std.Out = out +} + +// SetFormatter sets the standard logger formatter. +func SetFormatter(formatter Formatter) { + std.mu.Lock() + defer std.mu.Unlock() + std.Formatter = formatter +} + +// SetLevel sets the standard logger level. +func SetLevel(level Level) { + std.mu.Lock() + defer std.mu.Unlock() + std.Level = level +} + +// GetLevel returns the standard logger level. +func GetLevel() Level { + std.mu.Lock() + defer std.mu.Unlock() + return std.Level +} + +// AddHook adds a hook to the standard logger hooks. +func AddHook(hook Hook) { + std.mu.Lock() + defer std.mu.Unlock() + std.Hooks.Add(hook) +} + +// WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key. +func WithError(err error) *Entry { + return std.WithField(ErrorKey, err) +} + +// WithField creates an entry from the standard logger and adds a field to +// it. If you want multiple fields, use `WithFields`. +// +// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal +// or Panic on the Entry it returns. +func WithField(key string, value interface{}) *Entry { + return std.WithField(key, value) +} + +// WithFields creates an entry from the standard logger and adds multiple +// fields to it. This is simply a helper for `WithField`, invoking it +// once for each field. +// +// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal +// or Panic on the Entry it returns. +func WithFields(fields Fields) *Entry { + return std.WithFields(fields) +} + +// Debug logs a message at level Debug on the standard logger. +func Debug(args ...interface{}) { + std.Debug(args...) +} + +// Print logs a message at level Info on the standard logger. +func Print(args ...interface{}) { + std.Print(args...) +} + +// Info logs a message at level Info on the standard logger. +func Info(args ...interface{}) { + std.Info(args...) +} + +// Warn logs a message at level Warn on the standard logger. +func Warn(args ...interface{}) { + std.Warn(args...) +} + +// Warning logs a message at level Warn on the standard logger. +func Warning(args ...interface{}) { + std.Warning(args...) +} + +// Error logs a message at level Error on the standard logger. +func Error(args ...interface{}) { + std.Error(args...) +} + +// Panic logs a message at level Panic on the standard logger. +func Panic(args ...interface{}) { + std.Panic(args...) +} + +// Fatal logs a message at level Fatal on the standard logger. +func Fatal(args ...interface{}) { + std.Fatal(args...) +} + +// Debugf logs a message at level Debug on the standard logger. +func Debugf(format string, args ...interface{}) { + std.Debugf(format, args...) +} + +// Printf logs a message at level Info on the standard logger. +func Printf(format string, args ...interface{}) { + std.Printf(format, args...) +} + +// Infof logs a message at level Info on the standard logger. +func Infof(format string, args ...interface{}) { + std.Infof(format, args...) +} + +// Warnf logs a message at level Warn on the standard logger. +func Warnf(format string, args ...interface{}) { + std.Warnf(format, args...) +} + +// Warningf logs a message at level Warn on the standard logger. +func Warningf(format string, args ...interface{}) { + std.Warningf(format, args...) +} + +// Errorf logs a message at level Error on the standard logger. +func Errorf(format string, args ...interface{}) { + std.Errorf(format, args...) +} + +// Panicf logs a message at level Panic on the standard logger. +func Panicf(format string, args ...interface{}) { + std.Panicf(format, args...) +} + +// Fatalf logs a message at level Fatal on the standard logger. +func Fatalf(format string, args ...interface{}) { + std.Fatalf(format, args...) +} + +// Debugln logs a message at level Debug on the standard logger. +func Debugln(args ...interface{}) { + std.Debugln(args...) +} + +// Println logs a message at level Info on the standard logger. +func Println(args ...interface{}) { + std.Println(args...) +} + +// Infoln logs a message at level Info on the standard logger. +func Infoln(args ...interface{}) { + std.Infoln(args...) +} + +// Warnln logs a message at level Warn on the standard logger. +func Warnln(args ...interface{}) { + std.Warnln(args...) +} + +// Warningln logs a message at level Warn on the standard logger. +func Warningln(args ...interface{}) { + std.Warningln(args...) +} + +// Errorln logs a message at level Error on the standard logger. +func Errorln(args ...interface{}) { + std.Errorln(args...) +} + +// Panicln logs a message at level Panic on the standard logger. +func Panicln(args ...interface{}) { + std.Panicln(args...) +} + +// Fatalln logs a message at level Fatal on the standard logger. +func Fatalln(args ...interface{}) { + std.Fatalln(args...) +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/formatter.go b/_vendor/vendor/github.com/Sirupsen/logrus/formatter.go new file mode 100644 index 00000000000..b5fbe934d12 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/formatter.go @@ -0,0 +1,45 @@ +package logrus + +import "time" + +const DefaultTimestampFormat = time.RFC3339 + +// The Formatter interface is used to implement a custom Formatter. It takes an +// `Entry`. It exposes all the fields, including the default ones: +// +// * `entry.Data["msg"]`. The message passed from Info, Warn, Error .. +// * `entry.Data["time"]`. The timestamp. +// * `entry.Data["level"]. The level the entry was logged at. +// +// Any additional fields added with `WithField` or `WithFields` are also in +// `entry.Data`. Format is expected to return an array of bytes which are then +// logged to `logger.Out`. +type Formatter interface { + Format(*Entry) ([]byte, error) +} + +// This is to not silently overwrite `time`, `msg` and `level` fields when +// dumping it. If this code wasn't there doing: +// +// logrus.WithField("level", 1).Info("hello") +// +// Would just silently drop the user provided level. Instead with this code +// it'll logged as: +// +// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."} +// +// It's not exported because it's still using Data in an opinionated way. It's to +// avoid code duplication between the two default formatters. +func prefixFieldClashes(data Fields) { + if t, ok := data["time"]; ok { + data["fields.time"] = t + } + + if m, ok := data["msg"]; ok { + data["fields.msg"] = m + } + + if l, ok := data["level"]; ok { + data["fields.level"] = l + } +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/hooks.go b/_vendor/vendor/github.com/Sirupsen/logrus/hooks.go new file mode 100644 index 00000000000..3f151cdc392 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/hooks.go @@ -0,0 +1,34 @@ +package logrus + +// A hook to be fired when logging on the logging levels returned from +// `Levels()` on your implementation of the interface. Note that this is not +// fired in a goroutine or a channel with workers, you should handle such +// functionality yourself if your call is non-blocking and you don't wish for +// the logging calls for levels returned from `Levels()` to block. +type Hook interface { + Levels() []Level + Fire(*Entry) error +} + +// Internal type for storing the hooks on a logger instance. +type LevelHooks map[Level][]Hook + +// Add a hook to an instance of logger. This is called with +// `log.Hooks.Add(new(MyHook))` where `MyHook` implements the `Hook` interface. +func (hooks LevelHooks) Add(hook Hook) { + for _, level := range hook.Levels() { + hooks[level] = append(hooks[level], hook) + } +} + +// Fire all the hooks for the passed level. Used by `entry.log` to fire +// appropriate hooks for a log entry. +func (hooks LevelHooks) Fire(level Level, entry *Entry) error { + for _, hook := range hooks[level] { + if err := hook.Fire(entry); err != nil { + return err + } + } + + return nil +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/json_formatter.go b/_vendor/vendor/github.com/Sirupsen/logrus/json_formatter.go new file mode 100644 index 00000000000..266554e9ff7 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/json_formatter.go @@ -0,0 +1,74 @@ +package logrus + +import ( + "encoding/json" + "fmt" +) + +type fieldKey string +type FieldMap map[fieldKey]string + +const ( + FieldKeyMsg = "msg" + FieldKeyLevel = "level" + FieldKeyTime = "time" +) + +func (f FieldMap) resolve(key fieldKey) string { + if k, ok := f[key]; ok { + return k + } + + return string(key) +} + +type JSONFormatter struct { + // TimestampFormat sets the format used for marshaling timestamps. + TimestampFormat string + + // DisableTimestamp allows disabling automatic timestamps in output + DisableTimestamp bool + + // FieldMap allows users to customize the names of keys for various fields. + // As an example: + // formatter := &JSONFormatter{ + // FieldMap: FieldMap{ + // FieldKeyTime: "@timestamp", + // FieldKeyLevel: "@level", + // FieldKeyLevel: "@message", + // }, + // } + FieldMap FieldMap +} + +func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { + data := make(Fields, len(entry.Data)+3) + for k, v := range entry.Data { + switch v := v.(type) { + case error: + // Otherwise errors are ignored by `encoding/json` + // https://github.com/Sirupsen/logrus/issues/137 + data[k] = v.Error() + default: + data[k] = v + } + } + prefixFieldClashes(data) + + timestampFormat := f.TimestampFormat + if timestampFormat == "" { + timestampFormat = DefaultTimestampFormat + } + + if !f.DisableTimestamp { + data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat) + } + data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message + data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String() + + serialized, err := json.Marshal(data) + if err != nil { + return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err) + } + return append(serialized, '\n'), nil +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/logger.go b/_vendor/vendor/github.com/Sirupsen/logrus/logger.go new file mode 100644 index 00000000000..b769f3d3522 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/logger.go @@ -0,0 +1,308 @@ +package logrus + +import ( + "io" + "os" + "sync" +) + +type Logger struct { + // The logs are `io.Copy`'d to this in a mutex. It's common to set this to a + // file, or leave it default which is `os.Stderr`. You can also set this to + // something more adventorous, such as logging to Kafka. + Out io.Writer + // Hooks for the logger instance. These allow firing events based on logging + // levels and log entries. For example, to send errors to an error tracking + // service, log to StatsD or dump the core on fatal errors. + Hooks LevelHooks + // All log entries pass through the formatter before logged to Out. The + // included formatters are `TextFormatter` and `JSONFormatter` for which + // TextFormatter is the default. In development (when a TTY is attached) it + // logs with colors, but to a file it wouldn't. You can easily implement your + // own that implements the `Formatter` interface, see the `README` or included + // formatters for examples. + Formatter Formatter + // The logging level the logger should log at. This is typically (and defaults + // to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be + // logged. `logrus.Debug` is useful in + Level Level + // Used to sync writing to the log. Locking is enabled by Default + mu MutexWrap + // Reusable empty entry + entryPool sync.Pool +} + +type MutexWrap struct { + lock sync.Mutex + disabled bool +} + +func (mw *MutexWrap) Lock() { + if !mw.disabled { + mw.lock.Lock() + } +} + +func (mw *MutexWrap) Unlock() { + if !mw.disabled { + mw.lock.Unlock() + } +} + +func (mw *MutexWrap) Disable() { + mw.disabled = true +} + +// Creates a new logger. Configuration should be set by changing `Formatter`, +// `Out` and `Hooks` directly on the default logger instance. You can also just +// instantiate your own: +// +// var log = &Logger{ +// Out: os.Stderr, +// Formatter: new(JSONFormatter), +// Hooks: make(LevelHooks), +// Level: logrus.DebugLevel, +// } +// +// It's recommended to make this a global instance called `log`. +func New() *Logger { + return &Logger{ + Out: os.Stderr, + Formatter: new(TextFormatter), + Hooks: make(LevelHooks), + Level: InfoLevel, + } +} + +func (logger *Logger) newEntry() *Entry { + entry, ok := logger.entryPool.Get().(*Entry) + if ok { + return entry + } + return NewEntry(logger) +} + +func (logger *Logger) releaseEntry(entry *Entry) { + logger.entryPool.Put(entry) +} + +// Adds a field to the log entry, note that it doesn't log until you call +// Debug, Print, Info, Warn, Fatal or Panic. It only creates a log entry. +// If you want multiple fields, use `WithFields`. +func (logger *Logger) WithField(key string, value interface{}) *Entry { + entry := logger.newEntry() + defer logger.releaseEntry(entry) + return entry.WithField(key, value) +} + +// Adds a struct of fields to the log entry. All it does is call `WithField` for +// each `Field`. +func (logger *Logger) WithFields(fields Fields) *Entry { + entry := logger.newEntry() + defer logger.releaseEntry(entry) + return entry.WithFields(fields) +} + +// Add an error as single field to the log entry. All it does is call +// `WithError` for the given `error`. +func (logger *Logger) WithError(err error) *Entry { + entry := logger.newEntry() + defer logger.releaseEntry(entry) + return entry.WithError(err) +} + +func (logger *Logger) Debugf(format string, args ...interface{}) { + if logger.Level >= DebugLevel { + entry := logger.newEntry() + entry.Debugf(format, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Infof(format string, args ...interface{}) { + if logger.Level >= InfoLevel { + entry := logger.newEntry() + entry.Infof(format, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Printf(format string, args ...interface{}) { + entry := logger.newEntry() + entry.Printf(format, args...) + logger.releaseEntry(entry) +} + +func (logger *Logger) Warnf(format string, args ...interface{}) { + if logger.Level >= WarnLevel { + entry := logger.newEntry() + entry.Warnf(format, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Warningf(format string, args ...interface{}) { + if logger.Level >= WarnLevel { + entry := logger.newEntry() + entry.Warnf(format, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Errorf(format string, args ...interface{}) { + if logger.Level >= ErrorLevel { + entry := logger.newEntry() + entry.Errorf(format, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Fatalf(format string, args ...interface{}) { + if logger.Level >= FatalLevel { + entry := logger.newEntry() + entry.Fatalf(format, args...) + logger.releaseEntry(entry) + } + Exit(1) +} + +func (logger *Logger) Panicf(format string, args ...interface{}) { + if logger.Level >= PanicLevel { + entry := logger.newEntry() + entry.Panicf(format, args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Debug(args ...interface{}) { + if logger.Level >= DebugLevel { + entry := logger.newEntry() + entry.Debug(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Info(args ...interface{}) { + if logger.Level >= InfoLevel { + entry := logger.newEntry() + entry.Info(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Print(args ...interface{}) { + entry := logger.newEntry() + entry.Info(args...) + logger.releaseEntry(entry) +} + +func (logger *Logger) Warn(args ...interface{}) { + if logger.Level >= WarnLevel { + entry := logger.newEntry() + entry.Warn(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Warning(args ...interface{}) { + if logger.Level >= WarnLevel { + entry := logger.newEntry() + entry.Warn(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Error(args ...interface{}) { + if logger.Level >= ErrorLevel { + entry := logger.newEntry() + entry.Error(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Fatal(args ...interface{}) { + if logger.Level >= FatalLevel { + entry := logger.newEntry() + entry.Fatal(args...) + logger.releaseEntry(entry) + } + Exit(1) +} + +func (logger *Logger) Panic(args ...interface{}) { + if logger.Level >= PanicLevel { + entry := logger.newEntry() + entry.Panic(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Debugln(args ...interface{}) { + if logger.Level >= DebugLevel { + entry := logger.newEntry() + entry.Debugln(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Infoln(args ...interface{}) { + if logger.Level >= InfoLevel { + entry := logger.newEntry() + entry.Infoln(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Println(args ...interface{}) { + entry := logger.newEntry() + entry.Println(args...) + logger.releaseEntry(entry) +} + +func (logger *Logger) Warnln(args ...interface{}) { + if logger.Level >= WarnLevel { + entry := logger.newEntry() + entry.Warnln(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Warningln(args ...interface{}) { + if logger.Level >= WarnLevel { + entry := logger.newEntry() + entry.Warnln(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Errorln(args ...interface{}) { + if logger.Level >= ErrorLevel { + entry := logger.newEntry() + entry.Errorln(args...) + logger.releaseEntry(entry) + } +} + +func (logger *Logger) Fatalln(args ...interface{}) { + if logger.Level >= FatalLevel { + entry := logger.newEntry() + entry.Fatalln(args...) + logger.releaseEntry(entry) + } + Exit(1) +} + +func (logger *Logger) Panicln(args ...interface{}) { + if logger.Level >= PanicLevel { + entry := logger.newEntry() + entry.Panicln(args...) + logger.releaseEntry(entry) + } +} + +//When file is opened with appending mode, it's safe to +//write concurrently to a file (within 4k message on Linux). +//In these cases user can choose to disable the lock. +func (logger *Logger) SetNoLock() { + logger.mu.Disable() +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/logrus.go b/_vendor/vendor/github.com/Sirupsen/logrus/logrus.go new file mode 100644 index 00000000000..e596691116d --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/logrus.go @@ -0,0 +1,143 @@ +package logrus + +import ( + "fmt" + "log" + "strings" +) + +// Fields type, used to pass to `WithFields`. +type Fields map[string]interface{} + +// Level type +type Level uint8 + +// Convert the Level to a string. E.g. PanicLevel becomes "panic". +func (level Level) String() string { + switch level { + case DebugLevel: + return "debug" + case InfoLevel: + return "info" + case WarnLevel: + return "warning" + case ErrorLevel: + return "error" + case FatalLevel: + return "fatal" + case PanicLevel: + return "panic" + } + + return "unknown" +} + +// ParseLevel takes a string level and returns the Logrus log level constant. +func ParseLevel(lvl string) (Level, error) { + switch strings.ToLower(lvl) { + case "panic": + return PanicLevel, nil + case "fatal": + return FatalLevel, nil + case "error": + return ErrorLevel, nil + case "warn", "warning": + return WarnLevel, nil + case "info": + return InfoLevel, nil + case "debug": + return DebugLevel, nil + } + + var l Level + return l, fmt.Errorf("not a valid logrus Level: %q", lvl) +} + +// A constant exposing all logging levels +var AllLevels = []Level{ + PanicLevel, + FatalLevel, + ErrorLevel, + WarnLevel, + InfoLevel, + DebugLevel, +} + +// These are the different logging levels. You can set the logging level to log +// on your instance of logger, obtained with `logrus.New()`. +const ( + // PanicLevel level, highest level of severity. Logs and then calls panic with the + // message passed to Debug, Info, ... + PanicLevel Level = iota + // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the + // logging level is set to Panic. + FatalLevel + // ErrorLevel level. Logs. Used for errors that should definitely be noted. + // Commonly used for hooks to send errors to an error tracking service. + ErrorLevel + // WarnLevel level. Non-critical entries that deserve eyes. + WarnLevel + // InfoLevel level. General operational entries about what's going on inside the + // application. + InfoLevel + // DebugLevel level. Usually only enabled when debugging. Very verbose logging. + DebugLevel +) + +// Won't compile if StdLogger can't be realized by a log.Logger +var ( + _ StdLogger = &log.Logger{} + _ StdLogger = &Entry{} + _ StdLogger = &Logger{} +) + +// StdLogger is what your logrus-enabled library should take, that way +// it'll accept a stdlib logger and a logrus logger. There's no standard +// interface, this is the closest we get, unfortunately. +type StdLogger interface { + Print(...interface{}) + Printf(string, ...interface{}) + Println(...interface{}) + + Fatal(...interface{}) + Fatalf(string, ...interface{}) + Fatalln(...interface{}) + + Panic(...interface{}) + Panicf(string, ...interface{}) + Panicln(...interface{}) +} + +// The FieldLogger interface generalizes the Entry and Logger types +type FieldLogger interface { + WithField(key string, value interface{}) *Entry + WithFields(fields Fields) *Entry + WithError(err error) *Entry + + Debugf(format string, args ...interface{}) + Infof(format string, args ...interface{}) + Printf(format string, args ...interface{}) + Warnf(format string, args ...interface{}) + Warningf(format string, args ...interface{}) + Errorf(format string, args ...interface{}) + Fatalf(format string, args ...interface{}) + Panicf(format string, args ...interface{}) + + Debug(args ...interface{}) + Info(args ...interface{}) + Print(args ...interface{}) + Warn(args ...interface{}) + Warning(args ...interface{}) + Error(args ...interface{}) + Fatal(args ...interface{}) + Panic(args ...interface{}) + + Debugln(args ...interface{}) + Infoln(args ...interface{}) + Println(args ...interface{}) + Warnln(args ...interface{}) + Warningln(args ...interface{}) + Errorln(args ...interface{}) + Fatalln(args ...interface{}) + Panicln(args ...interface{}) +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/terminal_appengine.go b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_appengine.go new file mode 100644 index 00000000000..e011a869458 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_appengine.go @@ -0,0 +1,10 @@ +// +build appengine + +package logrus + +import "io" + +// IsTerminal returns true if stderr's file descriptor is a terminal. +func IsTerminal(f io.Writer) bool { + return true +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/terminal_bsd.go b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_bsd.go new file mode 100644 index 00000000000..5f6be4d3c04 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_bsd.go @@ -0,0 +1,10 @@ +// +build darwin freebsd openbsd netbsd dragonfly +// +build !appengine + +package logrus + +import "syscall" + +const ioctlReadTermios = syscall.TIOCGETA + +type Termios syscall.Termios diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/terminal_linux.go b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_linux.go new file mode 100644 index 00000000000..308160ca804 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_linux.go @@ -0,0 +1,14 @@ +// Based on ssh/terminal: +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !appengine + +package logrus + +import "syscall" + +const ioctlReadTermios = syscall.TCGETS + +type Termios syscall.Termios diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/terminal_notwindows.go b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_notwindows.go new file mode 100644 index 00000000000..190297abf38 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_notwindows.go @@ -0,0 +1,28 @@ +// Based on ssh/terminal: +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build linux darwin freebsd openbsd netbsd dragonfly +// +build !appengine + +package logrus + +import ( + "io" + "os" + "syscall" + "unsafe" +) + +// IsTerminal returns true if stderr's file descriptor is a terminal. +func IsTerminal(f io.Writer) bool { + var termios Termios + switch v := f.(type) { + case *os.File: + _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(v.Fd()), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0) + return err == 0 + default: + return false + } +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/terminal_solaris.go b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_solaris.go new file mode 100644 index 00000000000..3c86b1abeeb --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_solaris.go @@ -0,0 +1,21 @@ +// +build solaris,!appengine + +package logrus + +import ( + "io" + "os" + + "golang.org/x/sys/unix" +) + +// IsTerminal returns true if the given file descriptor is a terminal. +func IsTerminal(f io.Writer) bool { + switch v := f.(type) { + case *os.File: + _, err := unix.IoctlGetTermios(int(v.Fd()), unix.TCGETA) + return err == nil + default: + return false + } +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/terminal_windows.go b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_windows.go new file mode 100644 index 00000000000..05d2f91f16b --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/terminal_windows.go @@ -0,0 +1,33 @@ +// Based on ssh/terminal: +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows,!appengine + +package logrus + +import ( + "io" + "os" + "syscall" + "unsafe" +) + +var kernel32 = syscall.NewLazyDLL("kernel32.dll") + +var ( + procGetConsoleMode = kernel32.NewProc("GetConsoleMode") +) + +// IsTerminal returns true if stderr's file descriptor is a terminal. +func IsTerminal(f io.Writer) bool { + switch v := f.(type) { + case *os.File: + var st uint32 + r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, uintptr(v.Fd()), uintptr(unsafe.Pointer(&st)), 0) + return r != 0 && e == 0 + default: + return false + } +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/text_formatter.go b/_vendor/vendor/github.com/Sirupsen/logrus/text_formatter.go new file mode 100644 index 00000000000..ba888540613 --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/text_formatter.go @@ -0,0 +1,189 @@ +package logrus + +import ( + "bytes" + "fmt" + "sort" + "strings" + "sync" + "time" +) + +const ( + nocolor = 0 + red = 31 + green = 32 + yellow = 33 + blue = 34 + gray = 37 +) + +var ( + baseTimestamp time.Time +) + +func init() { + baseTimestamp = time.Now() +} + +type TextFormatter struct { + // Set to true to bypass checking for a TTY before outputting colors. + ForceColors bool + + // Force disabling colors. + DisableColors bool + + // Disable timestamp logging. useful when output is redirected to logging + // system that already adds timestamps. + DisableTimestamp bool + + // Enable logging the full timestamp when a TTY is attached instead of just + // the time passed since beginning of execution. + FullTimestamp bool + + // TimestampFormat to use for display when a full timestamp is printed + TimestampFormat string + + // The fields are sorted by default for a consistent output. For applications + // that log extremely frequently and don't use the JSON formatter this may not + // be desired. + DisableSorting bool + + // QuoteEmptyFields will wrap empty fields in quotes if true + QuoteEmptyFields bool + + // QuoteCharacter can be set to the override the default quoting character " + // with something else. For example: ', or `. + QuoteCharacter string + + // Whether the logger's out is to a terminal + isTerminal bool + + sync.Once +} + +func (f *TextFormatter) init(entry *Entry) { + if len(f.QuoteCharacter) == 0 { + f.QuoteCharacter = "\"" + } + if entry.Logger != nil { + f.isTerminal = IsTerminal(entry.Logger.Out) + } +} + +func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { + var b *bytes.Buffer + keys := make([]string, 0, len(entry.Data)) + for k := range entry.Data { + keys = append(keys, k) + } + + if !f.DisableSorting { + sort.Strings(keys) + } + if entry.Buffer != nil { + b = entry.Buffer + } else { + b = &bytes.Buffer{} + } + + prefixFieldClashes(entry.Data) + + f.Do(func() { f.init(entry) }) + + isColored := (f.ForceColors || f.isTerminal) && !f.DisableColors + + timestampFormat := f.TimestampFormat + if timestampFormat == "" { + timestampFormat = DefaultTimestampFormat + } + if isColored { + f.printColored(b, entry, keys, timestampFormat) + } else { + if !f.DisableTimestamp { + f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat)) + } + f.appendKeyValue(b, "level", entry.Level.String()) + if entry.Message != "" { + f.appendKeyValue(b, "msg", entry.Message) + } + for _, key := range keys { + f.appendKeyValue(b, key, entry.Data[key]) + } + } + + b.WriteByte('\n') + return b.Bytes(), nil +} + +func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, timestampFormat string) { + var levelColor int + switch entry.Level { + case DebugLevel: + levelColor = gray + case WarnLevel: + levelColor = yellow + case ErrorLevel, FatalLevel, PanicLevel: + levelColor = red + default: + levelColor = blue + } + + levelText := strings.ToUpper(entry.Level.String())[0:4] + + if f.DisableTimestamp { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m %-44s ", levelColor, levelText, entry.Message) + } else if !f.FullTimestamp { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), entry.Message) + } else { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), entry.Message) + } + for _, k := range keys { + v := entry.Data[k] + fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k) + f.appendValue(b, v) + } +} + +func (f *TextFormatter) needsQuoting(text string) bool { + if f.QuoteEmptyFields && len(text) == 0 { + return true + } + for _, ch := range text { + if !((ch >= 'a' && ch <= 'z') || + (ch >= 'A' && ch <= 'Z') || + (ch >= '0' && ch <= '9') || + ch == '-' || ch == '.') { + return true + } + } + return false +} + +func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) { + + b.WriteString(key) + b.WriteByte('=') + f.appendValue(b, value) + b.WriteByte(' ') +} + +func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) { + switch value := value.(type) { + case string: + if !f.needsQuoting(value) { + b.WriteString(value) + } else { + fmt.Fprintf(b, "%s%v%s", f.QuoteCharacter, value, f.QuoteCharacter) + } + case error: + errmsg := value.Error() + if !f.needsQuoting(errmsg) { + b.WriteString(errmsg) + } else { + fmt.Fprintf(b, "%s%v%s", f.QuoteCharacter, errmsg, f.QuoteCharacter) + } + default: + fmt.Fprint(b, value) + } +} diff --git a/_vendor/vendor/github.com/Sirupsen/logrus/writer.go b/_vendor/vendor/github.com/Sirupsen/logrus/writer.go new file mode 100644 index 00000000000..7bdebedc60b --- /dev/null +++ b/_vendor/vendor/github.com/Sirupsen/logrus/writer.go @@ -0,0 +1,62 @@ +package logrus + +import ( + "bufio" + "io" + "runtime" +) + +func (logger *Logger) Writer() *io.PipeWriter { + return logger.WriterLevel(InfoLevel) +} + +func (logger *Logger) WriterLevel(level Level) *io.PipeWriter { + return NewEntry(logger).WriterLevel(level) +} + +func (entry *Entry) Writer() *io.PipeWriter { + return entry.WriterLevel(InfoLevel) +} + +func (entry *Entry) WriterLevel(level Level) *io.PipeWriter { + reader, writer := io.Pipe() + + var printFunc func(args ...interface{}) + + switch level { + case DebugLevel: + printFunc = entry.Debug + case InfoLevel: + printFunc = entry.Info + case WarnLevel: + printFunc = entry.Warn + case ErrorLevel: + printFunc = entry.Error + case FatalLevel: + printFunc = entry.Fatal + case PanicLevel: + printFunc = entry.Panic + default: + printFunc = entry.Print + } + + go entry.writerScanner(reader, printFunc) + runtime.SetFinalizer(writer, writerFinalizer) + + return writer +} + +func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) { + scanner := bufio.NewScanner(reader) + for scanner.Scan() { + printFunc(scanner.Text()) + } + if err := scanner.Err(); err != nil { + entry.Errorf("Error while reading from Writer: %s", err) + } + reader.Close() +} + +func writerFinalizer(writer *io.PipeWriter) { + writer.Close() +} diff --git a/_vendor/vendor/gopkg.in/natefinch/lumberjack.v2/LICENSE b/_vendor/vendor/gopkg.in/natefinch/lumberjack.v2/LICENSE new file mode 100644 index 00000000000..c3d4cc307d2 --- /dev/null +++ b/_vendor/vendor/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/vendor/gopkg.in/natefinch/lumberjack.v2/chown.go b/_vendor/vendor/gopkg.in/natefinch/lumberjack.v2/chown.go new file mode 100644 index 00000000000..11d06697232 --- /dev/null +++ b/_vendor/vendor/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/vendor/gopkg.in/natefinch/lumberjack.v2/chown_linux.go b/_vendor/vendor/gopkg.in/natefinch/lumberjack.v2/chown_linux.go new file mode 100644 index 00000000000..2758ec9cedd --- /dev/null +++ b/_vendor/vendor/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/vendor/gopkg.in/natefinch/lumberjack.v2/lumberjack.go b/_vendor/vendor/gopkg.in/natefinch/lumberjack.v2/lumberjack.go new file mode 100644 index 00000000000..b4bb54f8282 --- /dev/null +++ b/_vendor/vendor/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) +} From b9be6362a67f24ad6b7bbd36e655af77206508ee Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 12 Apr 2017 02:19:14 +0800 Subject: [PATCH 02/17] *: replace ngaut/log with logrus --- cmd/pd-server/main.go | 2 +- glide.lock | 12 ++-- glide.yaml | 10 +-- pkg/etcdutil/etcdutil.go | 2 +- pkg/metricutil/metricutil.go | 2 +- server/balancer.go | 2 +- server/cache.go | 2 +- server/cluster.go | 2 +- server/cluster_worker.go | 2 +- server/coordinator.go | 2 +- server/grpc_service.go | 2 +- server/id.go | 2 +- server/kv.go | 2 +- server/leader.go | 2 +- server/operator.go | 2 +- server/region_cache.go | 2 +- server/scheduler.go | 2 +- server/server.go | 2 +- server/tso.go | 2 +- server/util.go | 114 +++++++++++++++++++++++++++++++---- 20 files changed, 132 insertions(+), 38 deletions(-) diff --git a/cmd/pd-server/main.go b/cmd/pd-server/main.go index 43b0fa10610..ab383fac5eb 100644 --- a/cmd/pd-server/main.go +++ b/cmd/pd-server/main.go @@ -19,9 +19,9 @@ import ( "os/signal" "syscall" + log "github.com/Sirupsen/logrus" "github.com/grpc-ecosystem/go-grpc-prometheus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/pd/pkg/metricutil" "github.com/pingcap/pd/server" "github.com/pingcap/pd/server/api" diff --git a/glide.lock b/glide.lock index 3e129d4657b..cc5ccd6c0cd 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 2de6008aff0da78b970f4414873c3318e0c084fc5be875d1935f1a5cc1b36f8d -updated: 2017-04-07T13:30:27.614299275+08:00 +hash: 46f235ac2fe8d8d5e5f3b4a11ecb2f0382254edf988463de160b3da3cdb813bf +updated: 2017-04-12T02:17:53.324092921+08:00 imports: - name: github.com/beorn7/perks version: 3ac7bf7a47d159a033b107610db8a1b6575507a4 @@ -112,10 +112,8 @@ imports: version: 1ea25387ff6f684839d82767c1733ff4d4d15d0a - name: github.com/gorilla/mux version: 0eeaf8392f5b04950925b8a69fe70f110fa7cbfc -- name: github.com/gorilla/websocket - version: a836c37014dbbf0d1e871d04531b851fcbdeb22d - name: github.com/grpc-ecosystem/go-grpc-prometheus - version: 2500245aa6110c562d17020fb31a2c133d737799 + version: 6b7015e65d366bf3f19b2b2a000a831940f0f7e0 - name: github.com/grpc-ecosystem/grpc-gateway version: 84398b94e188ee336f307779b57b3aa91af7063c subpackages: @@ -172,6 +170,8 @@ imports: - model - name: github.com/prometheus/procfs version: abf152e5f3e97f2fafac028d2cc06c1feb87ffa5 +- name: github.com/Sirupsen/logrus + version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f - name: github.com/spf13/cobra version: 9495bc009a56819bdb0ddbc1a373e29c140bc674 repo: https://github.com/spf13/cobra.git @@ -227,6 +227,8 @@ imports: - naming - peer - transport +- name: gopkg.in/natefinch/lumberjack.v2 + version: dd45e6a67c53f673bb49ca8a001fd3a63ceb640e - name: gopkg.in/yaml.v2 version: 53feefa2559fb8dfa8d81baad31be332c97d6c77 testImports: [] diff --git a/glide.yaml b/glide.yaml index 2ec256a5bcd..0703d9066ea 100644 --- a/glide.yaml +++ b/glide.yaml @@ -97,8 +97,6 @@ import: version: ~1.1.0 - package: github.com/gorilla/mux version: ~1.1.0 -- package: github.com/gorilla/websocket - version: a836c37014dbbf0d1e871d04531b851fcbdeb22d - package: github.com/grpc-ecosystem/grpc-gateway version: 84398b94e188ee336f307779b57b3aa91af7063c subpackages: @@ -120,7 +118,7 @@ import: - package: github.com/pingcap/check version: ce8a2f822ab1e245a4eefcef2996531c79c943f1 - package: github.com/pingcap/kvproto - version: 0811f27579a1070546c4d5496bf3f6fa62cec750 + version: 0811f27579a1070546c4d5496bf3f6fa62cec750 subpackages: - pkg/coprocessor - pkg/eraftpb @@ -201,7 +199,7 @@ import: repo: https://github.com/spf13/cobra.git vcs: git - package: github.com/chzyer/readline - version: c914be64f07d9998f52bf0d598ec26d457168c0f + version: c914be64f07d9998f52bf0d598ec26d457168c0f - package: github.com/dustin/go-humanize version: 7a41df006ff9af79a29f0ffa9c5f21fbe6314a2d - package: github.com/elazarl/go-bindata-assetfs @@ -209,3 +207,7 @@ import: version: c2ca1c75c6af5556eb6ce67994120adf12f2ccef - package: github.com/montanaflynn/stats version: f8cd06f93c6c1b06028caafb88b540fc820f77c1 +- package: github.com/Sirupsen/logrus + version: v0.11.5 +- package: gopkg.in/natefinch/lumberjack.v2 + version: v2 diff --git a/pkg/etcdutil/etcdutil.go b/pkg/etcdutil/etcdutil.go index bdebb9e86eb..60037401bd1 100644 --- a/pkg/etcdutil/etcdutil.go +++ b/pkg/etcdutil/etcdutil.go @@ -18,11 +18,11 @@ import ( "strings" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/etcdserver" "github.com/coreos/etcd/pkg/types" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/pd/pkg/apiutil" "golang.org/x/net/context" ) diff --git a/pkg/metricutil/metricutil.go b/pkg/metricutil/metricutil.go index 239187bc3e5..54a77b48b45 100644 --- a/pkg/metricutil/metricutil.go +++ b/pkg/metricutil/metricutil.go @@ -17,7 +17,7 @@ import ( "time" "unicode" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/pd/pkg/typeutil" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/push" diff --git a/server/balancer.go b/server/balancer.go index fb679e2c885..d7878dd46c7 100644 --- a/server/balancer.go +++ b/server/balancer.go @@ -17,8 +17,8 @@ import ( "math" "time" + log "github.com/Sirupsen/logrus" "github.com/montanaflynn/stats" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" ) diff --git a/server/cache.go b/server/cache.go index b4e4cff235a..b7337ede96a 100644 --- a/server/cache.go +++ b/server/cache.go @@ -18,9 +18,9 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/gogo/protobuf/proto" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" ) diff --git a/server/cluster.go b/server/cluster.go index 2b7c909f67a..b8bea77413f 100644 --- a/server/cluster.go +++ b/server/cluster.go @@ -21,9 +21,9 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" ) diff --git a/server/cluster_worker.go b/server/cluster_worker.go index 2c7af8944ee..75391f793e2 100644 --- a/server/cluster_worker.go +++ b/server/cluster_worker.go @@ -16,9 +16,9 @@ package server import ( "bytes" + log "github.com/Sirupsen/logrus" "github.com/gogo/protobuf/proto" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" ) diff --git a/server/coordinator.go b/server/coordinator.go index 4e4ec3b6a3f..bf8da3a974f 100644 --- a/server/coordinator.go +++ b/server/coordinator.go @@ -17,8 +17,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/pdpb" "golang.org/x/net/context" ) diff --git a/server/grpc_service.go b/server/grpc_service.go index d8b97323208..213081f38dd 100644 --- a/server/grpc_service.go +++ b/server/grpc_service.go @@ -17,8 +17,8 @@ import ( "fmt" "io" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" "golang.org/x/net/context" diff --git a/server/id.go b/server/id.go index 46aa94be2fa..3602431128a 100644 --- a/server/id.go +++ b/server/id.go @@ -17,9 +17,9 @@ import ( "path" "sync" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/juju/errors" - "github.com/ngaut/log" ) const ( diff --git a/server/kv.go b/server/kv.go index 6321f9c6d00..38812a33fb5 100644 --- a/server/kv.go +++ b/server/kv.go @@ -20,10 +20,10 @@ import ( "path" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/gogo/protobuf/proto" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" "golang.org/x/net/context" ) diff --git a/server/leader.go b/server/leader.go index 26e9383a64c..b8b332e572d 100644 --- a/server/leader.go +++ b/server/leader.go @@ -20,10 +20,10 @@ import ( "strings" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/mvcc/mvccpb" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/pdpb" "golang.org/x/net/context" ) diff --git a/server/operator.go b/server/operator.go index 25feb703a9f..5df240f2b9d 100644 --- a/server/operator.go +++ b/server/operator.go @@ -17,7 +17,7 @@ import ( "fmt" "time" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" ) diff --git a/server/region_cache.go b/server/region_cache.go index a99381c23d3..ba217ec0dc1 100644 --- a/server/region_cache.go +++ b/server/region_cache.go @@ -18,7 +18,7 @@ import ( "sync" "time" - "github.com/ngaut/log" + log "github.com/Sirupsen/logrus" ) type cacheItem struct { diff --git a/server/scheduler.go b/server/scheduler.go index ce0ab1001be..2d49df1c76b 100644 --- a/server/scheduler.go +++ b/server/scheduler.go @@ -17,8 +17,8 @@ import ( "fmt" "math" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" ) diff --git a/server/server.go b/server/server.go index fd9d3c10e0c..ef841ea6808 100644 --- a/server/server.go +++ b/server/server.go @@ -23,11 +23,11 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/embed" "github.com/coreos/etcd/pkg/types" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/ngaut/systimemon" "github.com/pingcap/kvproto/pkg/pdpb" "github.com/pingcap/pd/pkg/etcdutil" diff --git a/server/tso.go b/server/tso.go index 18ea494c73e..ee00dcfd3e6 100644 --- a/server/tso.go +++ b/server/tso.go @@ -18,9 +18,9 @@ import ( "sync/atomic" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/pdpb" ) diff --git a/server/util.go b/server/util.go index 6964f8abbb0..e0e322516fa 100644 --- a/server/util.go +++ b/server/util.go @@ -23,24 +23,31 @@ import ( "os" "path" "reflect" + "runtime" "strings" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" "github.com/coreos/pkg/capnslog" "github.com/golang/protobuf/proto" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" "github.com/pingcap/pd/pkg/etcdutil" "golang.org/x/net/context" + lumberjack "gopkg.in/natefinch/lumberjack.v2" ) const ( requestTimeout = etcdutil.DefaultRequestTimeout slowRequestTime = etcdutil.DefaultSlowRequestTime + defaultLogMaxSize = 1 // MB + defaultLogMaxBackups = 3 + defaultLogMaxAge = 28 // days + defaultLogLevel = log.InfoLevel + logDirMode = 0755 ) @@ -246,14 +253,18 @@ func rpcConnect(addr string) (net.Conn, error) { type redirectFormatter struct{} -// Format turns capnslog logs to ngaut logs. -// TODO: remove ngaut log caller stack, "util.go:xxx" +// Format turns capnslog logs to logrus logs. func (rf *redirectFormatter) Format(pkg string, level capnslog.LogLevel, depth int, entries ...interface{}) { if pkg != "" { pkg = fmt.Sprint(pkg, ": ") } - logStr := fmt.Sprint(level.Char(), " | ", pkg, entries) + logStr := fmt.Sprint(pkg, entries) + + // trim square brackets + if len(logStr) > 2 && logStr[0] == '[' { + logStr = logStr[1 : len(logStr)-1] + } switch level { case capnslog.CRITICAL: @@ -276,31 +287,110 @@ func (rf *redirectFormatter) Format(pkg string, level capnslog.LogLevel, depth i // Flush only for implementing Formatter. func (rf *redirectFormatter) Flush() {} +// modifyHook enjects 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, 3) + cnt := runtime.Callers(6, pc) + + for i := 0; i < cnt; i++ { + fu := runtime.FuncForPC(pc[i] - 1) + name := fu.Name() + if !strings.Contains(name, "github.com/Sirupsen/logrus") && + !strings.Contains(name, "github.com/coreos/pkg/capnslog") { + 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": + return log.WarnLevel + case "warning": + return log.WarnLevel + case "debug": + return log.DebugLevel + case "info": + return log.InfoLevel + } + return defaultLogLevel +} + +// for compatability with ngaut/log +type textFormatter struct{} + +// 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{} + } + b.WriteString(entry.Time.Format("2006/01/02 15:04:05")) + if file, ok := entry.Data["file"]; ok { + b.WriteString(fmt.Sprintf(" %s:%v:", file, entry.Data["line"])) + } + b.WriteString(fmt.Sprintf(" [%s] ", entry.Level.String())) + b.WriteString(entry.Message) + for k := range entry.Data { + if k != "file" && k != "line" { + b.WriteString(fmt.Sprintf(" %v=%v", k, entry.Data[k])) + } + } + b.WriteByte('\n') + return b.Bytes(), nil +} + // setLogOutput sets output path for all logs. func setLogOutput(logFile string) error { - // PD log. + // PD log dir := path.Dir(logFile) err := os.MkdirAll(dir, logDirMode) if err != nil { return errors.Trace(err) } - log.SetOutputByName(logFile) - log.SetRotateByDay() - // ETCD log. - capnslog.SetFormatter(&redirectFormatter{}) + // use lumberjack to logrotate + log.SetOutput(&lumberjack.Logger{ + Filename: logFile, + MaxSize: defaultLogMaxSize, // megabytes + MaxBackups: defaultLogMaxBackups, + MaxAge: defaultLogMaxAge, // days + LocalTime: true, + }) return nil } // InitLogger initalizes PD's logger. func InitLogger(cfg *Config) error { - log.SetLevelByString(cfg.LogLevel) - log.SetHighlighting(false) + log.SetLevel(stringToLogLevel(cfg.LogLevel)) + log.AddHook(&contextHook{}) + log.SetFormatter(&textFormatter{}) + + // etcd log + capnslog.SetFormatter(&redirectFormatter{}) // Force redirect etcd log to stderr. if len(cfg.LogFile) == 0 { - capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stderr, false)) return nil } From b99587ae691568efde81b68572e688bac8827fa5 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 12 Apr 2017 02:22:42 +0800 Subject: [PATCH 03/17] server/util: fix typo --- server/util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/util.go b/server/util.go index e0e322516fa..b0b47482d40 100644 --- a/server/util.go +++ b/server/util.go @@ -43,7 +43,7 @@ const ( requestTimeout = etcdutil.DefaultRequestTimeout slowRequestTime = etcdutil.DefaultSlowRequestTime - defaultLogMaxSize = 1 // MB + defaultLogMaxSize = 300 // MB defaultLogMaxBackups = 3 defaultLogMaxAge = 28 // days defaultLogLevel = log.InfoLevel @@ -287,7 +287,7 @@ func (rf *redirectFormatter) Format(pkg string, level capnslog.LogLevel, depth i // Flush only for implementing Formatter. func (rf *redirectFormatter) Flush() {} -// modifyHook enjects file name and line pos into log entry. +// modifyHook injects file name and line pos into log entry. type contextHook struct{} // Fire implements logrus.Hook interface From de7eb725ee2bf593707911600e687b90de531a26 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 12 Apr 2017 11:56:27 +0800 Subject: [PATCH 04/17] server/util: address commit --- server/util.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server/util.go b/server/util.go index b0b47482d40..aac54003e75 100644 --- a/server/util.go +++ b/server/util.go @@ -43,6 +43,7 @@ const ( requestTimeout = etcdutil.DefaultRequestTimeout slowRequestTime = etcdutil.DefaultSlowRequestTime + defaultLogTimeFormat = "2006/01/02 15:04:05" defaultLogMaxSize = 300 // MB defaultLogMaxBackups = 3 defaultLogMaxAge = 28 // days @@ -299,8 +300,9 @@ func (hook *contextHook) Fire(entry *log.Entry) error { for i := 0; i < cnt; i++ { fu := runtime.FuncForPC(pc[i] - 1) name := fu.Name() - if !strings.Contains(name, "github.com/Sirupsen/logrus") && - !strings.Contains(name, "github.com/coreos/pkg/capnslog") { + isSkip := strings.Contains(name, "github.com/Sirupsen/logrus") || + strings.Contains(name, "github.com/coreos/pkg/capnslog") + if !isSkip { file, line := fu.FileLine(pc[i] - 1) entry.Data["file"] = path.Base(file) entry.Data["line"] = line @@ -344,15 +346,15 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) { } else { b = &bytes.Buffer{} } - b.WriteString(entry.Time.Format("2006/01/02 15:04:05")) + b.WriteString(entry.Time.Format(defaultLogTimeFormat)) if file, ok := entry.Data["file"]; ok { - b.WriteString(fmt.Sprintf(" %s:%v:", file, entry.Data["line"])) + fmt.Fprintf(b, " %s:%v:", file, entry.Data["line"]) } - b.WriteString(fmt.Sprintf(" [%s] ", entry.Level.String())) + fmt.Fprintf(b, " [%s] ", entry.Level.String()) b.WriteString(entry.Message) for k := range entry.Data { if k != "file" && k != "line" { - b.WriteString(fmt.Sprintf(" %v=%v", k, entry.Data[k])) + fmt.Fprintf(b, " %v=%v", k, entry.Data[k]) } } b.WriteByte('\n') From 7e82d84a8b93439e3ca47af4850acaee1c290a9e Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 12 Apr 2017 13:31:13 +0800 Subject: [PATCH 05/17] *: isolate logutil subpackage --- cmd/pd-server/main.go | 3 +- conf/config.toml | 4 +- pd-client/client.go | 2 +- pdctl/command/global.go | 4 +- pkg/logutil/log.go | 201 ++++++++++++++++++++++++++++++++++++++++ server/config.go | 11 +-- server/server.go | 1 + server/util.go | 157 ------------------------------- 8 files changed, 215 insertions(+), 168 deletions(-) create mode 100644 pkg/logutil/log.go diff --git a/cmd/pd-server/main.go b/cmd/pd-server/main.go index ab383fac5eb..523c58a422f 100644 --- a/cmd/pd-server/main.go +++ b/cmd/pd-server/main.go @@ -22,6 +22,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/grpc-ecosystem/go-grpc-prometheus" "github.com/juju/errors" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/pd/pkg/metricutil" "github.com/pingcap/pd/server" "github.com/pingcap/pd/server/api" @@ -44,7 +45,7 @@ func main() { log.Fatalf("parse cmd flags error %s\n", err) } - err = server.InitLogger(cfg) + err = logutil.InitLogger(&cfg.Log) if err != nil { log.Fatalf("initalize logger error %s\n", err) } diff --git a/conf/config.toml b/conf/config.toml index 6461cff7cee..9b0143c1707 100644 --- a/conf/config.toml +++ b/conf/config.toml @@ -15,9 +15,11 @@ initial-cluster = "pd=http://127.0.0.1:2380" initial-cluster-state = "new" lease = 3 -log-level = "info" tso-save-interval = "3s" +[log] +log-level = "info" + [metric] # prometheus client push interval, set "0s" to disable prometheus. interval = "15s" diff --git a/pd-client/client.go b/pd-client/client.go index 2f6f45637ca..91dabe3769b 100644 --- a/pd-client/client.go +++ b/pd-client/client.go @@ -20,8 +20,8 @@ import ( "sync" "time" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" "github.com/pingcap/pd/pkg/apiutil" diff --git a/pdctl/command/global.go b/pdctl/command/global.go index 41a07d17302..d04959e652e 100644 --- a/pdctl/command/global.go +++ b/pdctl/command/global.go @@ -23,8 +23,8 @@ import ( "net/url" "os" + log "github.com/Sirupsen/logrus" "github.com/juju/errors" - "github.com/ngaut/log" "github.com/pingcap/pd/pd-client" "github.com/spf13/cobra" ) @@ -88,7 +88,7 @@ func InitPDClient(cmd *cobra.Command) error { if err != nil { return err } - log.SetLevel(log.LOG_LEVEL_NONE) + log.SetLevel(log.InfoLevel) if pdClient != nil { return nil } diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go new file mode 100644 index 00000000000..2b6da3c565a --- /dev/null +++ b/pkg/logutil/log.go @@ -0,0 +1,201 @@ +// 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" + defaultLogMaxSize = 300 // MB + defaultLogMaxBackups = 3 + defaultLogMaxAge = 28 // days + defaultLogLevel = log.InfoLevel + + logDirMode = 0755 +) + +// LogConfig serializes log related config in toml/json. +type LogConfig struct { + // Log level. + Level string `toml:"log-level" json:"log-level"` + // Log file. + Filename string `toml:"log-file" json:"log-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) + + // trim square brackets + if len(logStr) > 2 && logStr[0] == '[' { + logStr = logStr[1 : len(logStr)-1] + } + + 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: + log.Debugf(logStr) + case 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, 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": + return log.WarnLevel + case "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{} + +// 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{} + } + b.WriteString(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 := range entry.Data { + if k != "file" && k != "line" { + fmt.Fprintf(b, " %v=%v", k, entry.Data[k]) + } + } + b.WriteByte('\n') + return b.Bytes(), nil +} + +// setLogOutput sets output path for all logs. +func setLogOutput(filename string) error { + // PD log + dir := path.Dir(filename) + err := os.MkdirAll(dir, logDirMode) + if err != nil { + return errors.Trace(err) + } + + // use lumberjack to logrotate + log.SetOutput(&lumberjack.Logger{ + Filename: filename, + MaxSize: defaultLogMaxSize, // megabytes + MaxBackups: defaultLogMaxBackups, + MaxAge: defaultLogMaxAge, // days + LocalTime: true, + }) + + return nil +} + +// InitLogger initalizes PD's logger. +func InitLogger(cfg *LogConfig) error { + log.SetLevel(stringToLogLevel(cfg.Level)) + log.AddHook(&contextHook{}) + log.SetFormatter(&textFormatter{}) + + // etcd log + capnslog.SetFormatter(&redirectFormatter{}) + + if len(cfg.Filename) == 0 { + return nil + } + + err := setLogOutput(cfg.Filename) + if err != nil { + return errors.Trace(err) + } + + return nil +} diff --git a/server/config.go b/server/config.go index c156c0e8825..576458666b2 100644 --- a/server/config.go +++ b/server/config.go @@ -25,6 +25,7 @@ import ( "github.com/BurntSushi/toml" "github.com/coreos/etcd/embed" "github.com/juju/errors" + "github.com/pingcap/pd/pkg/logutil" "github.com/pingcap/pd/pkg/metricutil" "github.com/pingcap/pd/pkg/testutil" "github.com/pingcap/pd/pkg/typeutil" @@ -56,10 +57,8 @@ type Config struct { // Etcd onlys support seoncds TTL, so here is second too. LeaderLease int64 `toml:"lease" json:"lease"` - // Log level. - LogLevel string `toml:"log-level" json:"log-level"` - // Log file. - LogFile string `toml:"log-file" json:"log-file"` + // Log related config. + Log logutil.LogConfig `toml:"log" json:"log"` // TsoSaveInterval is the interval to save timestamp. TsoSaveInterval typeutil.Duration `toml:"tso-save-interval" json:"tso-save-interval"` @@ -107,8 +106,8 @@ func NewConfig() *Config { fs.StringVar(&cfg.InitialCluster, "initial-cluster", "", "initial cluster configuration for bootstrapping, e,g. pd=http://127.0.0.1:2380") fs.StringVar(&cfg.Join, "join", "", "join to an existing cluster (usage: cluster's '${advertise-client-urls}'") - fs.StringVar(&cfg.LogLevel, "L", "info", "log level: debug, info, warn, error, fatal") - fs.StringVar(&cfg.LogFile, "log-file", "", "log file path") + fs.StringVar(&cfg.Log.Level, "L", "info", "log level: debug, info, warn, error, fatal") + fs.StringVar(&cfg.Log.Filename, "log-file", "", "log file path") return cfg } diff --git a/server/server.go b/server/server.go index ef841ea6808..69120db73ea 100644 --- a/server/server.go +++ b/server/server.go @@ -98,6 +98,7 @@ func NewServer(cfg *Config) (*Server, error) { } go systimemon.StartMonitor(time.Now, func() { + log.Errorf("system time jumps backward") timeJumpBackCounter.Inc() }) return s, nil diff --git a/server/util.go b/server/util.go index aac54003e75..ad5c3dbbe9a 100644 --- a/server/util.go +++ b/server/util.go @@ -20,23 +20,18 @@ import ( "math/rand" "net" "net/http" - "os" - "path" "reflect" - "runtime" "strings" "time" log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" - "github.com/coreos/pkg/capnslog" "github.com/golang/protobuf/proto" "github.com/juju/errors" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" "github.com/pingcap/pd/pkg/etcdutil" "golang.org/x/net/context" - lumberjack "gopkg.in/natefinch/lumberjack.v2" ) const ( @@ -252,158 +247,6 @@ func rpcConnect(addr string) (net.Conn, error) { return nil, errors.Errorf("connect to %s failed", addr) } -type redirectFormatter struct{} - -// Format turns capnslog logs to logrus logs. -func (rf *redirectFormatter) Format(pkg string, level capnslog.LogLevel, depth int, entries ...interface{}) { - if pkg != "" { - pkg = fmt.Sprint(pkg, ": ") - } - - logStr := fmt.Sprint(pkg, entries) - - // trim square brackets - if len(logStr) > 2 && logStr[0] == '[' { - logStr = logStr[1 : len(logStr)-1] - } - - 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: - log.Debugf(logStr) - case capnslog.TRACE: - log.Debugf(logStr) - } -} - -// Flush only for implementing Formatter. -func (rf *redirectFormatter) Flush() {} - -// 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, 3) - cnt := runtime.Callers(6, pc) - - for i := 0; i < cnt; i++ { - fu := runtime.FuncForPC(pc[i] - 1) - name := fu.Name() - isSkip := strings.Contains(name, "github.com/Sirupsen/logrus") || - strings.Contains(name, "github.com/coreos/pkg/capnslog") - if !isSkip { - 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": - return log.WarnLevel - case "warning": - return log.WarnLevel - case "debug": - return log.DebugLevel - case "info": - return log.InfoLevel - } - return defaultLogLevel -} - -// for compatability with ngaut/log -type textFormatter struct{} - -// 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{} - } - b.WriteString(entry.Time.Format(defaultLogTimeFormat)) - if file, ok := entry.Data["file"]; ok { - fmt.Fprintf(b, " %s:%v:", file, entry.Data["line"]) - } - fmt.Fprintf(b, " [%s] ", entry.Level.String()) - b.WriteString(entry.Message) - for k := range entry.Data { - if k != "file" && k != "line" { - fmt.Fprintf(b, " %v=%v", k, entry.Data[k]) - } - } - b.WriteByte('\n') - return b.Bytes(), nil -} - -// setLogOutput sets output path for all logs. -func setLogOutput(logFile string) error { - // PD log - dir := path.Dir(logFile) - err := os.MkdirAll(dir, logDirMode) - if err != nil { - return errors.Trace(err) - } - - // use lumberjack to logrotate - log.SetOutput(&lumberjack.Logger{ - Filename: logFile, - MaxSize: defaultLogMaxSize, // megabytes - MaxBackups: defaultLogMaxBackups, - MaxAge: defaultLogMaxAge, // days - LocalTime: true, - }) - - return nil -} - -// InitLogger initalizes PD's logger. -func InitLogger(cfg *Config) error { - log.SetLevel(stringToLogLevel(cfg.LogLevel)) - log.AddHook(&contextHook{}) - log.SetFormatter(&textFormatter{}) - - // etcd log - capnslog.SetFormatter(&redirectFormatter{}) - - // Force redirect etcd log to stderr. - if len(cfg.LogFile) == 0 { - return nil - } - - err := setLogOutput(cfg.LogFile) - if err != nil { - return errors.Trace(err) - } - - return nil -} - // GetMembers return a slice of Members. func GetMembers(etcdClient *clientv3.Client) ([]*pdpb.Member, error) { listResp, err := etcdutil.ListEtcdMembers(etcdClient) From ddf4e8a0401d2bf41931e165c2b0384fde1cb794 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 12 Apr 2017 13:31:45 +0800 Subject: [PATCH 06/17] pkg/logutil: add testcase --- pkg/logutil/log_test.go | 67 +++++++++++++++++++++++++++++++++++++++++ server/server_test.go | 2 +- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 pkg/logutil/log_test.go diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go new file mode 100644 index 00000000000..6d70930f860 --- /dev/null +++ b/pkg/logutil/log_test.go @@ -0,0 +1,67 @@ +// 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" + "testing" + + log "github.com/Sirupsen/logrus" + "github.com/coreos/pkg/capnslog" + . "github.com/pingcap/check" +) + +const ( + logPattern = `\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d ([\w_%!$@.,+~-]+|\\.)+:\d+: \[(fatal|error|warning|info|debug)\] .*?\n` +) + +func Test(t *testing.T) { + TestingT(t) +} + +var _ = Suite(&testLogSuite{}) + +type testLogSuite struct { + buf *bytes.Buffer +} + +func (s *testLogSuite) SetUpSuite(c *C) { + s.buf = &bytes.Buffer{} +} + +// TestLogging assure log format and log redirection works. +func (s *testLogSuite) TestLogging(c *C) { + conf := &LogConfig{Level: "warn", Filename: ""} + c.Assert(InitLogger(conf), IsNil) + + log.SetOutput(s.buf) + + tlog := capnslog.NewPackageLogger("github.com/pingcap/pd/pkg/logutil", "test") + + tlog.Infof("this message should not be sent to buf") + c.Assert(s.buf.Len(), Equals, 0) + + tlog.Warningf("this message should be sent to buf") + entry, err := s.buf.ReadString('\n') + + c.Assert(err, IsNil) + c.Assert(entry, Matches, logPattern) + + log.Warnf("this message if from logrus") + entry, err = s.buf.ReadString('\n') + + c.Assert(err, IsNil) + c.Assert(entry, Matches, logPattern) + +} diff --git a/server/server_test.go b/server/server_test.go index e26f198c97f..71b8a27d807 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -20,8 +20,8 @@ import ( "testing" "time" + log "github.com/Sirupsen/logrus" "github.com/coreos/etcd/clientv3" - "github.com/ngaut/log" . "github.com/pingcap/check" "github.com/pingcap/pd/pkg/testutil" ) From f398ee5c2dcfb2bb9a49dfe5c07a07c800038530 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 12 Apr 2017 17:58:37 +0800 Subject: [PATCH 07/17] *: minor fix, robust error handling --- cmd/pd-server/main.go | 4 ++-- pkg/logutil/log.go | 16 +++++++++++++--- pkg/logutil/log_test.go | 7 +++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cmd/pd-server/main.go b/cmd/pd-server/main.go index 523c58a422f..d6e5e37bfb0 100644 --- a/cmd/pd-server/main.go +++ b/cmd/pd-server/main.go @@ -42,12 +42,12 @@ func main() { case flag.ErrHelp: os.Exit(0) default: - log.Fatalf("parse cmd flags error %s\n", err) + log.Fatalf("parse cmd flags error: %s\n", err) } err = logutil.InitLogger(&cfg.Log) if err != nil { - log.Fatalf("initalize logger error %s\n", err) + log.Fatalf("initalize logger error: %s\n", err) } server.LogPDInfo() diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go index 2b6da3c565a..bdae37f4238 100644 --- a/pkg/logutil/log.go +++ b/pkg/logutil/log.go @@ -94,7 +94,7 @@ 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, 3) + pc := make([]uintptr, 3) cnt := runtime.Callers(6, pc) for i := 0; i < cnt; i++ { @@ -161,6 +161,11 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) { // setLogOutput sets output path for all logs. func setLogOutput(filename string) error { // PD log + if st, err := os.Stat(filename); err == nil { + if st.IsDir() { + return errors.New("can't use directory as log file name") + } + } dir := path.Dir(filename) err := os.MkdirAll(dir, logDirMode) if err != nil { @@ -168,14 +173,19 @@ func setLogOutput(filename string) error { } // use lumberjack to logrotate - log.SetOutput(&lumberjack.Logger{ + output := &lumberjack.Logger{ Filename: filename, MaxSize: defaultLogMaxSize, // megabytes MaxBackups: defaultLogMaxBackups, MaxAge: defaultLogMaxAge, // days LocalTime: true, - }) + } + + if _, err := output.Write([]byte{}); err != nil { + return errors.Errorf("log file is not writable: %v", err) + } + log.SetOutput(output) return nil } diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go index 6d70930f860..34a2e5851ac 100644 --- a/pkg/logutil/log_test.go +++ b/pkg/logutil/log_test.go @@ -49,19 +49,18 @@ func (s *testLogSuite) TestLogging(c *C) { tlog := capnslog.NewPackageLogger("github.com/pingcap/pd/pkg/logutil", "test") - tlog.Infof("this message should not be sent to buf") + tlog.Infof("[this message should not be sent to buf]") c.Assert(s.buf.Len(), Equals, 0) - tlog.Warningf("this message should be sent to buf") + tlog.Warningf("[this message should be sent to buf]") entry, err := s.buf.ReadString('\n') c.Assert(err, IsNil) c.Assert(entry, Matches, logPattern) - log.Warnf("this message if from logrus") + log.Warnf("this message comes from logrus") entry, err = s.buf.ReadString('\n') c.Assert(err, IsNil) c.Assert(entry, Matches, logPattern) - } From f82f0622a62313b57d1a4ddd6a2f1207733e2ab8 Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 13 Apr 2017 12:04:00 +0800 Subject: [PATCH 08/17] pkg/logutil: add filename check test --- pkg/logutil/log_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go index 34a2e5851ac..ff8a7e7e09b 100644 --- a/pkg/logutil/log_test.go +++ b/pkg/logutil/log_test.go @@ -15,6 +15,7 @@ package logutil import ( "bytes" + "strings" "testing" log "github.com/Sirupsen/logrus" @@ -23,7 +24,8 @@ import ( ) const ( - logPattern = `\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d ([\w_%!$@.,+~-]+|\\.)+:\d+: \[(fatal|error|warning|info|debug)\] .*?\n` + logPattern = `\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d ([\w_%!$@.,+~-]+|\\.)+:\d+: \[(fatal|error|warning|info|debug)\] .*?\n` + thisFilename = "log_test.go" ) func Test(t *testing.T) { @@ -54,13 +56,14 @@ func (s *testLogSuite) TestLogging(c *C) { tlog.Warningf("[this message should be sent to buf]") entry, err := s.buf.ReadString('\n') - c.Assert(err, IsNil) c.Assert(entry, Matches, logPattern) + // All capnslog log will be trigered in logutil/log.go + c.Assert(strings.Contains(entry, "log.go"), IsTrue) log.Warnf("this message comes from logrus") entry, err = s.buf.ReadString('\n') - c.Assert(err, IsNil) c.Assert(entry, Matches, logPattern) + c.Assert(strings.Contains(entry, thisFilename), IsTrue) } From 805fa46bc4abb7dd3845286b18af3b9f45f8f9af Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 13 Apr 2017 12:18:24 +0800 Subject: [PATCH 09/17] conf: log config --- conf/config.toml | 3 ++- pkg/logutil/log.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/conf/config.toml b/conf/config.toml index 9b0143c1707..05bd8a5457a 100644 --- a/conf/config.toml +++ b/conf/config.toml @@ -18,7 +18,8 @@ lease = 3 tso-save-interval = "3s" [log] -log-level = "info" +level = "info" +#file = "" [metric] # prometheus client push interval, set "0s" to disable prometheus. diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go index bdae37f4238..720603bc686 100644 --- a/pkg/logutil/log.go +++ b/pkg/logutil/log.go @@ -40,9 +40,9 @@ const ( // LogConfig serializes log related config in toml/json. type LogConfig struct { // Log level. - Level string `toml:"log-level" json:"log-level"` + Level string `toml:"level" json:"level"` // Log file. - Filename string `toml:"log-file" json:"log-file"` + Filename string `toml:"file" json:"file"` } // redirectFormatter will redirect etcd logs to logrus logs. From 59c005e4ea59af7c08ef51c946516cc0a1aa3ef9 Mon Sep 17 00:00:00 2001 From: andelf Date: Fri, 14 Apr 2017 18:55:46 +0800 Subject: [PATCH 10/17] *: fix log config backward compatibility --- cmd/pd-server/main.go | 4 ++++ server/config.go | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/pd-server/main.go b/cmd/pd-server/main.go index d6e5e37bfb0..5c7d27017ac 100644 --- a/cmd/pd-server/main.go +++ b/cmd/pd-server/main.go @@ -52,6 +52,10 @@ func main() { server.LogPDInfo() + for _, msg := range cfg.WarningMsgs { + log.Warn(msg) + } + // TODO: Make it configurable if it has big impact on performance. grpc_prometheus.EnableHandlingTimeHistogram() diff --git a/server/config.go b/server/config.go index 576458666b2..bcd64f7ed62 100644 --- a/server/config.go +++ b/server/config.go @@ -60,6 +60,10 @@ type Config struct { // Log related config. Log logutil.LogConfig `toml:"log" json:"log"` + // Backward compatibility. + LogFileDeprecated string `toml:"log-file" json:"log-file"` + LogLevelDeprecated string `toml:"log-level" json:"log-level"` + // TsoSaveInterval is the interval to save timestamp. TsoSaveInterval typeutil.Duration `toml:"tso-save-interval" json:"tso-save-interval"` @@ -84,6 +88,9 @@ type Config struct { electionMs uint64 configFile string + + // For all warnings during parsing. + WarningMsgs []string } // NewConfig creates a new config. @@ -106,7 +113,7 @@ func NewConfig() *Config { fs.StringVar(&cfg.InitialCluster, "initial-cluster", "", "initial cluster configuration for bootstrapping, e,g. pd=http://127.0.0.1:2380") fs.StringVar(&cfg.Join, "join", "", "join to an existing cluster (usage: cluster's '${advertise-client-urls}'") - fs.StringVar(&cfg.Log.Level, "L", "info", "log level: debug, info, warn, error, fatal") + fs.StringVar(&cfg.Log.Level, "L", "", "log level: debug, info, warn, error, fatal (default 'info')") fs.StringVar(&cfg.Log.Filename, "log-file", "", "log file path") return cfg @@ -175,6 +182,19 @@ func (c *Config) Parse(arguments []string) error { if err != nil { return errors.Trace(err) } + + // Backward compatibility for toml config + fmt.Printf("fuck %+v\n", c) + if c.LogFileDeprecated != "" && c.Log.Filename == "" { + c.Log.Filename = c.LogFileDeprecated + msg := fmt.Sprintf("log-file in %s is deprecated, use [log] instead", c.configFile) + c.WarningMsgs = append(c.WarningMsgs, msg) + } + if c.LogLevelDeprecated != "" && c.Log.Level == "" { + c.Log.Level = c.LogLevelDeprecated + msg := fmt.Sprintf("log-level in %s is deprecated, use [log] instead", c.configFile) + c.WarningMsgs = append(c.WarningMsgs, msg) + } } // Parse again to replace with command line options. From a980205d682584504e0ef5a8b6c76dd572ea75a3 Mon Sep 17 00:00:00 2001 From: andelf Date: Fri, 14 Apr 2017 19:02:19 +0800 Subject: [PATCH 11/17] *: address comment --- pkg/logutil/log.go | 12 ++++-------- pkg/logutil/log_test.go | 5 ++--- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go index 720603bc686..6bd803b97d8 100644 --- a/pkg/logutil/log.go +++ b/pkg/logutil/log.go @@ -72,9 +72,7 @@ func (rf *redirectFormatter) Format(pkg string, level capnslog.LogLevel, depth i log.Infof(logStr) case capnslog.INFO: log.Infof(logStr) - case capnslog.DEBUG: - log.Debugf(logStr) - case capnslog.TRACE: + case capnslog.DEBUG, capnslog.TRACE: log.Debugf(logStr) } } @@ -121,9 +119,7 @@ func stringToLogLevel(level string) log.Level { return log.FatalLevel case "error": return log.ErrorLevel - case "warn": - return log.WarnLevel - case "warning": + case "warn", "warning": return log.WarnLevel case "debug": return log.DebugLevel @@ -149,9 +145,9 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) { fmt.Fprintf(b, " %s:%v:", file, entry.Data["line"]) } fmt.Fprintf(b, " [%s] %s", entry.Level.String(), entry.Message) - for k := range entry.Data { + for k, v := range entry.Data { if k != "file" && k != "line" { - fmt.Fprintf(b, " %v=%v", k, entry.Data[k]) + fmt.Fprintf(b, " %v=%v", k, v) } } b.WriteByte('\n') diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go index ff8a7e7e09b..47529d3acd6 100644 --- a/pkg/logutil/log_test.go +++ b/pkg/logutil/log_test.go @@ -24,8 +24,7 @@ import ( ) const ( - logPattern = `\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d ([\w_%!$@.,+~-]+|\\.)+:\d+: \[(fatal|error|warning|info|debug)\] .*?\n` - thisFilename = "log_test.go" + logPattern = `\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d ([\w_%!$@.,+~-]+|\\.)+:\d+: \[(fatal|error|warning|info|debug)\] .*?\n` ) func Test(t *testing.T) { @@ -65,5 +64,5 @@ func (s *testLogSuite) TestLogging(c *C) { entry, err = s.buf.ReadString('\n') c.Assert(err, IsNil) c.Assert(entry, Matches, logPattern) - c.Assert(strings.Contains(entry, thisFilename), IsTrue) + c.Assert(strings.Contains(entry, "log_test.go"), IsTrue) } From 376c8f01906153cd3d3083ee036875676f189e5f Mon Sep 17 00:00:00 2001 From: andelf Date: Fri, 14 Apr 2017 22:01:14 +0800 Subject: [PATCH 12/17] config: fix debug print --- server/config.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/config.go b/server/config.go index bcd64f7ed62..037d85e9525 100644 --- a/server/config.go +++ b/server/config.go @@ -184,7 +184,6 @@ func (c *Config) Parse(arguments []string) error { } // Backward compatibility for toml config - fmt.Printf("fuck %+v\n", c) if c.LogFileDeprecated != "" && c.Log.Filename == "" { c.Log.Filename = c.LogFileDeprecated msg := fmt.Sprintf("log-file in %s is deprecated, use [log] instead", c.configFile) From 2013cfaaf498a6694a9f62e11b0eaca56cdaeaca Mon Sep 17 00:00:00 2001 From: andelf Date: Tue, 18 Apr 2017 14:03:54 +0800 Subject: [PATCH 13/17] pkg/logutil: remove trim square bracket --- pkg/logutil/log.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go index 6bd803b97d8..49fe0608fa4 100644 --- a/pkg/logutil/log.go +++ b/pkg/logutil/log.go @@ -56,11 +56,6 @@ func (rf *redirectFormatter) Format(pkg string, level capnslog.LogLevel, depth i logStr := fmt.Sprint(pkg, entries) - // trim square brackets - if len(logStr) > 2 && logStr[0] == '[' { - logStr = logStr[1 : len(logStr)-1] - } - switch level { case capnslog.CRITICAL: log.Fatalf(logStr) From 7bb95551017d340acbe1a588f8515305a8d22acd Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 19 Apr 2017 11:33:25 +0800 Subject: [PATCH 14/17] pkg/logutil: add tests --- pkg/logutil/log_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go index 47529d3acd6..47008523731 100644 --- a/pkg/logutil/log_test.go +++ b/pkg/logutil/log_test.go @@ -41,6 +41,16 @@ func (s *testLogSuite) SetUpSuite(c *C) { s.buf = &bytes.Buffer{} } +func (s *testLogSuite) TestStringToLogLevel(c *C) { + c.Assert(stringToLogLevel("fatal"), Equals, log.FatalLevel) + c.Assert(stringToLogLevel("ERROR"), Equals, log.ErrorLevel) + c.Assert(stringToLogLevel("warn"), Equals, log.WarnLevel) + c.Assert(stringToLogLevel("warning"), Equals, log.WarnLevel) + c.Assert(stringToLogLevel("debug"), Equals, log.DebugLevel) + c.Assert(stringToLogLevel("info"), Equals, log.InfoLevel) + c.Assert(stringToLogLevel("whatever"), Equals, log.InfoLevel) +} + // TestLogging assure log format and log redirection works. func (s *testLogSuite) TestLogging(c *C) { conf := &LogConfig{Level: "warn", Filename: ""} From acabc9b3ae580aa2fd585223a455471c3247af76 Mon Sep 17 00:00:00 2001 From: andelf Date: Mon, 24 Apr 2017 14:39:01 +0800 Subject: [PATCH 15/17] pkg/logutil: isolate file log config --- conf/config.toml | 4 +++- pkg/logutil/log.go | 11 ++++++++--- pkg/logutil/log_test.go | 2 +- server/config.go | 8 ++++---- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/conf/config.toml b/conf/config.toml index 05bd8a5457a..71f1df06986 100644 --- a/conf/config.toml +++ b/conf/config.toml @@ -19,7 +19,9 @@ tso-save-interval = "3s" [log] level = "info" -#file = "" + +#[log.file] +#filename = "" [metric] # prometheus client push interval, set "0s" to disable prometheus. diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go index 49fe0608fa4..630d293f3cc 100644 --- a/pkg/logutil/log.go +++ b/pkg/logutil/log.go @@ -37,12 +37,17 @@ const ( logDirMode = 0755 ) +// FileLogConfig serializes file log related config in toml/json. +type FileLogConfig struct { + Filename string `toml:"filename" json:"filename"` +} + // LogConfig serializes log related config in toml/json. type LogConfig struct { // Log level. Level string `toml:"level" json:"level"` // Log file. - Filename string `toml:"file" json:"file"` + File FileLogConfig `toml:"file" json:"file"` } // redirectFormatter will redirect etcd logs to logrus logs. @@ -189,11 +194,11 @@ func InitLogger(cfg *LogConfig) error { // etcd log capnslog.SetFormatter(&redirectFormatter{}) - if len(cfg.Filename) == 0 { + if len(cfg.File.Filename) == 0 { return nil } - err := setLogOutput(cfg.Filename) + err := setLogOutput(cfg.File.Filename) if err != nil { return errors.Trace(err) } diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go index 47008523731..3668029272b 100644 --- a/pkg/logutil/log_test.go +++ b/pkg/logutil/log_test.go @@ -53,7 +53,7 @@ func (s *testLogSuite) TestStringToLogLevel(c *C) { // TestLogging assure log format and log redirection works. func (s *testLogSuite) TestLogging(c *C) { - conf := &LogConfig{Level: "warn", Filename: ""} + conf := &LogConfig{Level: "warn", File: FileLogConfig{}} c.Assert(InitLogger(conf), IsNil) log.SetOutput(s.buf) diff --git a/server/config.go b/server/config.go index 037d85e9525..27e1ce06507 100644 --- a/server/config.go +++ b/server/config.go @@ -114,7 +114,7 @@ func NewConfig() *Config { fs.StringVar(&cfg.Join, "join", "", "join to an existing cluster (usage: cluster's '${advertise-client-urls}'") fs.StringVar(&cfg.Log.Level, "L", "", "log level: debug, info, warn, error, fatal (default 'info')") - fs.StringVar(&cfg.Log.Filename, "log-file", "", "log file path") + fs.StringVar(&cfg.Log.File.Filename, "log-file", "", "log file path") return cfg } @@ -184,9 +184,9 @@ func (c *Config) Parse(arguments []string) error { } // Backward compatibility for toml config - if c.LogFileDeprecated != "" && c.Log.Filename == "" { - c.Log.Filename = c.LogFileDeprecated - msg := fmt.Sprintf("log-file in %s is deprecated, use [log] instead", c.configFile) + if c.LogFileDeprecated != "" && c.Log.File.Filename == "" { + c.Log.File.Filename = c.LogFileDeprecated + msg := fmt.Sprintf("log-file in %s is deprecated, use [log.file] instead", c.configFile) c.WarningMsgs = append(c.WarningMsgs, msg) } if c.LogLevelDeprecated != "" && c.Log.Level == "" { From 6144c5c8fde28d0c5491eb0cf67c9312b6a18934 Mon Sep 17 00:00:00 2001 From: andelf Date: Mon, 24 Apr 2017 14:50:54 +0800 Subject: [PATCH 16/17] ci: fix circle-ci under forked projects --- circle.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/circle.yml b/circle.yml index 9c18466de7c..74510dcdd56 100644 --- a/circle.yml +++ b/circle.yml @@ -1,6 +1,8 @@ machine: environment: GODIST: "go1.7.3.linux-amd64.tar.gz" + GITHUB_PROJECT_USERNAME: pingcap + GITHUB_PROJECT_REPONAME: pd post: - mkdir -p download - test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST @@ -11,10 +13,10 @@ dependencies: cache_directories: - ~/download override: - - mkdir -p $HOME/.go_project/src/github.com/$CIRCLE_PROJECT_USERNAME - - ln -fs $HOME/$CIRCLE_PROJECT_REPONAME $HOME/.go_project/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME + - mkdir -p $HOME/.go_project/src/github.com/$GITHUB_PROJECT_USERNAME + - ln -fs $HOME/$CIRCLE_PROJECT_REPONAME $HOME/.go_project/src/github.com/$GITHUB_PROJECT_USERNAME/$GITHUB_PROJECT_REPONAME - echo 'export GOPATH=$GOPATH:$HOME/.go_project' >> ~/.circlerc test: override: - - cd $HOME/.go_project/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME && make dev + - cd $HOME/.go_project/src/github.com/$GITHUB_PROJECT_USERNAME/$GITHUB_PROJECT_REPONAME && make dev From 2f600b87ac7149bf570fd999deb9dda6dcfd0eca Mon Sep 17 00:00:00 2001 From: andelf Date: Mon, 24 Apr 2017 15:20:16 +0800 Subject: [PATCH 17/17] merge origin/master --- circle.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/circle.yml b/circle.yml index f86c6517e26..ee7878dd864 100644 --- a/circle.yml +++ b/circle.yml @@ -3,8 +3,6 @@ machine: GITHUB_PROJECT_USERNAME: pingcap GITHUB_PROJECT_REPONAME: pd GODIST: "go1.7.3.linux-amd64.tar.gz" - GITHUB_PROJECT_USERNAME: pingcap - GITHUB_PROJECT_REPONAME: pd post: - mkdir -p download - test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST