Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang apache logging
type ApacheLogRecord struct {
http.ResponseWriter
ip string
time time.Time
method, uri, protocol string
status int
responseBytes int64
elapsedTime time.Duration
}
func (r *ApacheLogRecord) Log(out io.Writer) {
timeFormatted := r.time.Format("02/Jan/2006 03:04:05")
requestLine := fmt.Sprintf("%s %s %s", r.method, r.uri, r.protocol)
fmt.Fprintf(out, ApacheFormatPattern, r.ip, timeFormatted, requestLine, r.status, r.responseBytes,
r.elapsedTime.Seconds())
}
func (r *ApacheLogRecord) Write(p []byte) (int, error) {
written, err := r.ResponseWriter.Write(p)
r.responseBytes += int64(written)
return written, err
}
func (r *ApacheLogRecord) WriteHeader(status int) {
r.status = status
r.ResponseWriter.WriteHeader(status)
}
type ApacheLoggingHandler struct {
handler http.Handler
out io.Writer
}
func NewApacheLoggingHandler(handler http.Handler, out io.Writer) http.Handler {
return &ApacheLoggingHandler{
handler: handler,
out: out,
}
}
func (h *ApacheLoggingHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
clientIP := r.RemoteAddr
if colon := strings.LastIndex(clientIP, ":"); colon != -1 {
clientIP = clientIP[:colon]
}
record := &ApacheLogRecord{
ResponseWriter: rw,
ip: clientIP,
time: time.Time{},
method: r.Method,
uri: r.RequestURI,
protocol: r.Proto,
status: http.StatusOK,
elapsedTime: time.Duration(0),
}
startTime := time.Now()
h.handler.ServeHTTP(record, r)
finishTime := time.Now()
record.time = finishTime.UTC()
record.elapsedTime = finishTime.Sub(startTime)
record.Log(h.out)
}
func main() {
mux := http.DefaultServeMux
mux.HandleFunc("/", indexHandler)
loggingHandler := NewApacheLoggingHandler(mux, os.Stderr)
server := &http.Server{
Addr: fmt.Sprintf(":%s", 2345),
Handler: loggingHandler,
}
server.ListenAndServe()
}
@philangist

This comment has been minimized.

Copy link

@philangist philangist commented Sep 8, 2013

Great code!

@jadekler

This comment has been minimized.

Copy link

@jadekler jadekler commented Apr 26, 2014

ApacheFormatPattern is not defined in this code. Could you clarify?

edit: I found it in another of your repos: const apacheFormatPattern = "%s - - [%s] "%s %s %s" %d %d %.4f\n"

edit edit: If anyone is interested in seeing this in the gorilla mux framework, i've used the above idea over at https://github.com/jadekler/git-go-websiteskeleton

@cevaris

This comment has been minimized.

Copy link

@cevaris cevaris commented Mar 8, 2015

This seems to be the proper ApacheFormatPattern for this code

const (
    ApacheFormatPattern = "%s - - [%s] \"%s %d %d\" %f\n"
)
@bblack-cuda

This comment has been minimized.

Copy link

@bblack-cuda bblack-cuda commented Mar 29, 2016

Hey. This implementation's AWESOME! What's the license for this code snippet in case I want to derive and use it in my code? Some form of CC? MIT? BSD? WTFPL?

@maugre

This comment has been minimized.

Copy link

@maugre maugre commented May 30, 2016

Also curious about the license. I've copied a derivative[1], modified slightly to suit my needs and added a Negroni middleware to the package. I want to share that package, but depends on the license.

  1. https://gist.github.com/Tantas/1fc00c5eb7c291e2a34b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment