6

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)

Steve
  • 1,596
  • 1
  • 10
  • 21

4 Answers4

3

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

Chris
  • 1,090
  • 2
  • 11
  • 22
  • OK, that works! But the other three do not! What's going on here?! – Steve Oct 24 '14 at 00:38
  • Wish I knew. I believe the problem is caused by trying to print via multiple threads. Not sure why my method works while the others fail. The clojure mailing list might be a better shot. – Chris Oct 24 '14 at 02:28
1

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.

ChrisBlom
  • 1,262
  • 12
  • 17
  • When I put "clojure.tools.trace/trace" or "println" or "print" statements in my code, they appear nowhere. They do not show in my cider buffer, or my running site's log. This doesn't work either when I use cider-jack-in or cider-connect to connect to a running site. – Steve Oct 20 '14 at 13:24
0

The Prone library may help you out. It is has a ring middleware for better exception reporting that also has the ability for debugging. When debugging, you can inspect any local bindings as well as the Ring request.

Here is a video that demonstrates how it works

Odinodin
  • 2,147
  • 3
  • 25
  • 43
0

Use (flush) after your print expressions to force output.

David L
  • 136
  • 6