-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- introduce simple logger in migration internal package - unit tests - integrate into main function
- Loading branch information
1 parent
1511955
commit 83faad9
Showing
5 changed files
with
166 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package internal | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
// Logger logs migration events to disk and, if initialized with verbose, | ||
// stdout too. | ||
type Logger struct { | ||
closer io.WriteCloser | ||
logger *log.Logger | ||
} | ||
|
||
// NewLogger creates a new Logger. All log writes go to f, the logging file. | ||
// If the verbose flag is true all log writes also go to stdout. | ||
func NewLogger(wc io.WriteCloser, verbose bool) *Logger { | ||
// by default just write to file | ||
var w io.Writer | ||
w = wc | ||
if verbose { | ||
w = io.MultiWriter(wc, os.Stdout) | ||
} | ||
return &Logger{ | ||
closer: wc, | ||
logger: log.New(w, "", 0), | ||
} | ||
} | ||
|
||
// Error logs an error to the logging output. | ||
func (l *Logger) Error(err error) { | ||
if err == nil { | ||
return | ||
} | ||
l.logger.Printf("ERROR: %s", err.Error()) | ||
} | ||
|
||
// Print logs a string to the logging output. | ||
func (l *Logger) Print(msg string) { | ||
l.logger.Print(msg) | ||
} | ||
|
||
// Close closes the logfile backing the Logger. | ||
func (l *Logger) Close() error { | ||
return l.closer.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package internal_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
. "github.com/filecoin-project/go-filecoin/tools/migration/internal" | ||
) | ||
|
||
func TestLoggerWritesToFile(t *testing.T) { | ||
f, err := ioutil.TempFile("", "logfile") | ||
require.NoError(t, err) | ||
defer func() { | ||
require.NoError(t, os.Remove(f.Name())) | ||
}() | ||
|
||
logger := NewLogger(f, false) | ||
logger.Print("testing print 1") | ||
errTest := errors.New("testing error 2") | ||
logger.Error(errTest) | ||
|
||
// Reopen file so we can read new writes | ||
out, err := ioutil.ReadFile(f.Name()) | ||
require.NoError(t, err) | ||
outStr := string(out) | ||
assert.Contains(t, outStr, "testing print 1") | ||
expectedErrStr := fmt.Sprintf("ERROR: %s", errTest.Error()) | ||
assert.Contains(t, outStr, expectedErrStr) | ||
} | ||
|
||
func TestLoggerWritesToBothVerbose(t *testing.T) { | ||
// Point os.Stdout to a temp file | ||
fStdout, err := ioutil.TempFile("", "stdout") | ||
require.NoError(t, err) | ||
defer func() { | ||
require.NoError(t, os.Remove(fStdout.Name())) | ||
}() | ||
old := os.Stdout | ||
os.Stdout = fStdout | ||
defer func() { os.Stdout = old }() | ||
|
||
// Create log file | ||
fLogFile, err := ioutil.TempFile("", "logfile") | ||
require.NoError(t, err) | ||
defer func() { | ||
require.NoError(t, os.Remove(fLogFile.Name())) | ||
}() | ||
|
||
// Log verbosely | ||
logger := NewLogger(fLogFile, true) | ||
logger.Print("test line") | ||
errTest := errors.New("test err") | ||
logger.Error(errTest) | ||
expectedErrStr := fmt.Sprintf("ERROR: %s", errTest.Error()) | ||
|
||
// Check logfile | ||
outLogFile, err := ioutil.ReadFile(fLogFile.Name()) | ||
require.NoError(t, err) | ||
outLogFileStr := string(outLogFile) | ||
assert.Contains(t, outLogFileStr, "test line") | ||
assert.Contains(t, outLogFileStr, expectedErrStr) | ||
|
||
// Check stdout alias file | ||
outStdout, err := ioutil.ReadFile(fStdout.Name()) | ||
require.NoError(t, err) | ||
outStdoutStr := string(outStdout) | ||
assert.Contains(t, outStdoutStr, "test line") | ||
assert.Contains(t, outStdoutStr, expectedErrStr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters