大猩猩处理程序将内容写入文件。

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

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)

huangapple
  • 本文由 发表于 2015年4月13日 18:48:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/29603488.html
匿名

发表评论

匿名网友

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

确定