pedestal

erwinrooijakkers 2019-08-29T14:50:06.016500Z

I want to add custom logging

erwinrooijakkers 2019-08-29T14:50:39.017Z

The goal is basically to ignore requests from a certain IP range

erwinrooijakkers 2019-08-29T14:50:48.017400Z

(Kubernetes master calling “/”)

erwinrooijakkers 2019-08-29T14:51:03.017600Z

I’ve got some Ring middleware to do that

erwinrooijakkers 2019-08-29T14:51:12.018Z

Is there a way to include that in Pedestal?

erwinrooijakkers 2019-08-29T14:51:31.018400Z

Now I see this:

INFO  io.pedestal.http  - {:msg "GET /", :line 80}
INFO  io.pedestal.http  - {:msg "GET /", :line 80}
INFO  io.pedestal.http  - {:msg "GET /", :line 80}
...
That needs to go

erwinrooijakkers 2019-08-29T14:52:22.018700Z

Middleware looks like this:

(defn logging-middleware [handler]
  (-> handler
      (middleware.conditional/if not-request-from-internal-ip? wrap-with-info-logger)))

2019-08-29T15:13:06.019100Z

@erwinrooijakkers you can provide your own request logger. See https://github.com/pedestal/pedestal/blob/master/service/src/io/pedestal/http.clj#L205-L206

2019-08-29T15:13:41.019800Z

Add :io.pedestal.http/request-logger to your service map

erwinrooijakkers 2019-08-29T15:14:01.020Z

thanks 🙂

erwinrooijakkers 2019-08-29T15:33:37.020200Z

Works like a charm

erwinrooijakkers 2019-08-29T15:34:51.020400Z

For those interested:

(defn- from-internal-ip?
  "Returns truthy when a request does not come from an internal IP (e.g., a
  Kubernetes node)"
  [{:keys [server-name] :as request}]
  (let [internal-ip-start "10."]
    (string/starts-with? (str server-name) internal-ip-start)))

(def log-request
  "Log the request's method and uri."
  (interceptor.helpers/on-request
   ::log-request
   (fn [request]
     (when-not (from-internal-ip? request)
       (log/info :msg (format "%s %s"
                              (string/upper-case (name (:request-method request)))
                              (:uri request))))
     request)))