How can I print to Stderr in Go without using log
The Go builtin functions print
and println
print to stderr. So if you simply want to output some text to stderr you can do
package main
func main() {
println("Hello stderr!")
}
Documentation: https://golang.org/pkg/builtin/#print
How do I print message to stderr in Go?
The log
package by default prints to os.Stderr
.
You can also use os.Stderr
directly (it's an os.File
).
How to process stderr in go?
StderrPipe
returns a ReadCloser
. You can use that to create a bufio.Scanner
and then read lines one by one:
sc := bufio.NewScanner(stderr)
for sc.Scan() {
fmt.Printf("Line: %s\n", sc.Text());
}
Does fmt.Print() write to stdout in GoLang?
From the documentation:
Print formats using the default formats for its operands and writes to standard output.
So yep, it writes to stdout.
How I can print to stderr in C?
The syntax is almost the same as printf
. With printf
you give the string format and its contents ie:
printf("my %s has %d chars\n", "string format", 30);
With fprintf
it is the same, except now you are also specifying the place to print to:
FILE *myFile;
...
fprintf( myFile, "my %s has %d chars\n", "string format", 30);
Or in your case:
fprintf( stderr, "my %s has %d chars\n", "string format", 30);
Catch stdErr output in golang
As the comment by @tkausi suggests, you ought to set a custom logger for the go-sql-driver
package with https://godoc.org/github.com/go-sql-driver/mysql#SetLogger
Your custom logger can then do whatever makes sense for your application, including "swallowing" these logging statements and not displaying anything in case of no errors.
To answer the question's title, you can capture os.Stderr
with something like this:
func doPrint() {
fmt.Fprintf(os.Stderr, "output")
}
func main() {
old := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
doPrint()
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
os.Stderr = old
fmt.Println("Captured:", buf.String())
}
Bash command that prints a message on stderr
echo something >&2
is the correct way to do what you want.
However...
This will create a little program that will echo its arguments to stderr
:
gcc -o echoerr -x c - <<'EOF'
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
}
fprintf(stderr, "\n");
exit(0);
}
EOF
You can use it like this:
$ ./echoerr this is an error message
this is an error message
$ ./echoerr this is an error message 2>error.out
$ cat error.out
this is an error message
Related Topics
Linux X86-64 Assembly and Printf
How to Detect a Buffer Over Run on Serial Port in Linux Using C++
Installing G++ on Windows Subsystem for Linux
Newbie on Debian and Trying to Make Java 7 the Default Java Version Used
List Files Recursively in Linux Cli With Path Relative to the Current Directory
A Way to Determine a Process'S "Real" Memory Usage, I.E. Private Dirty Rss
Linux Join Utility Complains About Input File Not Being Sorted
Fast Way to Find Difference Between Two Strings of Equal Length in Perl
How to Make Perl Wait for Child Processes Started in the Background with System()
How to Sort with Multiple Lines in Bash
Creating a Self-Extracting Zip Archive on a Linux Box
How to Detect Usb Device Disconnect Under Linux/Qt/C++
Linux' Hrtimer - Microsecond Precision
What Does the Gcc Error Message, "Error: Unsupported for 'Mov'", Mean
How to Check If a Files Exists in a Specific Directory in a Bash Script
Curl Progress Bar: How to Pipe and Extract Numbers Only Using Grep