How can I print to Stderr in Go without using log

huangapple go评论101阅读模式
英文:

How can I print to Stderr in Go without using log

问题

如何在不使用 log 的情况下向 Stderr 写入消息?

这个 Stack Overflow 帖子中的评论 展示了如何使用 loglog.Println("Message"),但如果我不想要时间戳怎么办?

下面的代码是正确的 Go 代码吗?

os.Stderr.WriteString("Message")

英文:

How can I write a message to Stderr without using log?

A comment in this SO post shows how to do it with log: log.Println("Message"), but what if I don't want a timestamp?

Is the following good Go?

os.Stderr.WriteString("Message")

答案1

得分: 140

如果你不想要时间戳,只需创建一个log.Logger,并将flag设置为0

l := log.New(os.Stderr, "", 0)
l.Println("log msg")

编辑:

>以下的Go代码是否正确?

os.Stderr.WriteString("Message")

这是可以接受的,你也可以使用fmt.Fprintf和相关函数来获取格式化的输出:

fmt.Fprintf(os.Stderr, "number of foo: %d", nFoo)
英文:

If you don't want timestamps, just create a new log.Logger with flag set to 0:

l := log.New(os.Stderr, "", 0)
l.Println("log msg")

EDIT:

>Is the following good Go?
>
os.Stderr.WriteString("Message")

This is acceptable, and you can also use fmt.Fprintf and friends to get formatted output:

fmt.Fprintf(os.Stderr, "number of foo: %d", nFoo)

答案2

得分: 130

使用fmt包,你可以选择以以下方式写入stderr

import "fmt"
import "os"

func main() {
    fmt.Fprintln(os.Stderr, "hello world")
}
英文:

Using the fmt package, you can choose to write to stderr this way:

import "fmt"
import "os"

func main() {
	fmt.Fprintln(os.Stderr, "hello world")
}

答案3

得分: 33

Go语言的内置函数printprintln会将内容打印到标准错误输出(stderr)。因此,如果你只是想将一些文本输出到stderr,可以这样做:

package main

func main() {
    println("Hello stderr!")
}

文档链接:https://golang.org/pkg/builtin/#print

英文:

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

答案4

得分: 26

os.Stderr是一个io.Writer,因此你可以在任何接受io.Writer的函数中使用它。以下是一些示例:

str := "Message"
fmt.Fprintln(os.Stderr, str)
io.WriteString(os.Stderr, str)
io.Copy(os.Stderr, bytes.NewBufferString(str))
os.Stderr.Write([]byte(str))

具体取决于你要打印的字符串的形式(例如,如果你想要先格式化它,如果你将其作为io.Reader,如果你将其作为字节切片...)。还有很多其他的方式。

英文:

os.Stderr is an io.Writer, so you can use it in any function which accepts an io.Writer. Here are a few examples:

str := "Message"
fmt.Fprintln(os.Stderr, str)
io.WriteString(os.Stderr, str)
io.Copy(os.Stderr, bytes.NewBufferString(str))
os.Stderr.Write([]byte(str))

It all depends on how exactly you have the string you want to print (i.e. if you want to format it first, if you have it as an io.Reader, if you have it as a byte slice...). And there can be a lot more ways.

答案5

得分: 7

默认情况下,日志记录器的标志设置为Ldate | Ltime。您可以将日志记录格式更改为以下任何一种(来自golang log文档):

Ldate         = 1 << iota     // 本地时区的日期:2009/01/23
Ltime                         // 本地时区的时间:01:23:23
Lmicroseconds                 // 微秒分辨率:01:23:23.123123。假设Ltime已设置。
Llongfile                     // 完整的文件名和行号:/a/b/c/d.go:23
Lshortfile                    // 最终文件名元素和行号:d.go:23。覆盖Llongfile
LUTC                          // 如果设置了Ldate或Ltime,则使用UTC而不是本地时区
LstdFlags     = Ldate | Ltime // 标准记录器的初始值

例如,标志Ldate | Ltime(或LstdFlags)会产生以下结果:

2009/01/23 01:23:23 message

而标志Ldate | Ltime | Lmicroseconds | Llongfile会产生以下结果:

2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message

您还可以通过将标志设置为0来将默认记录器设置为不打印任何内容:

log.SetFlags(0)
英文:

By default the logger flags are set to Ldate | Ltime. You can change the logger format to any of the following (from the golang log documentation):

Ldate         = 1 &lt;&lt; iota     // the date in the local time zone: 2009/01/23
Ltime                         // the time in the local time zone: 01:23:23
Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
Llongfile                     // full file name and line number: /a/b/c/d.go:23
Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
LstdFlags     = Ldate | Ltime // initial values for the standard logger

For example, flags Ldate | Ltime (or LstdFlags) produce,

2009/01/23 01:23:23 message

While flags Ldate | Ltime | Lmicroseconds | Llongfile produce,

2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message

You can also set the default logger to not print anything by setting the flag to 0:

log.SetFlags(0)

答案6

得分: -1

使用SetOutput函数,将输出流设置为os.Stdout

import (
	"log"
	"os"
)

func init() {
	log.SetOutput(os.Stdout)
}

func main() {
	log.Println("Gene Story SNP File Storage Server Started.")
}
英文:

use SetOutput function, set output stream to os.Stdout

import (
	&quot;log&quot;
	&quot;os&quot;
)

func init() {
	log.SetOutput(os.Stdout)
}

func main() {
	log.Println(&quot;Gene Story SNP File Storage Server Started.&quot;)
}

huangapple
  • 本文由 发表于 2015年4月19日 02:52:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/29721449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定