Printing Message to Standard Error in Go
If you need to print debugging or testing logs separately from existing logs, you may consider sending messages to the standard error stream (stderr). This is useful when you want to isolate your logs for easier analysis.
Methods for Printing to stderr
There are multiple ways to print messages to stderr in Go:
1. Using log.Logger:
Create a new log.Logger, specifying os.Stderr as the output stream:
l := log.New(os.Stderr, "", 1)
l.Println("log message")
2. Using fmt.Fprintf:
Use fmt.Fprintf to write formatted messages to stderr:
fmt.Fprintf(os.Stderr, "log message: %s", str)
3. Writing Directly to os.Stderr:
Write directly to os.Stderr using os.Stderr.WriteString:
os.Stderr.WriteString("log message")
By directing your logs to stderr, you can easily separate them from other logs and focus on debugging and testing information.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3