Improve gemini log formatting
alex wennerberg alex@alexwennerberg.com
Sat, 02 Jan 2021 10:54:34 -0800
M
gemini.go
→
gemini.go
@@ -53,7 +53,7 @@ return buff.String()
} func gmiIndex(w *gmi.ResponseWriter, r *gmi.Request) { - log.Println("Index request") + logGemini(r) // TODO move into wrapper t, err := template.ParseFiles("templates/index.gmi") if err != nil { log.Fatal(err)@@ -79,6 +79,7 @@ t.Execute(w, data)
} func gmiPage(w *gmi.ResponseWriter, r *gmi.Request) { + logGemini(r) // TODO move into wrapper userName := filepath.Clean(strings.Split(r.URL.Host, ".")[0]) // clean probably unnecessary fileName := filepath.Clean(r.URL.Path) if fileName == "/" {@@ -87,7 +88,6 @@ } else if strings.HasPrefix(fileName, "/"+HiddenFolder) {
w.WriteStatus(gmi.StatusNotFound) return } - log.Println("Request for gemini file", fileName, "for user", userName) fullPath := path.Join(c.FilesDirectory, userName, fileName) if fileName == "/gemlog" { // temp hack _, err := os.Stat(path.Join(fullPath, "index.gmi"))
M
log.go
→
log.go
@@ -1,8 +1,11 @@
package main import ( + "fmt" + gmi "git.sr.ht/~adnano/go-gemini" "github.com/gorilla/handlers" "io" + "log" "net" "net/http" "net/url"@@ -144,3 +147,12 @@
// Parse logs and write to database // Anonymize user and IP? + +func logGemini(r *gmi.Request) { + line := fmt.Sprintf("gemini %s - [%s] %s %s\n", r.RemoteAddr.String(), + time.Now().Format("02/Jan/2006:15:04:05 -0700"), + r.URL.Host, + r.URL.Path) + buf := []byte(line) + log.Writer().Write(buf) +}