I want to print some logs for debugging and testing, but existing logs are massive, so I want to print my own logs to stderr:
go run main.go 1>/dev/null
So that I can just see my own logs.
How can I do that?
There are multiple methods to send a message to stderr:
Creating a new log.Logger:
l := log.New(os.Stderr, "", 1)
l.Println("log message")
Using fmt.Fprintf:
fmt.Fprintf(os.Stderr, "log message: %s", str)
Directly writing to os.Stderr using os.Stderr.WriteString:
os.Stderr.WriteString("log message")
The log package by default prints to os.Stderr.
You can also use os.Stderr directly (it's an os.File).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With