Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing *out* in Clojure with Lein and Ring

I am running Lein 2 and cider 0.7.0. I made a sample ring app that uses ring/run-jetty to start.

(ns nimbus-admin.handler
  (:require [compojure.core :refer :all]
            [compojure.handler :as handler]
            [clojure.tools.nrepl.server :as nrepl-server]
            [cider.nrepl :refer (cider-nrepl-handler)]
            [ring.adapter.jetty :as ring]
            [clojure.tools.trace :refer [trace]]
            [ring.util.response :refer [resource-response response redirect content-type]]
            [compojure.route :as route])
  (:gen-class))


(defroutes app-routes 
  (GET "/blah" req "blah")
  (route/resources "/")
  (route/not-found (trace "not-found" "Not Found")))

(def app (handler/site app-routes))

(defn start-nrepl-server []
  (nrepl-server/start-server :port 7888 :handler cider-nrepl-handler))

(defn start-jetty [ip port]
  (ring/run-jetty app {:port port :ip ip}))

(defn -main
  ([] (-main 8080 "0.0.0.0"))
  ([port ip & args] 
     (let [port (Integer. port)]
       (start-nrepl-server)
       (start-jetty ip port))))

then connect to it with cider like:

cider-connect 127.0.0.1 7888

I can navigate to my site and eval forms in emacs and it will update what is running live in my nrepl session, so that is great.

I cannot see output, either with (print "test") (println "test") (trace "out" 1)

Finally, my project file:

(defproject nimbus-admin "0.1.0"
  :description ""
  :url ""
  :min-lein-version "2.0.0"
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [com.climate/clj-newrelic "0.1.1"]
                 [com.ashafa/clutch "0.4.0-RC1"]
                 [ring "1.3.1"]
                 [clj-time "0.8.0"]
                 [midje "1.6.3"]
                 [org.clojure/tools.nrepl "0.2.6"]
                 [ring/ring-json "0.3.1"]
                 [org.clojure/tools.trace "0.7.8"]
                 [compojure "1.1.9"]
                 [org.clojure/data.json "0.2.5"]
                 [org.clojure/core.async "0.1.346.0-17112a-alpha"]
                 ]
  :plugins [[lein-environ "1.0.0"]
            [cider/cider-nrepl "0.7.0"]]
  :main nimbus-admin.handler)

I start the site with lein run

Edit I CAN see output, ONLY when using (.println System/out msg)

like image 319
Steve Avatar asked Mar 12 '14 23:03

Steve


2 Answers

Have you tried (.println System/out msg)? I had the same problem and this worked for me.

like image 75
Chris Avatar answered Sep 28 '22 16:09

Chris


It's possible to just put print statements in your code manually. If you want to print information about each request, you can add middleware. The handler you pass to jetty is a function from Ring requests to Ring responses. Ring request and responses are just maps, see the Ring spec for more which keys they should contain. Middleware is just a function that takes a handler as its first argument and returns a handler.

Example of a middleware function to print basic info about requests and responses:

(defn log-middleware [handler]
  (fn [request]
    (let [response (handler request)]
      (println "=>" (name (:request-method request)) ":" (:uri request))
      (println "<=" (:status request))
      response)))

This middleware should print to the cider repl buffer, but cider behaves strangely sometimes and send output to *Messages* or the nrepl server buffer.

You use this middleware by applying it to your handlers:

 (def application (log-middleware (handler/site routes)))

Headers could be printed this way to: just get the :headers field form the request map and print it.

like image 36
ChrisBlom Avatar answered Sep 28 '22 17:09

ChrisBlom