使用Go语言简单的HTTP服务器将日志打印到日志中

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

Print to Log using Go Language Simple HTTP Server

问题

我正在尝试记录请求者的IP地址,他们使用的方法以及他们请求的文件。但由于某种原因,它只在终端输出,而没有保存到logfile.txt中...

package main

import (
  "fmt"
  "net/http"
  "log"
  "encoding/json"
  "io/ioutil"
)

type Options struct {
  Path string
  Port string
}

func Log(handler http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
    handler.ServeHTTP(w, r)
  })
}

func main() {

  op := &Options{Path: "./", Port: "8001"}

  data, _ := ioutil.ReadFile("./config.json")

  json.Unmarshal(data, op)

  http.Handle("/", http.FileServer(http.Dir(op.Path)))
  err := http.ListenAndServe(":" + op.Port, Log(http.DefaultServeMux))
  if err != nil {
    log.Fatal("ListenAndServe: ", err)
  }
}
英文:

I am trying to log the IP address of the requestor, what METHOD they are using and what file they are requesting. But for some reason it only outputs on the terminal and doesn't save it to logfile.txt...

package main

import (
  "fmt"
  "net/http"
  "log"
  "encoding/json"
  "io/ioutil"
)

type Options struct {
  Path string
  Port string
}

func Log(handler http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
    handler.ServeHTTP(w, r)
  })
}

func main() {

  op := &Options{Path: "./", Port: "8001"}

  data, _ := ioutil.ReadFile("./config.json")

  json.Unmarshal(data, op)

  http.Handle("/", http.FileServer(http.Dir(op.Path)))
  err := http.ListenAndServe(":" + op.Port, Log(http.DefaultServeMux))
  if err != nil {
    log.Fatal("ListenAndServe: ", err)
  }
}

答案1

得分: 7

在你的Log函数中,你使用的是fmt.Printf而不是fmt.Fprintf

例如,

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

var logFile *os.File

func Log(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(logFile, "%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
		handler.ServeHTTP(w, r)
	})
}

func main() {
	var err error
	logFile, err = os.Create("logfile.txt")
	if err != nil {
		log.Fatal("Log file create:", err)
		return
	}
	defer logFile.Close()
}
英文:

In your Log function, you are using fmt.Printf not fmt.Fprintf.

For example,

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

var logFile *os.File

func Log(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(logFile, "%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
		handler.ServeHTTP(w, r)
	})
}

func main() {
	var err error
	logFile, err = os.Create("logfile.txt")
	if err != nil {
		log.Fatal("Log file create:", err)
		return
	}
	defer logFile.Close()
}

huangapple
  • 本文由 发表于 2012年10月11日 08:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/12830504.html
匿名

发表评论

匿名网友

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

确定