log_test.go 974 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package gou

import (
	"io/ioutil"
	"os"
	"strings"
	"testing"
	"time"
)

func TestSetupLogToFile(t *testing.T) {
	tmpf, err := ioutil.TempFile("", "goutest")
	if err != nil {
		t.Fatalf("error creating log file: %v\n", err)
	}
	defer os.Remove(tmpf.Name())

18
	SetupLoggingFile(tmpf, "debug")
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	logStr := "hihi"
	Infof(logStr)

	// Flush file buffer to disk
	err = tmpf.Sync()
	if err != nil {
		t.Errorf("error syncing tmpf: %v", err)
	}
	time.Sleep(1 * time.Second)

	// Read tmp file and confirm log message was written
	bytes, err := ioutil.ReadFile(tmpf.Name())
	if err != nil {
		t.Errorf("error reading temp file[%s]: %v\n", tmpf.Name(), err)
	}

	logFileBytes := string(bytes)
	if !strings.Contains(logFileBytes, logStr) {
		t.Logf("logfile:\n%s", logFileBytes)
		t.Errorf("%s not found in logfile %s\n", logStr, tmpf.Name())
	}
}

func TestLogrusLogger(t *testing.T) {
	SetupLogrus("debug")

	Debug("Debug")
	Infof("Info")
	Warn("Warn")
	Error("Error")

	rus = nil
}