简体   繁体   English

在http.FileServer上登录404

[英]Log 404 on http.FileServer

I'm serving jpeg images from dirPath using http.FileServer with: 我正在使用http.FileServerdirPath提供jpeg图像:

http.Handle("/o/", http.StripPrefix(
    path.Join("/o", dirPath), http.FileServer(http.Dir(dirPath))))

What I don't understand is how to log when a request is made for a file that does not exist. 我不明白的是当请求一个不存在的文件时如何记录日志。 I can see http.FileServer returning 404 page not found when I make a request with the browser, but not on the server console. 使用浏览器发出请求时,我可以看到未找到 http.FileServer返回404页面 ,但未在服务器控制台上找到。

The http.Handler s returned by http.StripPrefix() and http.FileServer() do not log HTTP 404 errors. http.Handler S按返回http.StripPrefix()http.FileServer()不记录HTTP 404错误。 You have to extend their functionality in order to achieve what you want. 您必须扩展其功能才能实现所需的功能。

We can wrap the http.Handler value returned by http.StripPrefix() or http.FileServer() in another http.Handler or http.HandlerFunc . 我们可以将由http.StripPrefix()http.FileServer()返回的http.Handler值包装在另一个http.Handlerhttp.HandlerFunc Once you wrapped the handler, register the wrapper of course. 包装处理程序后,请当然注册包装程序。

The wrapper implementation will simply called the wrapped one, and once it returns, can inspect the HTTP response status code. 包装器实现将简单地称为包装器实现,一旦返回,就可以检查HTTP响应状态代码。 If it is an error (or specifically HTTP 404 Not Found), can log it appropriately. 如果这是一个错误(或者特别是未找到HTTP 404),则可以对其进行适当记录。

Problem is http.ResponseWriter does not support reading the response status code. 问题是http.ResponseWriter不支持读取响应状态代码。 What we can do is we also wrap the http.ResponseWriter and when status code is written, we will store it for later to be available. 我们可以做的是,我们还包装了http.ResponseWriter并且在编写状态代码时,我们将其存储起来以备后用。

Our http.ResponseWriter wrapper: 我们的http.ResponseWriter包装器:

type StatusRespWr struct {
    http.ResponseWriter // We embed http.ResponseWriter
    status int
}

func (w *StatusRespWr) WriteHeader(status int) {
    w.status = status // Store the status for our own use
    w.ResponseWriter.WriteHeader(status)
}

And wrapping the http.Handler : 并包装http.Handler

func wrapHandler(h http.Handler) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        srw := &StatusRespWr{ResponseWriter: w}
        h.ServeHTTP(srw, r)
        if srw.status >= 400 { // 400+ codes are the error codes
            log.Printf("Error status code: %d when serving path: %s",
                srw.status, r.RequestURI)
        }
    }
}

And the main function creating a file server, wrapping it and registering it: 主要功能是创建文件服务器,将其包装并注册:

http.HandleFunc("/o/", wrapHandler(
    http.StripPrefix("/o", http.FileServer(http.Dir("/test")))))
panic(http.ListenAndServe(":8181", nil))

Example output when requesting a non-existing file: 请求不存在的文件时的示例输出:

2015/12/01 11:47:40 Error status code: 404 when serving path: /o/sub/b.txt2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM