summaryrefslogtreecommitdiff
path: root/contrib/log
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-01-09 16:40:24 -0700
committertjpcc <tjp@ctrl-c.club>2023-01-09 16:40:24 -0700
commitff05d62013906f3086b452bfeda3e0d5b9b7a541 (patch)
tree3be29de0b1bc7c273041c6d89b71ca447c940556 /contrib/log
Initial commit.
some basics: - minimal README - some TODOs - server and request handler framework - contribs: file serving, request logging - server examples - CI setup
Diffstat (limited to 'contrib/log')
-rw-r--r--contrib/log/log.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/contrib/log/log.go b/contrib/log/log.go
new file mode 100644
index 0000000..2ccd3bc
--- /dev/null
+++ b/contrib/log/log.go
@@ -0,0 +1,35 @@
+package log
+
+import (
+ "context"
+ "io"
+ "time"
+
+ kitlog "github.com/go-kit/log"
+
+ "tildegit.org/tjp/gus/gemini"
+)
+
+func Requests(out io.Writer, logger kitlog.Logger) gemini.Middleware {
+ if logger == nil {
+ logger = kitlog.NewLogfmtLogger(kitlog.NewSyncWriter(out))
+ }
+
+ return func(next gemini.Handler) gemini.Handler {
+ return func(ctx context.Context, r *gemini.Request) (resp *gemini.Response) {
+ start := time.Now()
+ defer func() {
+ end := time.Now()
+ logger.Log(
+ "msg", "request",
+ "ts", end,
+ "dur", end.Sub(start),
+ "url", r.URL,
+ "status", resp.Status,
+ )
+ }()
+
+ return next(ctx, r)
+ }
+ }
+}