在Go中,有几种方法可以处理HTTP客户端或服务器中的日志记录。一种常见的方法是使用标准库中的日志包。下面是如何使用日志包在客户端中记录HTTP请求和响应的示例:
package main
import (
"log"
"net/http"
"net/http/httputil"
"time"
)
func main() {
client := &http.Client{
Timeout: 10 * time.Second,
}
req, err := http.NewRequest("GET", "https://example.com", nil)
if err != nil {
log.Fatal(err)
}
// 通过httputil.DumpRequestOut获取请求数据reqBytes, err := httputil.DumpRequestOut(req, true)
if err != nil {
log.Fatal(err)
}
log.Printf("Request: %s", reqBytes)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
// 通过httputil.DumpResponse获取响应数据respBytes, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Fatal(err)
}
log.Printf("Response: %s", respBytes)
defer resp.Body.Close()
}
此代码设置超时为10秒的HTTP客户端,创建要https://example.com的GET请求,并使用httputil记录传出请求。DumpRequestOut,使用客户端发送请求,使用httputil记录传入的响应。DumpResponse,最后关闭响应正文。
您可以自定义日志记录输出以满足您的需求,例如,通过使用不同的日志级别或格式,或者通过记录其他信息(如时间戳或请求/响应标头)来自定义。
标准库包日志为GO语言的日志管理提供了基本的基础设施,可用于记录我们的GO程序。日志记录的主要目的是跟踪程序中发生的情况、发生的位置以及发生的时间。日志可以提供代码跟踪、分析。日志记录(程序员的眼睛和耳朵)是一种查找这些错误并了解有关程序如何运行的更多信息的方法。
要使用软件包log,我们必须将其添加到导入列表中:
import (
"log"
)
在最简单的用法中,它格式化消息并将其发送到标准错误:
// Program in GO language to demonstrates how to use base log package.
package main
import (
"log"
)
func init(){
log.SetPrefix("LOG: ")
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)
log.Println("init started")
}
func main() {
// Println writes to the standard logger.
log.Println("main started")
// Fatalln is Println() followed by a call to os.Exit(1)
log.Fatalln("fatal message")
// Panicln is Println() followed by a call to panic()
log.Panicln("panic message")
}
执行此代码后,输出将如下所示:
输出
LOG: 2017/06/25 14:49:41.989813 C:/golang/example38.go:11: init started
LOG: 2017/06/25 14:49:41.990813 C:/golang/example38.go:15: main started
LOG: 2017/06/25 14:49:41.990813 C:/golang/example38.go:18: fatal message
exit status 1
将消息发送到标准错误对于简单的工具很有用。当我们构建服务器、应用程序或系统服务时,我们需要一个更好的位置来发送日志消息。在这里,所有错误消息都发送到标准错误,无论该消息是实际错误还是信息性消息。
标准日志条目包含以下内容:
- 前缀
- 日期时间
- 写入日志的源代码文件的完整路径
- 执行写入和最后消息的代码行。
这些信息是自动生成的,有关事件发生时间的信息以及有关事件发生地点的信息。
Println是编写日志消息的标准方法。
Fatalln或任何其他fatal调用,库会打印错误消息,然后调用 os。退出(1),强制程序退出。
Panicln用于写入日志消息,然后发出panic,除非恢复或将导致程序终止,否则可能会发出紧急消息。
用GO语言编程,带有真实世界的日志记录示例。
现在我正在举一个真实世界的例子,并在我的程序中实现上面的日志包。例如,我正在测试SMTP连接是否正常工作。对于测试用例,我将连接到不存在的SMTP服务器“smtp.smail.com”,因此程序将以日志消息终止。
// Program in GO language with real world example of logging.
package main
import (
"log"
"net/smtp"
)
func init(){
log.SetPrefix("TRACE: ")
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)
log.Println("init started")
}
func main() {
// Connect to the remote SMTP server.
client, err := smtp.Dial("smtp.smail.com:25")
if err != nil {
log.Fatalln(err)
}
client.Data()
}
输出
TRACE: 2017/06/25 14:54:42.662011 C:/golang/example39.go:9: init started
TRACE: 2017/06/25 14:55:03.685213 C:/golang/example39.go:15: dial tcp 23.27.98.252:25: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
exit status 1
上面的程序正在从日志中抛出致命异常。Fatalln(err)。