package main
import (
"bytes"
"fmt"
"log"
)
func main() {
// Log into byte
var buf bytes.Buffer
logInfo := log.New(&buf, "[Info] ", log.Lshortfile)
logInfo.Print("Hello, log file!")
logInfo.Printf("Hello, %s", "crazy")
fmt.Print(&buf)
logInfo.Fatalln("Ut oh")
fmt.Print(&buf)
}
Hello. I am trying to use log.Fatal or log.Fatalln instead of having log.New and os.Exit. However, it seems like the logger does not log "Ut oh" part (logInfo.Fatalln)
Following is expected output:
[Info] main.go:17: Hello, log file!
[Info] main.go:18: Hello, crazy
[Info] main.go:17: Hello, log file!
[Info] main.go:18: Hello, crazy
[Info] main.go:xx: Ut oh
Following is what I got:
[Info] main.go:17: Hello, log file!
[Info] main.go:18: Hello, crazy
it seems like it didn't do anything from logInfo.Fatalln("Ut oh")
Could you please tell me what I missed? Thank you
log.Fatalln
exits your app, so the second fmt.Print(&buf)
does not get executed.
Change your logger to write to os.Stderr
or something and you will see Ut oh
print.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.