You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.2 KiB
89 lines
2.2 KiB
9 years ago
|
/*
|
||
9 years ago
|
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
|
||
9 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
9 years ago
|
"bufio"
|
||
|
"bytes"
|
||
9 years ago
|
"runtime/debug"
|
||
9 years ago
|
"strings"
|
||
9 years ago
|
|
||
|
"github.com/Sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
type fields map[string]interface{}
|
||
|
|
||
|
var log = logrus.New() // Default console logger.
|
||
|
|
||
9 years ago
|
// logger carries logging configuration for various supported loggers.
|
||
|
// Currently supported loggers are
|
||
|
//
|
||
|
// - console [default]
|
||
|
// - file
|
||
|
// - syslog
|
||
|
type logger struct {
|
||
8 years ago
|
Console consoleLogger `json:"console"`
|
||
|
File fileLogger `json:"file"`
|
||
|
Syslog syslogLogger `json:"syslog"`
|
||
9 years ago
|
// Add new loggers here.
|
||
|
}
|
||
|
|
||
9 years ago
|
// stackInfo returns printable stack trace.
|
||
|
func stackInfo() string {
|
||
|
// Convert stack-trace bytes to io.Reader.
|
||
|
rawStack := bufio.NewReader(bytes.NewBuffer(debug.Stack()))
|
||
|
// Skip stack trace lines until our real caller.
|
||
|
for i := 0; i <= 4; i++ {
|
||
|
rawStack.ReadLine()
|
||
|
}
|
||
|
|
||
|
// Read the rest of useful stack trace.
|
||
|
stackBuf := new(bytes.Buffer)
|
||
|
stackBuf.ReadFrom(rawStack)
|
||
|
|
||
|
// Strip GOPATH of the build system and return.
|
||
8 years ago
|
return strings.Replace(stackBuf.String(), GOPATH+"/src/", "", -1)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// errorIf synonymous with fatalIf but doesn't exit on error != nil
|
||
9 years ago
|
func errorIf(err error, msg string, data ...interface{}) {
|
||
9 years ago
|
if err == nil {
|
||
|
return
|
||
|
}
|
||
9 years ago
|
fields := logrus.Fields{
|
||
8 years ago
|
"cause": err.Error(),
|
||
9 years ago
|
}
|
||
9 years ago
|
if globalTrace {
|
||
9 years ago
|
fields["stack"] = "\n" + stackInfo()
|
||
9 years ago
|
}
|
||
9 years ago
|
log.WithFields(fields).Errorf(msg, data...)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// fatalIf wrapper function which takes error and prints jsonic error messages.
|
||
9 years ago
|
func fatalIf(err error, msg string, data ...interface{}) {
|
||
9 years ago
|
if err == nil {
|
||
|
return
|
||
|
}
|
||
9 years ago
|
fields := logrus.Fields{
|
||
8 years ago
|
"cause": err.Error(),
|
||
9 years ago
|
}
|
||
9 years ago
|
if globalTrace {
|
||
9 years ago
|
fields["stack"] = "\n" + stackInfo()
|
||
9 years ago
|
}
|
||
9 years ago
|
log.WithFields(fields).Fatalf(msg, data...)
|
||
9 years ago
|
}
|