2015-09-18 08:49:06 +02:00
|
|
|
/*
|
2017-03-24 00:36:00 +01:00
|
|
|
* Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc.
|
2015-09-18 08:49:06 +02: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-19 01:23:42 +02:00
|
|
|
package cmd
|
2015-09-18 08:49:06 +02:00
|
|
|
|
|
|
|
import (
|
2016-10-11 09:50:27 +02:00
|
|
|
"fmt"
|
2017-03-24 00:36:00 +01:00
|
|
|
"io/ioutil"
|
2016-10-12 10:03:50 +02:00
|
|
|
"path"
|
2016-10-11 09:50:27 +02:00
|
|
|
"runtime"
|
2016-05-25 11:32:35 +02:00
|
|
|
"strings"
|
2016-11-23 20:35:04 +01:00
|
|
|
"sync"
|
2015-09-18 08:49:06 +02:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-03-24 00:36:00 +01:00
|
|
|
"github.com/minio/mc/pkg/console"
|
2017-11-25 20:58:29 +01:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2015-09-18 08:49:06 +02:00
|
|
|
)
|
|
|
|
|
2017-03-24 00:36:00 +01:00
|
|
|
var log = NewLogger()
|
2015-09-18 08:49:06 +02:00
|
|
|
|
2017-03-24 00:36:00 +01:00
|
|
|
type loggers struct {
|
2017-02-10 00:20:54 +01:00
|
|
|
sync.RWMutex
|
2017-03-24 00:36:00 +01: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-13 00:27:10 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:36:00 +01: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 == "" {
|
2017-11-25 20:58:29 +01:00
|
|
|
err = fmt.Errorf("Missing filename for enabled file logger")
|
2017-03-24 00:36:00 +01:00
|
|
|
}
|
2017-03-16 00:30:34 +01:00
|
|
|
}
|
2017-03-24 00:36:00 +01:00
|
|
|
|
|
|
|
return err
|
2017-03-16 00:30:34 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 00:20:54 +01:00
|
|
|
// SetFile set new file logger.
|
2017-03-24 00:36:00 +01:00
|
|
|
func (l *loggers) SetFile(flogger FileLogger) {
|
2017-02-10 00:20:54 +01:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.File = flogger
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFileLogger get current file logger.
|
2017-03-24 00:36:00 +01:00
|
|
|
func (l *loggers) GetFile() FileLogger {
|
2017-02-10 00:20:54 +01:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
return l.File
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConsole set new console logger.
|
2017-03-24 00:36:00 +01:00
|
|
|
func (l *loggers) SetConsole(clogger ConsoleLogger) {
|
2017-02-10 00:20:54 +01:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.Console = clogger
|
|
|
|
}
|
|
|
|
|
2017-03-24 00:36:00 +01:00
|
|
|
// GetConsole get current console logger.
|
|
|
|
func (l *loggers) GetConsole() ConsoleLogger {
|
2017-02-10 00:20:54 +01:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
return l.Console
|
|
|
|
}
|
|
|
|
|
2017-03-24 00:36:00 +01: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 11:24:34 +02:00
|
|
|
}
|
2017-03-24 00:36:00 +01: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 11:24:34 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:36:00 +01: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 08:49:06 +02:00
|
|
|
}
|
2017-03-24 00:36:00 +01: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 08:49:06 +02:00
|
|
|
}
|
2017-03-24 00:36:00 +01: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 08:49:06 +02:00
|
|
|
}
|
2017-03-24 00:36:00 +01:00
|
|
|
}
|
2016-08-25 18:39:01 +02:00
|
|
|
|
2017-04-01 10:06:06 +02:00
|
|
|
// NewLogger - returns a new initialized logger.
|
2017-03-24 00:36:00 +01:00
|
|
|
func NewLogger() *Logger {
|
|
|
|
logger := logrus.New()
|
|
|
|
logger.Out = ioutil.Discard
|
|
|
|
logger.Level = logrus.DebugLevel
|
|
|
|
|
2017-04-01 10:06:06 +02:00
|
|
|
l := &Logger{
|
2017-03-24 00:36:00 +01:00
|
|
|
logger: logger,
|
|
|
|
consoleTarget: NewConsoleLogger(),
|
2016-11-23 20:35:04 +01:00
|
|
|
}
|
2017-03-24 00:36:00 +01:00
|
|
|
|
2017-04-01 10:06:06 +02:00
|
|
|
// Adds a console logger.
|
|
|
|
logger.Hooks.Add(l)
|
2017-03-24 00:36:00 +01:00
|
|
|
|
2017-04-01 10:06:06 +02:00
|
|
|
return l
|
2015-09-19 11:36:50 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:36:00 +01: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 20:58:29 +01:00
|
|
|
err = errors.Cause(err)
|
2017-03-24 00:36:00 +01:00
|
|
|
switch err.(type) {
|
|
|
|
case BucketNotFound, BucketNotEmpty, BucketExists:
|
|
|
|
ok = true
|
2017-10-22 07:30:34 +02:00
|
|
|
case ObjectNotFound, ObjectExistsAsDirectory:
|
|
|
|
ok = true
|
|
|
|
case BucketPolicyNotFound, InvalidUploadID:
|
2017-03-24 00:36:00 +01:00
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil || isErrIgnored(err) {
|
2015-09-19 11:36:50 +02:00
|
|
|
return
|
|
|
|
}
|
2017-03-24 00:36:00 +01:00
|
|
|
|
2016-05-16 23:31:28 +02:00
|
|
|
fields := logrus.Fields{
|
2016-11-24 01:36:26 +01:00
|
|
|
"source": source,
|
|
|
|
"cause": err.Error(),
|
2015-09-19 11:36:50 +02:00
|
|
|
}
|
2017-03-24 00:36:00 +01:00
|
|
|
|
2017-11-25 20:58:29 +01:00
|
|
|
if terr, ok := err.(*errors.Error); ok {
|
|
|
|
fields["stack"] = strings.Join(terr.Stack(), " ")
|
2015-09-19 11:36:50 +02:00
|
|
|
}
|
2017-02-21 10:32:05 +01:00
|
|
|
|
2017-03-24 00:36:00 +01: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 20:35:04 +01:00
|
|
|
}
|
2015-09-18 08:49:06 +02:00
|
|
|
}
|
2016-11-24 01:36:26 +01:00
|
|
|
|
2017-03-24 00:36:00 +01: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-24 01:36:26 +01:00
|
|
|
}
|