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 (
|
2017-03-23 19:36:00 -04:00
|
|
|
"errors"
|
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"
|
2016-11-23 14:35:04 -05:00
|
|
|
"sync"
|
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"
|
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
|
|
|
type loggers struct {
|
2017-02-09 18:20:54 -05:00
|
|
|
sync.RWMutex
|
2017-03-23 19:36:00 -04:00
|
|
|
Console ConsoleLogger `json:"console"`
|
|
|
|
File FileLogger `json:"file"`
|
config/main: Re-write config files - add to new config v3
- New config format.
```
{
"version": "3",
"address": ":9000",
"backend": {
"type": "fs",
"disk": "/path"
},
"credential": {
"accessKey": "WLGDGYAQYIGI833EV05A",
"secretKey": "BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF"
},
"region": "us-east-1",
"logger": {
"file": {
"enable": false,
"fileName": "",
"level": "error"
},
"syslog": {
"enable": false,
"address": "",
"level": "debug"
},
"console": {
"enable": true,
"level": "fatal"
}
}
}
```
New command lines in lieu of supporting XL.
Minio initialize filesystem backend.
~~~
$ minio init fs <path>
~~~
Minio initialize XL backend.
~~~
$ minio init xl <url1>...<url16>
~~~
For 'fs' backend it starts the server.
~~~
$ minio server
~~~
For 'xl' backend it waits for servers to join.
~~~
$ minio server
... [PROGRESS BAR] of servers connecting
~~~
Now on other servers execute 'join' and they connect.
~~~
....
minio join <url1> -- from <url2> && minio server
minio join <url1> -- from <url3> && minio server
...
...
minio join <url1> -- from <url16> && minio server
~~~
2016-02-12 18:27:10 -05:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
// Validate - Check whether loggers are valid or not.
|
|
|
|
func (l *loggers) Validate() (err error) {
|
|
|
|
if l != nil {
|
|
|
|
fileLogger := l.GetFile()
|
|
|
|
if fileLogger.Enable && fileLogger.Filename == "" {
|
|
|
|
err = errors.New("Missing filename for enabled file logger")
|
|
|
|
}
|
2017-03-15 19:30:34 -04:00
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
|
|
|
|
return err
|
2017-03-15 19:30:34 -04:00
|
|
|
}
|
|
|
|
|
2017-02-09 18:20:54 -05:00
|
|
|
// SetFile set new file logger.
|
2017-03-23 19:36:00 -04:00
|
|
|
func (l *loggers) SetFile(flogger FileLogger) {
|
2017-02-09 18:20:54 -05:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.File = flogger
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFileLogger get current file logger.
|
2017-03-23 19:36:00 -04:00
|
|
|
func (l *loggers) GetFile() FileLogger {
|
2017-02-09 18:20:54 -05:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
return l.File
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConsole set new console logger.
|
2017-03-23 19:36:00 -04:00
|
|
|
func (l *loggers) SetConsole(clogger ConsoleLogger) {
|
2017-02-09 18:20:54 -05:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.Console = clogger
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
// GetConsole get current console logger.
|
|
|
|
func (l *loggers) GetConsole() ConsoleLogger {
|
2017-02-09 18:20:54 -05:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
return l.Console
|
|
|
|
}
|
|
|
|
|
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-03-23 19:36:00 -04:00
|
|
|
// NewLogger - returns new logger.
|
|
|
|
func NewLogger() *Logger {
|
|
|
|
logger := logrus.New()
|
|
|
|
logger.Out = ioutil.Discard
|
|
|
|
logger.Level = logrus.DebugLevel
|
|
|
|
|
|
|
|
log := &Logger{
|
|
|
|
logger: logger,
|
|
|
|
consoleTarget: NewConsoleLogger(),
|
2016-11-23 14:35:04 -05:00
|
|
|
}
|
2017-03-23 19:36:00 -04:00
|
|
|
|
|
|
|
logger.Hooks.Add(log)
|
|
|
|
|
|
|
|
return log
|
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) {
|
|
|
|
err = errorCause(err)
|
|
|
|
switch err.(type) {
|
|
|
|
case BucketNotFound, BucketNotEmpty, BucketExists:
|
|
|
|
ok = true
|
|
|
|
case ObjectNotFound, ObjectExistsAsDirectory, BucketPolicyNotFound, InvalidUploadID, BadDigest:
|
|
|
|
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
|
|
|
|
|
|
|
if terr, ok := err.(*Error); ok {
|
|
|
|
fields["stack"] = strings.Join(terr.Trace(), " ")
|
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
|
|
|
}
|