about summary refs log tree commit diff stats
path: root/logging.go
diff options
context:
space:
mode:
Diffstat (limited to 'logging.go')
-rw-r--r--logging.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/logging.go b/logging.go
index 26eb416..311aa8b 100644
--- a/logging.go
+++ b/logging.go
@@ -4,8 +4,32 @@ import (
 	"fmt"
 	"log"
 	"net/http"
+	"os"
 )
 
+var logChan = make(chan struct{})
+
+func initLogging() {
+	logfile, err := os.OpenFile("api.log", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
+	if err != nil {
+		log.SetOutput(os.Stderr)
+		log.Printf("Log init error: %s", err.Error())
+		return
+	}
+
+	go func(*os.File) {
+		<-logChan
+		log.Printf("Closing log file ...")
+		err := logfile.Close()
+		if err != nil {
+			log.SetOutput(os.Stderr)
+			log.Printf("Error closing log file: %s", err.Error())
+		}
+	}(logfile)
+
+	log.SetOutput(logfile)
+}
+
 // Appends a 200 OK to the request log
 func log200(r *http.Request) {
 	useragent := r.Header["User-Agent"]