如何在Go中将消息打印到stderr?

I want to print some logs for debugging and testing, but previous logs are massive, So I should print my own logs to stderr:

go run main.go 1>/dev/null

So that I can just see my own logs.

What should I do?

Thank you

The log package by default prints to os.Stderr.

You can also use os.Stderr directly (it's an os.File).

There are multiple methods to send a message to stderr:

  1. Creating a new log.Logger:

    l := log.New(os.Stderr, "", 1)
    l.Println("log message")
    
  2. Using fmt.Fprintf:

    fmt.Fprintf(os.Stderr, "log message: %s", str)
    
  3. Directly writing to os.Stderr using os.Stderr.WriteString:

    os.Stderr.WriteString("log message")