Refactor logger (#3924)

This patch fixes below

* Previously fatalIf() never writes log other than first logging target.
* quiet flag is not honored to show progress messages other than startup messages.
* Removes console package usage for progress messages.
This commit is contained in:
Bala FA
2017-03-24 05:06:00 +05:30
committed by Harshavardhana
parent 11e15f9b4c
commit d3cb79a57c
18 changed files with 450 additions and 426 deletions

View File

@@ -1,5 +1,5 @@
/*
* Minio Cloud Storage (C) 2015 Minio, Inc.
* Minio Cloud Storage (C) 2015, 2016, 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,48 +17,15 @@
package cmd
import (
"bytes"
"encoding/json"
"errors"
"testing"
"github.com/Sirupsen/logrus"
)
// Tests callerSource.
func TestCallerSource(t *testing.T) {
currentSource := func() string { return callerSource() }
// Tests getSource().
func TestGetSource(t *testing.T) {
currentSource := func() string { return getSource() }
gotSource := currentSource()
expectedSource := "[logger_test.go:31:TestCallerSource()]"
expectedSource := "[logger_test.go:26:TestGetSource()]"
if gotSource != expectedSource {
t.Errorf("expected : %s, got : %s", expectedSource, gotSource)
}
}
// Tests error logger.
func TestLogger(t *testing.T) {
var buffer bytes.Buffer
var fields logrus.Fields
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)
if err != nil {
t.Fatal(err)
}
if fields["level"] != "error" {
t.Fatalf("Expected error, got %s", fields["level"])
}
msg, ok := fields["cause"]
if !ok {
t.Fatal("Cause field missing")
}
if msg != "Fake error" {
t.Fatal("Cause field has unexpected message", msg)
}
}