func ExampleLogger() { var buf bytes.Buffer logger := log.New(&buf, "logger: ", log.Lshortfile) logger.Print("Hello, log file!") fmt.Print(&buf) // Output: // logger: example_test.go:17: Hello, log file! }
func TestWriteToWithLog(t *testing.T) { defer remove(t, "log.txt") os.Remove("log.txt") l := log.New(nil, "", 0) defer WriteToWithLog("log.txt", l).Close() want := "some log\n" l.Println("some log") fmt.Fprintln(os.Stdout, "This is from stdout") fmt.Fprintln(os.Stderr, "This is from stderr") got := readFile(t, "log.txt") if got != want { t.Errorf("\nwant: '%s'\n got: '%s'", want, got) } }
func TestRotateWithLog(t *testing.T) { os.Remove("log.txt") os.Remove("log.txt.old") l := log.New(nil, "", 0) stdout := os.Stdout stderr := os.Stderr defer remove(t, "log.txt", "log.txt.old") rl := WriteToWithLog("log.txt", l) defer rl.Close() l.Println("some log") err := os.Rename("log.txt", "log.txt.old") if err != nil { os.Stdout = stdout os.Stderr = stderr t.Fatalf("TestRotate(): %v\n", err) } l.Println("new filename") got := readFile(t, "log.txt.old") want := "some log\nnew filename\n" if got != want { t.Errorf("log file renamed failed\nwant: '%s'\n got: '%v'", want, got) } rl.rotate() want = "This is after rotation\n" l.Printf(want) os.Stdout = stdout os.Stderr = stderr got = readFile(t, "log.txt") if got != want { t.Errorf("rotate() failed\nwant: '%s'\n got: '%v'", want, got) } }