英文:
gorilla handlers write to file
问题
import (
"github.com/gorilla/handlers"
)
func main(){
...
err := http.ListenAndServe(":9000", access_log(r))
if err != nil {
log.Fatal("HTTP server: ", err)
}
}
func access_log(r http.Handler) http.Handler {
f, err := os.OpenFile("log/access.log", os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0666)
if err != nil {
log.Panic("Access log: ", err)
}
return handlers.LoggingHandler(io.Writer(f), r)
}
文件已创建,但每次请求时没有写入文件。
#更新
当输出写入os.Stdout
时,这个方法可以工作。
func access_log(r http.Handler) http.Handler {
/*f, err := os.OpenFile("log/access.log", os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0666)
if err != nil {
log.Panic("Access log: ", err)
}
return handlers.LoggingHandler(io.Writer(f), r)*/
return handlers.LoggingHandler(os.Stdout, r)
}
#更新 II
func main(){
runtime.GOMAXPROCS(runtime.NumCPU())
r := mux.NewRouter()
www := r.Host("secure.domain.com").Subrouter()
www.HandleFunc("/", Handler_www)
api := r.Host("api.domain.com").Subrouter()
api.HandleFunc("/", Handler_api)
err := http.ListenAndServe(":9000", access_log(r))
if err != nil {
log.Fatal("HTTP server: ", err)
}
}
func access_log(r http.Handler) http.Handler {
f, err := os.OpenFile("log/access.log", os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0666)
if err != nil {
log.Panic("Access log: ", err)
}
return handlers.LoggingHandler(f, r)
}
英文:
import (
"github.com/gorilla/handlers"
)
func main(){
...
err := http.ListenAndServe(":9000", access_log(r))
if err != nil {
log.Fatal("HTTP server: ", err)
}
}
func access_log(r http.Handler) http.Handler {
f, err := os.OpenFile("log/access.log", os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0666)
if err != nil {
log.Panic("Access log: ", err)
}
return handlers.LoggingHandler(io.Writer(f), r)
}
The file is created but nothing is written to the file on each request
#update
This works when the output is written to os.Stdout
func access_log(r http.Handler) http.Handler {
/*f, err := os.OpenFile("log/access.log", os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0666)
if err != nil {
log.Panic("Access log: ", err)
}
return handlers.LoggingHandler(io.Writer(f), r)*/
return handlers.LoggingHandler(os.Stdout, r)
}
#update II
func main(){
runtime.GOMAXPROCS(runtime.NumCPU())
r := mux.NewRouter()
www := r.Host("secure.domain.com").Subrouter()
www.HandleFunc("/", Handler_www)
api := r.Host("api.domain.com").Subrouter()
api.HandleFunc("/", Handler_api)
err := http.ListenAndServe(":9000", access_log(r))
if err != nil {
log.Fatal("HTTP server: ", err)
}
}
func access_log(r http.Handler) http.Handler {
f, err := os.OpenFile("log/access.log", os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0666)
if err != nil {
log.Panic("Access log: ", err)
}
return handlers.LoggingHandler(f, r)
}
答案1
得分: 4
尝试从一个工作示例开始,如“制作和使用HTTP中间件”中所述:
package main
import (
"github.com/gorilla/handlers"
"net/http"
"os"
)
func main() {
finalHandler := http.HandlerFunc(final)
logFile, err := os.OpenFile("server.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
http.Handle("/", handlers.LoggingHandler(logFile, finalHandler))
http.ListenAndServe(":3000", nil)
}
func final(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}
handlers.LoggingHandler()
直接使用文件,而不是 io.Writer(f)
。
在你的情况下:
return handlers.LoggingHandler(f, r)
英文:
Try starting from a working example, as described in "Making and Using HTTP Middleware":
package main
import (
"github.com/gorilla/handlers"
"net/http"
"os"
)
func main() {
finalHandler := http.HandlerFunc(final)
logFile, err := os.OpenFile("server.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
http.Handle("/", handlers.LoggingHandler(logFile, finalHandler))
http.ListenAndServe(":3000", nil)
}
func final(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}
The handlers.LoggingHandler()
uses the file directly, not io.Writer(f)
.
In your case:
return handlers.LoggingHandler(f, r)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论