2015-09-18 02:49:06 -04:00
|
|
|
/*
|
2017-03-23 19:36:00 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc.
|
2015-09-18 02:49:06 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2015-09-18 02:49:06 -04:00
|
|
|
|
|
|
|
import (
|
2016-10-11 03:50:27 -04:00
|
|
|
"fmt"
|
2017-03-23 19:36:00 -04:00
|
|
|
"io/ioutil"
|
2016-10-12 04:03:50 -04:00
|
|
|
"path"
|
2016-10-11 03:50:27 -04:00
|
|
|
"runtime"
|
2016-05-25 05:32:35 -04:00
|
|
|
"strings"
|
2015-09-18 02:49:06 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-03-23 19:36:00 -04:00
|
|
|
"github.com/minio/mc/pkg/console"
|
2017-11-25 14:58:29 -05:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2015-09-18 02:49:06 -04:00
|
|
|
)
|
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
var log = NewLogger()
|
2015-09-18 02:49:06 -04:00
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
// LogTarget - interface for log target.
|
|
|
|
type LogTarget interface {
|
|
|
|
Fire(entry *logrus.Entry) error
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// BaseLogTarget - base log target.
|
|
|
|
type BaseLogTarget struct {
|
|
|
|
Enable bool `json:"enable"`
|
|
|
|
formatter logrus.Formatter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logger - higher level logger.
|
|
|
|
type Logger struct {
|
|
|
|
logger *logrus.Logger
|
|
|
|
consoleTarget ConsoleLogger
|
|
|
|
targets []LogTarget
|
|
|
|
quiet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTarget - add logger to this hook.
|
|
|
|
func (log *Logger) AddTarget(logTarget LogTarget) {
|
|
|
|
log.targets = append(log.targets, logTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConsoleTarget - sets console target to this hook.
|
|
|
|
func (log *Logger) SetConsoleTarget(consoleTarget ConsoleLogger) {
|
|
|
|
log.consoleTarget = consoleTarget
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire - log entry handler to save logs.
|
|
|
|
func (log *Logger) Fire(entry *logrus.Entry) (err error) {
|
|
|
|
if err = log.consoleTarget.Fire(entry); err != nil {
|
|
|
|
log.Printf("Unable to log to console target. %s\n", err)
|
2016-10-22 05:24:34 -04:00
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
|
|
|
|
for _, logTarget := range log.targets {
|
|
|
|
if err = logTarget.Fire(entry); err != nil {
|
|
|
|
log.Printf("Unable to log to target %s. %s\n", logTarget, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2016-10-22 05:24:34 -04:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
// Levels - returns list of log levels support.
|
|
|
|
func (log *Logger) Levels() []logrus.Level {
|
|
|
|
return []logrus.Level{
|
|
|
|
logrus.PanicLevel,
|
|
|
|
logrus.FatalLevel,
|
|
|
|
logrus.ErrorLevel,
|
|
|
|
logrus.WarnLevel,
|
|
|
|
logrus.InfoLevel,
|
|
|
|
logrus.DebugLevel,
|
2015-09-18 02:49:06 -04:00
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// EnableQuiet - sets quiet option.
|
|
|
|
func (log *Logger) EnableQuiet() {
|
|
|
|
log.quiet = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Println - wrapper to console.Println() with quiet flag.
|
|
|
|
func (log *Logger) Println(args ...interface{}) {
|
|
|
|
if !log.quiet {
|
|
|
|
console.Println(args...)
|
2015-09-18 02:49:06 -04:00
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Printf - wrapper to console.Printf() with quiet flag.
|
|
|
|
func (log *Logger) Printf(format string, args ...interface{}) {
|
|
|
|
if !log.quiet {
|
|
|
|
console.Printf(format, args...)
|
2015-09-18 02:49:06 -04:00
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
}
|
2016-08-25 12:39:01 -04:00
|
|
|
|
2017-04-01 04:06:06 -04:00
|
|
|
// NewLogger - returns a new initialized logger.
|
2017-03-23 19:36:00 -04:00
|
|
|
func NewLogger() *Logger {
|
|
|
|
logger := logrus.New()
|
|
|
|
logger.Out = ioutil.Discard
|
|
|
|
logger.Level = logrus.DebugLevel
|
|
|
|
|
2017-04-01 04:06:06 -04:00
|
|
|
l := &Logger{
|
2017-03-23 19:36:00 -04:00
|
|
|
logger: logger,
|
|
|
|
consoleTarget: NewConsoleLogger(),
|
2016-11-23 14:35:04 -05:00
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
|
2017-04-01 04:06:06 -04:00
|
|
|
// Adds a console logger.
|
|
|
|
logger.Hooks.Add(l)
|
2017-03-23 19:36:00 -04:00
|
|
|
|
2017-04-01 04:06:06 -04:00
|
|
|
return l
|
2015-09-19 05:36:50 -04:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
func getSource() string {
|
|
|
|
var funcName string
|
|
|
|
pc, filename, lineNum, ok := runtime.Caller(2)
|
|
|
|
if ok {
|
|
|
|
filename = path.Base(filename)
|
|
|
|
funcName = strings.TrimPrefix(runtime.FuncForPC(pc).Name(), "github.com/minio/minio/cmd.")
|
|
|
|
} else {
|
|
|
|
filename = "<unknown>"
|
|
|
|
lineNum = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("[%s:%d:%s()]", filename, lineNum, funcName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logIf(level logrus.Level, source string, err error, msg string, data ...interface{}) {
|
|
|
|
isErrIgnored := func(err error) (ok bool) {
|
2017-11-25 14:58:29 -05:00
|
|
|
err = errors.Cause(err)
|
2017-03-23 19:36:00 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case BucketNotFound, BucketNotEmpty, BucketExists:
|
|
|
|
ok = true
|
2017-10-22 01:30:34 -04:00
|
|
|
case ObjectNotFound, ObjectExistsAsDirectory:
|
|
|
|
ok = true
|
|
|
|
case BucketPolicyNotFound, InvalidUploadID:
|
2017-03-23 19:36:00 -04:00
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil || isErrIgnored(err) {
|
2015-09-19 05:36:50 -04:00
|
|
|
return
|
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
|
2016-05-16 17:31:28 -04:00
|
|
|
fields := logrus.Fields{
|
2016-11-23 19:36:26 -05:00
|
|
|
"source": source,
|
|
|
|
"cause": err.Error(),
|
2015-09-19 05:36:50 -04:00
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
|
2017-11-25 14:58:29 -05:00
|
|
|
if terr, ok := err.(*errors.Error); ok {
|
|
|
|
fields["stack"] = strings.Join(terr.Stack(), " ")
|
2015-09-19 05:36:50 -04:00
|
|
|
}
|
2017-02-21 04:32:05 -05:00
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
switch level {
|
|
|
|
case logrus.PanicLevel:
|
|
|
|
log.logger.WithFields(fields).Panicf(msg, data...)
|
|
|
|
case logrus.FatalLevel:
|
|
|
|
log.logger.WithFields(fields).Fatalf(msg, data...)
|
|
|
|
case logrus.ErrorLevel:
|
|
|
|
log.logger.WithFields(fields).Errorf(msg, data...)
|
|
|
|
case logrus.WarnLevel:
|
|
|
|
log.logger.WithFields(fields).Warnf(msg, data...)
|
|
|
|
case logrus.InfoLevel:
|
|
|
|
log.logger.WithFields(fields).Infof(msg, data...)
|
|
|
|
default:
|
|
|
|
log.logger.WithFields(fields).Debugf(msg, data...)
|
2016-11-23 14:35:04 -05:00
|
|
|
}
|
2015-09-18 02:49:06 -04:00
|
|
|
}
|
2016-11-23 19:36:26 -05:00
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
func errorIf(err error, msg string, data ...interface{}) {
|
|
|
|
logIf(logrus.ErrorLevel, getSource(), err, msg, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fatalIf(err error, msg string, data ...interface{}) {
|
|
|
|
logIf(logrus.FatalLevel, getSource(), err, msg, data...)
|
2016-11-23 19:36:26 -05:00
|
|
|
}
|