Search code examples
loggingclojurepprint

How to log which is the Pretty print like pprint by clojure.tools.logging?


I am using org.clojure/tools.logging. I want to get a function which can pretty print the log, but I can't find it. For example, the content which i want to print is

{:status 401,
 :headers {"Content-Type" "application/octet-stream"},
 :body {:error "You don't login."}}

but i call the function name is info,I get this

{:status 401, :headers {Content-Type application/octet-stream}, :body {:error You don't login.}}

Solution

  • How about something like this:

    (require '[clojure.pprint :as pprint])
    
    (defn pformat [& args]
            (with-out-str
              (apply pprint/pprint args)))
    

    Then:

    (require '[clojure.tools.logging :as log])
    
    (log/info (pformat {:status 401,
                        :headers {"Content-Type" "application/octet-stream"},
                        :body {:error "You don't login."}}))
    

    Which outputs something like this:

    Apr 29, 2015 9:43:40 AM user invoke
    INFO: {:headers {"Content-Type" "application/octet-stream"},
     :status 401,
     :body {:error "You don't login."}}