英文:
Standard Library http package logs to stdout by default, can I disable this?
问题
在使用Go语言中的内置http模块时,我注意到http.ListenAndServe()
默认会将日志输出到stdout
。在我的终端中,主要显示的内容是:
http: TLS handshake error from x.x.x.x:x: tls: client offered an unsupported, maximum protocol version of 301.
我理解了这个错误是什么以及造成它的原因,但事实是,我并不特别关心。
我该如何阻止http包(或ListenAndServe()
)默认将日志输出到stdout?这让我的控制台变得混乱。我有95%的把握说这些日志不是我自己记录的,因为我设置的所有日志都有"INFO"、"WARNING"等前缀。
英文:
When using the built-in http module in Go, I notice that http.ListenAndServe()
will make logging calls to stdout
by default. The main thing showing up in my terminal is:
http: TLS handshake error from x.x.x.x:x: tls: client offered an unsupported, maximum protocol version of 301.
Now I understand what this error is and what causes it, but the fact of the matter is, I don't particularly care.
How can I stop the http package (or ListenAndServe()
, whatever) from logging to stdout by default? It's clogging up my pretty console. I'm 95% certain that I'm not the one logging the messages as all of the logs I have setup have "INFO", "WARNING" etc prefixes on them.
答案1
得分: 4
它不会记录到标准输出(stdout),而是记录到 HTTP 服务器的 ErrorLog
(如果已设置),否则记录到标准日志记录器(即 log 包中非方法函数使用的日志记录器)。如果你已经有一个日志记录器,可以使用该日志记录器构建一个带有该日志记录器作为 ErrorLog
的 http.Server
,然后调用 ListenAndServe
方法。否则,可以使用 log.SetOutput
来更改标准日志的输出目标。
英文:
It doesn't log to stdout, it logs to the http server's ErrorLog
if it's set, and to the standard logger (the one used by the non-method functions in log) otherwise. If you have a logger already, construct a http.Server
with that logger as its ErrorLog
and call ListenAndServe
on that. Otherwise use log.SetOutput
to change the destination of the standard log.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论