154 lines
5.4 KiB
Clojure
154 lines
5.4 KiB
Clojure
(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)
|
|
)
|