From c3c4ea9e55e5cfb7c932be4ccc7bb05ae3fbc6a6 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 8 Feb 2023 23:15:13 +1300 Subject: [PATCH] Add basic request logging, disabled by default --- src/application-globals.rkt | 2 +- src/config.rkt | 4 ++- src/log.rkt | 54 ++++++++++++++++++++++++++++++++++ src/page-set-user-settings.rkt | 2 ++ src/page-static-archive.rkt | 4 ++- src/page-wiki-offline.rkt | 9 ++++-- 6 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/log.rkt diff --git a/src/application-globals.rkt b/src/application-globals.rkt index 52fd937..7cc12fe 100644 --- a/src/application-globals.rkt +++ b/src/application-globals.rkt @@ -61,7 +61,7 @@ "Documentation and more information")) (p (a (@ (href "https://lists.sr.ht/~cadence/breezewiki-discuss")) - "Discussions / Bug reports / Feature requests")) + "Chat / Bug reports / Feature requests")) ,(if (config-true? 'instance_is_official) `(p ,(format "This instance is run by the ~a developer, " (config-get 'application_name)) (a (@ (href "https://cadence.moe/contact")) diff --git a/src/config.rkt b/src/config.rkt index 4b79f4a..90e1b99 100644 --- a/src/config.rkt +++ b/src/config.rkt @@ -39,7 +39,9 @@ (feature_offline::enabled . "false") (feature_offline::format . "json.gz") - (feature_offline::only . "false"))) + (feature_offline::only . "false") + + (access_log::enabled . "false"))) (define loaded-alist (with-handlers diff --git a/src/log.rkt b/src/log.rkt new file mode 100644 index 0000000..5647986 --- /dev/null +++ b/src/log.rkt @@ -0,0 +1,54 @@ +#lang typed/racket/base +(require racket/file + racket/port + racket/runtime-path + racket/string + typed/srfi/19 + "config.rkt") + +(provide + log-page-request + log-styles-request + log-set-settings-request) + +(define last-flush 0) +(define flush-every-millis 60000) + +(define-runtime-path log-file "../storage/logs/access-0.log") +(define log-port + (if (config-true? 'access_log::enabled) + (begin + (make-directory* (simplify-path (build-path log-file 'up))) + (open-output-file log-file #:exists 'append)) + (open-output-nowhere))) + +(: get-date-iso8601 (-> String)) +(define (get-date-iso8601) + (date->string (current-date 0) "~5")) + +(: offline-string (Boolean -> String)) +(define (offline-string offline?) + (if offline? "---" "ooo")) + +(: log (String * -> Void)) +(define (log . entry) + ;; create log entry string + (define full-entry (cons (get-date-iso8601) entry)) + ;; write to output port + (displayln (string-join full-entry ";") log-port) + ;; flush output port to file (don't do this too frequently) + (when ((- (current-milliseconds) last-flush) . >= . flush-every-millis) + (flush-output log-port) + (set! last-flush (current-milliseconds)))) + +(: log-page-request (Boolean String String (U 'light 'dark 'default) -> Void)) +(define (log-page-request offline? wikiname title theme) + (log "page" (offline-string offline?) wikiname title (symbol->string theme))) + +(: log-styles-request (Boolean String String -> Void)) +(define (log-styles-request offline? wikiname basename) + (log "style" (offline-string offline?) wikiname basename)) + +(: log-set-settings-request (Symbol -> Void)) +(define (log-set-settings-request theme) + (log "settings" (symbol->string theme))) diff --git a/src/page-set-user-settings.rkt b/src/page-set-user-settings.rkt index 588129d..b949142 100644 --- a/src/page-set-user-settings.rkt +++ b/src/page-set-user-settings.rkt @@ -4,6 +4,7 @@ web-server/http "application-globals.rkt" "data.rkt" + "log.rkt" "../lib/url-utils.rkt" "../lib/xexpr-utils.rkt") @@ -14,5 +15,6 @@ (response-handler (define next-location (dict-ref (url-query (request-uri req)) 'next_location)) (define new-settings (read (open-input-string (dict-ref (url-query (request-uri req)) 'new_settings)))) + (log-set-settings-request (user-cookies^-theme new-settings)) (define headers (user-cookies-setter new-settings)) (generate-redirect next-location #:headers headers))) diff --git a/src/page-static-archive.rkt b/src/page-static-archive.rkt index 0d01a59..a2c69ed 100644 --- a/src/page-static-archive.rkt +++ b/src/page-static-archive.rkt @@ -12,7 +12,8 @@ "../archiver/archiver.rkt" "../lib/mime-types.rkt" "../lib/xexpr-utils.rkt" - "../src/config.rkt") + "config.rkt" + "log.rkt") (provide page-static-archive) @@ -53,6 +54,7 @@ (define (handle-style wikiname dest) (when (config-true? 'debug) (printf "using offline mode for style ~a ~a~n" wikiname dest)) + (log-styles-request #t wikiname dest) (define fs-path (build-path path-archive wikiname "styles" dest)) (println fs-path) (unless (file-exists? fs-path) diff --git a/src/page-wiki-offline.rkt b/src/page-wiki-offline.rkt index 064ba05..a3986da 100644 --- a/src/page-wiki-offline.rkt +++ b/src/page-wiki-offline.rkt @@ -22,6 +22,7 @@ "application-globals.rkt" "config.rkt" "data.rkt" + "log.rkt" "page-wiki.rkt" "../lib/archive-file-mappings.rkt" "../lib/pure-utils.rkt" @@ -47,6 +48,12 @@ (define maybe-hashed-basename (if ((string-length basename) . > . 240) (sha1 (string->bytes/latin-1 basename)) basename)) + + (define user-cookies (user-cookies-getter req)) + (define theme (user-cookies^-theme user-cookies)) + + (log-page-request #t wikiname maybe-hashed-basename theme) + (define archive-format (case (config-get 'feature_offline::format) [(".json" "json") (cons "~a.json" (λ () (read-json)))] @@ -98,8 +105,6 @@ (define original-page (html->xexp (preprocess-html-wiki (jp "/parse/text" data)))) (define page ((query-selector (λ (t a c) (has-class? "mw-parser-output" a)) original-page))) (define initial-head-data ((head-data-getter wikiname) data)) - (define user-cookies (user-cookies-getter req)) - (define theme (user-cookies^-theme user-cookies)) (define head-data (case theme [(light dark)