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
import (
"io/ioutil"
"github.com/Sirupsen/logrus"
)
import "github.com/Sirupsen/logrus"
// consoleLogger - default logger if not other logging is enabled.
type consoleLogger struct {
@ -32,15 +28,19 @@ type consoleLogger struct {
func enableConsoleLogger() {
clogger := serverConfig.GetConsoleLogger()
if !clogger.Enable {
// Disable console logger if asked for.
log.Out = ioutil.Discard
return
}
consoleLogger := logrus.New()
// log.Out and log.Formatter use the default versions.
// Only set specific log level.
lvl, err := logrus.ParseLevel(clogger.Level)
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 (
"fmt"
"io/ioutil"
"os"
"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)
fatalIf(err, "Unable to open log file.")
fileLogger := logrus.New()
// Add a local file hook.
log.Hooks.Add(&localFile{file})
fileLogger.Hooks.Add(&localFile{file})
lvl, err := logrus.ParseLevel(flogger.Level)
fatalIf(err, "Unknown log level found in the config file.")
// Set default JSON formatter.
log.Formatter = new(logrus.JSONFormatter)
log.Level = lvl // Minimum log level.
fileLogger.Out = ioutil.Discard
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.

@ -20,6 +20,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"log/syslog"
"github.com/Sirupsen/logrus"
@ -43,9 +44,16 @@ func enableSyslogLogger(raddr string) {
syslogHook, err := newSyslog("udp", raddr, syslog.LOG_ERR, "MINIO")
fatalIf(err, "Unable to initialize syslog logger.")
log.Hooks.Add(syslogHook) // Add syslog hook.
log.Formatter = &logrus.JSONFormatter{} // JSON formatted log.
log.Level = logrus.ErrorLevel // Minimum log level.
sysLogger := logrus.New()
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.

@ -21,13 +21,17 @@ import (
"path"
"runtime"
"strings"
"sync"
"github.com/Sirupsen/logrus"
)
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.
// Currently supported loggers are
@ -69,8 +73,10 @@ func errorIf(err error, msg string, data ...interface{}) {
fields["stack"] = strings.Join(e.Trace(), " ")
}
for _, log := range log.loggers {
log.WithFields(fields).Errorf(msg, data...)
}
}
// fatalIf wrapper function which takes error and prints jsonic error messages.
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 {
fields["stack"] = strings.Join(e.Trace(), " ")
}
for _, log := range log.loggers {
log.WithFields(fields).Fatalf(msg, data...)
}
}

@ -39,8 +39,12 @@ func TestCallerLocation(t *testing.T) {
func TestLogger(t *testing.T) {
var buffer bytes.Buffer
var fields logrus.Fields
log.Out = &buffer
log.Formatter = new(logrus.JSONFormatter)
testLog := logrus.New()
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.")
err := json.Unmarshal(buffer.Bytes(), &fields)

Loading…
Cancel
Save