logger: Disassociate shared log config between console, file and syslog (#3333)

logurs is not helping us to set different log formats (json/text) to
different loggers. Now, we create different logurs instances and call
them in errorIf and fatalIf
master
Anis Elleuch 8 years ago committed by Harshavardhana
parent 01ae5bb39c
commit 22c98d3fa2
  1. 16
      cmd/logger-console-hook.go
  2. 14
      cmd/logger-file-hook.go
  3. 14
      cmd/logger-syslog-hook_nix.go
  4. 10
      cmd/logger.go
  5. 8
      cmd/logger_test.go

@ -16,11 +16,7 @@
package cmd package cmd
import ( import "github.com/Sirupsen/logrus"
"io/ioutil"
"github.com/Sirupsen/logrus"
)
// consoleLogger - default logger if not other logging is enabled. // consoleLogger - default logger if not other logging is enabled.
type consoleLogger struct { type consoleLogger struct {
@ -32,15 +28,19 @@ type consoleLogger struct {
func enableConsoleLogger() { func enableConsoleLogger() {
clogger := serverConfig.GetConsoleLogger() clogger := serverConfig.GetConsoleLogger()
if !clogger.Enable { if !clogger.Enable {
// Disable console logger if asked for.
log.Out = ioutil.Discard
return return
} }
consoleLogger := logrus.New()
// log.Out and log.Formatter use the default versions. // log.Out and log.Formatter use the default versions.
// Only set specific log level. // Only set specific log level.
lvl, err := logrus.ParseLevel(clogger.Level) lvl, err := logrus.ParseLevel(clogger.Level)
fatalIf(err, "Unknown log level found in the config file.") fatalIf(err, "Unknown log level found in the config file.")
log.Level = lvl consoleLogger.Level = lvl
consoleLogger.Formatter = new(logrus.TextFormatter)
log.mu.Lock()
log.loggers = append(log.loggers, consoleLogger)
log.mu.Unlock()
} }

@ -18,6 +18,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
@ -43,15 +44,22 @@ func enableFileLogger() {
file, err := os.OpenFile(flogger.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) file, err := os.OpenFile(flogger.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
fatalIf(err, "Unable to open log file.") fatalIf(err, "Unable to open log file.")
fileLogger := logrus.New()
// Add a local file hook. // Add a local file hook.
log.Hooks.Add(&localFile{file}) fileLogger.Hooks.Add(&localFile{file})
lvl, err := logrus.ParseLevel(flogger.Level) lvl, err := logrus.ParseLevel(flogger.Level)
fatalIf(err, "Unknown log level found in the config file.") fatalIf(err, "Unknown log level found in the config file.")
// Set default JSON formatter. // Set default JSON formatter.
log.Formatter = new(logrus.JSONFormatter) fileLogger.Out = ioutil.Discard
log.Level = lvl // Minimum log level. fileLogger.Formatter = new(logrus.JSONFormatter)
fileLogger.Level = lvl // Minimum log level.
log.mu.Lock()
log.loggers = append(log.loggers, fileLogger)
log.mu.Unlock()
} }
// Fire fires the file logger hook and logs to the file. // Fire fires the file logger hook and logs to the file.

@ -20,6 +20,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io/ioutil"
"log/syslog" "log/syslog"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
@ -43,9 +44,16 @@ func enableSyslogLogger(raddr string) {
syslogHook, err := newSyslog("udp", raddr, syslog.LOG_ERR, "MINIO") syslogHook, err := newSyslog("udp", raddr, syslog.LOG_ERR, "MINIO")
fatalIf(err, "Unable to initialize syslog logger.") fatalIf(err, "Unable to initialize syslog logger.")
log.Hooks.Add(syslogHook) // Add syslog hook. sysLogger := logrus.New()
log.Formatter = &logrus.JSONFormatter{} // JSON formatted log.
log.Level = logrus.ErrorLevel // Minimum log level. sysLogger.Hooks.Add(syslogHook) // Add syslog hook.
sysLogger.Formatter = &logrus.JSONFormatter{} // JSON formatted log.
sysLogger.Level = logrus.ErrorLevel // Minimum log level.
sysLogger.Out = ioutil.Discard
log.mu.Lock()
log.loggers = append(log.loggers, sysLogger)
log.mu.Unlock()
} }
// newSyslog - Creates a hook to be added to an instance of logger. // newSyslog - Creates a hook to be added to an instance of logger.

@ -21,13 +21,17 @@ import (
"path" "path"
"runtime" "runtime"
"strings" "strings"
"sync"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
) )
type fields map[string]interface{} type fields map[string]interface{}
var log = logrus.New() // Default console logger. var log = struct {
loggers []*logrus.Logger // All registered loggers.
mu sync.Mutex
}{}
// logger carries logging configuration for various supported loggers. // logger carries logging configuration for various supported loggers.
// Currently supported loggers are // Currently supported loggers are
@ -69,8 +73,10 @@ func errorIf(err error, msg string, data ...interface{}) {
fields["stack"] = strings.Join(e.Trace(), " ") fields["stack"] = strings.Join(e.Trace(), " ")
} }
for _, log := range log.loggers {
log.WithFields(fields).Errorf(msg, data...) log.WithFields(fields).Errorf(msg, data...)
} }
}
// fatalIf wrapper function which takes error and prints jsonic error messages. // fatalIf wrapper function which takes error and prints jsonic error messages.
func fatalIf(err error, msg string, data ...interface{}) { func fatalIf(err error, msg string, data ...interface{}) {
@ -85,5 +91,7 @@ func fatalIf(err error, msg string, data ...interface{}) {
if e, ok := err.(*Error); ok { if e, ok := err.(*Error); ok {
fields["stack"] = strings.Join(e.Trace(), " ") fields["stack"] = strings.Join(e.Trace(), " ")
} }
for _, log := range log.loggers {
log.WithFields(fields).Fatalf(msg, data...) log.WithFields(fields).Fatalf(msg, data...)
} }
}

@ -39,8 +39,12 @@ func TestCallerLocation(t *testing.T) {
func TestLogger(t *testing.T) { func TestLogger(t *testing.T) {
var buffer bytes.Buffer var buffer bytes.Buffer
var fields logrus.Fields var fields logrus.Fields
log.Out = &buffer testLog := logrus.New()
log.Formatter = new(logrus.JSONFormatter) testLog.Out = &buffer
testLog.Formatter = new(logrus.JSONFormatter)
log.mu.Lock()
log.loggers = append(log.loggers, testLog)
log.mu.Unlock()
errorIf(errors.New("Fake error"), "Failed with error.") errorIf(errors.New("Fake error"), "Failed with error.")
err := json.Unmarshal(buffer.Bytes(), &fields) err := json.Unmarshal(buffer.Bytes(), &fields)

Loading…
Cancel
Save