clean up logging and hook up run fns to api proc handlers
This commit is contained in:
parent
a480f98cca
commit
ef4f8959df
6 changed files with 21 additions and 174 deletions
|
|
@ -1,15 +1,25 @@
|
||||||
(ns micro-blog.api
|
(ns micro-blog.api
|
||||||
(:require [io.pedestal.connector :as conn]
|
(:require [io.pedestal.connector :as conn]
|
||||||
[io.pedestal.http.http-kit :as hk]
|
[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]
|
(defn mastodon-proc-handler []
|
||||||
;; (log/info :in 'greet-handler :method (:request-method request) :uri (:uri request))
|
(tel/log! :info "Procing Mastodon Scrape")
|
||||||
|
(micro-blog.mastodon/run)
|
||||||
{:status 200
|
{: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
|
(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 []
|
(defn create-connector []
|
||||||
(-> (conn/default-connector-map 8890)
|
(-> (conn/default-connector-map 8890)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
(ns micro-blog.config
|
(ns micro-blog.config)
|
||||||
(:require
|
|
||||||
[taoensso.telemere :as tel]))
|
|
||||||
|
|
||||||
(def ^:private env-overrides
|
(def ^:private env-overrides
|
||||||
{:pocket-base-pw "POCKET_BASE_PW"
|
{:pocket-base-pw "POCKET_BASE_PW"
|
||||||
|
|
|
||||||
|
|
@ -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)
|
|
||||||
)
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
(ns micro-blog.logging
|
(ns micro-blog.logging.main
|
||||||
(:import [java.time Instant])
|
(:import [java.time Instant])
|
||||||
(:require
|
(:require
|
||||||
[taoensso.telemere :as tel]
|
[taoensso.telemere :as tel]
|
||||||
[cheshire.core :as json]
|
[cheshire.core :as json]
|
||||||
[cheshire.generate :as gen]
|
[cheshire.generate :as gen]
|
||||||
[micro-blog.pedestal-telmere :as pt]))
|
[micro-blog.logging.pedestal-telmere :as pt]))
|
||||||
|
|
||||||
(def json-logging-handler
|
(def json-logging-handler
|
||||||
(tel/handler:console
|
(tel/handler:console
|
||||||
|
|
@ -28,10 +28,3 @@
|
||||||
(tel/add-handler! ::json-logger json-logging-handler)
|
(tel/add-handler! ::json-logger json-logging-handler)
|
||||||
;; Also setup Pedestal integration
|
;; Also setup Pedestal integration
|
||||||
(pt/setup-pedestal-telmere-logging!))
|
(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!"))
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
(ns micro-blog.pedestal-telmere
|
(ns micro-blog.logging.pedestal-telmere
|
||||||
(:require [io.pedestal.log :as pedestal-log]
|
(:require [io.pedestal.log :as pedestal-log]
|
||||||
[taoensso.telemere :as tel]))
|
[taoensso.telemere :as tel]))
|
||||||
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
(:import [java.time Instant Duration])
|
(:import [java.time Instant Duration])
|
||||||
(:require [chime.core :as chime]
|
(:require [chime.core :as chime]
|
||||||
[taoensso.telemere :as tel]
|
[taoensso.telemere :as tel]
|
||||||
micro-blog.logging
|
[micro-blog.logging.main :as logging]
|
||||||
micro-blog.api
|
micro-blog.api
|
||||||
[micro-blog.blue-sky :as blue-sky]
|
[micro-blog.blue-sky :as blue-sky]
|
||||||
[micro-blog.mastodon :as masto]))
|
[micro-blog.mastodon :as masto]))
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
masto/run])
|
masto/run])
|
||||||
|
|
||||||
(defn -main []
|
(defn -main []
|
||||||
(micro-blog.logging/setup-logging)
|
(logging/setup-logging)
|
||||||
(tel/log! :info "Setting up API")
|
(tel/log! :info "Setting up API")
|
||||||
(micro-blog.api/start)
|
(micro-blog.api/start)
|
||||||
(tel/log! :info "Setting up crons")
|
(tel/log! :info "Setting up crons")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue