From ef4f8959dfc31f2ebd99d57aab513a3ba7710565 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Tue, 12 Aug 2025 14:39:20 +0200 Subject: [PATCH] clean up logging and hook up run fns to api proc handlers --- src/micro_blog/api.clj | 20 ++- src/micro_blog/config.clj | 4 +- src/micro_blog/example_usage.clj | 154 ------------------ .../{logging.clj => logging/main.clj} | 11 +- .../{ => logging}/pedestal_telmere.clj | 2 +- src/micro_blog/main.clj | 4 +- 6 files changed, 21 insertions(+), 174 deletions(-) delete mode 100644 src/micro_blog/example_usage.clj rename src/micro_blog/{logging.clj => logging/main.clj} (71%) rename src/micro_blog/{ => logging}/pedestal_telmere.clj (97%) diff --git a/src/micro_blog/api.clj b/src/micro_blog/api.clj index c918e0c..f1db175 100644 --- a/src/micro_blog/api.clj +++ b/src/micro_blog/api.clj @@ -1,15 +1,25 @@ (ns micro-blog.api (:require [io.pedestal.connector :as conn] [io.pedestal.http.http-kit :as hk] - [io.pedestal.log :as log])) + micro-blog.mastodon + micro-blog.blue-sky + [taoensso.telemere :as tel])) -(defn greet-handler [request] - ;; (log/info :in 'greet-handler :method (:request-method request) :uri (:uri request)) +(defn mastodon-proc-handler [] + (tel/log! :info "Procing Mastodon Scrape") + (micro-blog.mastodon/run) {:status 200 - :body "Hello, world!\n"}) + :body "Scraped Mastodon\n"}) + +(defn blue-sky-proc-handler [] + (tel/log! :info "Procing BlueSky Scrape") + (micro-blog.mastodon/run) + {:status 200 + :body "Scraped Mastodon\n"}) (def routes - #{["/greet" :get greet-handler :route-name :greet]}) + #{["/bluesky" :get blue-sky-proc-handler :route-name :blue-sky] + ["/mastodon" :get mastodon-proc-handler :route-name :mastodon]}) (defn create-connector [] (-> (conn/default-connector-map 8890) diff --git a/src/micro_blog/config.clj b/src/micro_blog/config.clj index feaa106..7b15275 100644 --- a/src/micro_blog/config.clj +++ b/src/micro_blog/config.clj @@ -1,6 +1,4 @@ -(ns micro-blog.config - (:require - [taoensso.telemere :as tel])) +(ns micro-blog.config) (def ^:private env-overrides {:pocket-base-pw "POCKET_BASE_PW" diff --git a/src/micro_blog/example_usage.clj b/src/micro_blog/example_usage.clj deleted file mode 100644 index 633095f..0000000 --- a/src/micro_blog/example_usage.clj +++ /dev/null @@ -1,154 +0,0 @@ -(ns micro-blog.example-usage - (:require [io.pedestal.log :as log] - [io.pedestal.connector :as conn] - [io.pedestal.http.http-kit :as hk] - [taoensso.telemere :as tel] - [micro-blog.pedestal-telmere :as pt] - [micro-blog.logging :as logging])) - -;; Example handlers that demonstrate Pedestal logging with Telmere - -(defn health-check-handler [request] - (log/info :in 'health-check-handler - :request-id (get-in request [:headers "x-request-id"]) - :user-agent (get-in request [:headers "user-agent"])) - {:status 200 - :headers {"Content-Type" "application/json"} - :body "{\"status\": \"healthy\", \"timestamp\": \"2024-01-01T12:00:00Z\"}"}) - -(defn error-handler [request] - (try - (throw (ex-info "Simulated error for demonstration" - {:error-type :simulation - :request-path (:uri request)})) - (catch Exception e - (log/error :in 'error-handler - :exception e - :request-method (:request-method request) - :uri (:uri request)) - {:status 500 - :headers {"Content-Type" "application/json"} - :body "{\"error\": \"Internal server error\"}")))) - -(defn user-handler [request] - (let [user-id (get-in request [:path-params :user-id])] - (log/debug :in 'user-handler :user-id user-id :action "fetching user") - - (if (= user-id "123") - (do - (log/info :in 'user-handler :user-id user-id :result "found") - {:status 200 - :headers {"Content-Type" "application/json"} - :body (str "{\"user_id\": \"" user-id "\", \"name\": \"John Doe\"}")}) - (do - (log/warn :in 'user-handler :user-id user-id :result "not-found") - {:status 404 - :headers {"Content-Type" "application/json"} - :body "{\"error\": \"User not found\"}"}))))) - -;; Interceptor that adds request logging -(def request-logger-interceptor - {:name ::request-logger - :enter (fn [context] - (let [request (:request context) - start-time (System/currentTimeMillis)] - (log/debug :in 'request-logger-interceptor - :event :request-start - :method (:request-method request) - :uri (:uri request) - :start-time start-time) - (assoc context ::start-time start-time))) - :leave (fn [context] - (let [request (:request context) - response (:response context) - start-time (::start-time context) - duration (- (System/currentTimeMillis) start-time)] - (log/info :in 'request-logger-interceptor - :event :request-complete - :method (:request-method request) - :uri (:uri request) - :status (:status response) - :duration-ms duration) - context))}) - -;; Routes with logging examples -(def routes - #{["/health" :get health-check-handler :route-name :health-check] - ["/error" :get error-handler :route-name :error-demo] - ["/users/:user-id" :get user-handler :route-name :get-user]}) - -(defn create-example-connector [] - (-> (conn/default-connector-map 8891) - (conn/with-default-interceptors) - (conn/with-interceptors [request-logger-interceptor]) - (conn/with-routes routes) - (hk/create-connector nil))) - -(defn start-example-server [] - ;; Setup Telmere logging first - (logging/setup-logging) - - (log/info :in 'start-example-server - :msg "Starting example server with Pedestal + Telmere logging" - :port 8891) - - ;; Start the server - (conn/start! (create-example-connector))) - -(defn demo-logging [] - "Demonstrates various logging scenarios" - ;; Setup logging first - (logging/setup-logging) - - ;; Direct Telmere logging - (tel/log! {:level :info :data {:source "telmere-direct"}} - "This is direct Telmere logging") - - ;; Pedestal logging (which goes through Telmere) - (log/trace :source "pedestal" :level "trace" :msg "Trace level message") - (log/debug :source "pedestal" :level "debug" :msg "Debug level message") - (log/info :source "pedestal" :level "info" :msg "Info level message") - (log/warn :source "pedestal" :level "warn" :msg "Warning level message") - (log/error :source "pedestal" :level "error" :msg "Error level message") - - ;; Logging with structured data - (log/info :in 'demo-function - :operation "data-processing" - :records-processed 150 - :processing-time-ms 250 - :success true) - - ;; Logging with exception - (try - (/ 1 0) - (catch Exception e - (log/error :in 'demo-function - :operation "division" - :exception e - :msg "Division by zero error"))) - - ;; Using MDC context - (log/with-context {:user-id "user-123" :session-id "sess-456"} - (log/info :in 'demo-function :msg "Processing user request") - (log/debug :in 'demo-function :msg "Validating user permissions")) - - (println "Demo logging complete - check your console output!")) - -(comment - ;; To test the integration: - - ;; 1. Start the example server - (start-example-server) - - ;; 2. Make some requests: - ;; curl http://localhost:8891/health - ;; curl http://localhost:8891/users/123 - ;; curl http://localhost:8891/users/999 - ;; curl http://localhost:8891/error - - ;; 3. Run logging demo - (demo-logging) - - ;; 4. Test just the logging setup - (logging/test-logging) - ) diff --git a/src/micro_blog/logging.clj b/src/micro_blog/logging/main.clj similarity index 71% rename from src/micro_blog/logging.clj rename to src/micro_blog/logging/main.clj index 4a17efd..a525193 100644 --- a/src/micro_blog/logging.clj +++ b/src/micro_blog/logging/main.clj @@ -1,10 +1,10 @@ -(ns micro-blog.logging +(ns micro-blog.logging.main (:import [java.time Instant]) (:require [taoensso.telemere :as tel] [cheshire.core :as json] [cheshire.generate :as gen] - [micro-blog.pedestal-telmere :as pt])) + [micro-blog.logging.pedestal-telmere :as pt])) (def json-logging-handler (tel/handler:console @@ -28,10 +28,3 @@ (tel/add-handler! ::json-logger json-logging-handler) ;; Also setup Pedestal integration (pt/setup-pedestal-telmere-logging!)) - -;; (defn test-logging [] -;; (setup-logging) -;; (tel/log! {:level :info :data {:thing "example"}} "This is a test log message") -;; ;; Test Pedestal logging integration -;; (require '[io.pedestal.log :as log]) -;; ((resolve 'log/info) :test true :message "Pedestal logging via Telmere works!")) diff --git a/src/micro_blog/pedestal_telmere.clj b/src/micro_blog/logging/pedestal_telmere.clj similarity index 97% rename from src/micro_blog/pedestal_telmere.clj rename to src/micro_blog/logging/pedestal_telmere.clj index 7710bd8..f422885 100644 --- a/src/micro_blog/pedestal_telmere.clj +++ b/src/micro_blog/logging/pedestal_telmere.clj @@ -1,4 +1,4 @@ -(ns micro-blog.pedestal-telmere +(ns micro-blog.logging.pedestal-telmere (:require [io.pedestal.log :as pedestal-log] [taoensso.telemere :as tel])) diff --git a/src/micro_blog/main.clj b/src/micro_blog/main.clj index f7b1ecc..cb84b05 100644 --- a/src/micro_blog/main.clj +++ b/src/micro_blog/main.clj @@ -3,7 +3,7 @@ (:import [java.time Instant Duration]) (:require [chime.core :as chime] [taoensso.telemere :as tel] - micro-blog.logging + [micro-blog.logging.main :as logging] micro-blog.api [micro-blog.blue-sky :as blue-sky] [micro-blog.mastodon :as masto])) @@ -12,7 +12,7 @@ masto/run]) (defn -main [] - (micro-blog.logging/setup-logging) + (logging/setup-logging) (tel/log! :info "Setting up API") (micro-blog.api/start) (tel/log! :info "Setting up crons")