In the latest version (0.2.4) of tools.logging, when logging with (error some-exception), only the exception message is logged. I depend on having the stack trace printed when an exception is logged. Printing the exception to stderr or stdout is not an option.
As far as I can see in the source, (error ...) does take an exception as the first argument.
(log/error throwable error-message)
What can I do to include the stack traces when logging?
The signature of error is:
(defmacro error
"Error level logging using print-style args."
{:arglists '([message & more] [throwable message & more])}
[& args]
`(logp :error ~#args))
which means that with just 1 param (as in (error some-exception)), the parameter is the message (which in your case is the toString of some-exception).
If you want to log the stacktrace you need a second param message like:
(def ex (RuntimeException. "ex"))
(error ex "Something broke!")
or
(error ex ex)
Related
There's something fundamental I'm not getting here. I expected the following test to pass. But the 2nd test case "staging"/"staging" fails. Its as if with-redefs-fn is failing to advance through the test-case instances. But the logging says everything is fine. This is confusing.
(deftest test-bad-derive-s3-environment
(testing "variants of props environments"
(doseq [test-case [{:env "qa1" :expect "qa1"}
{:env "dev" :expect "qa1"}
{:env "staging" :expect "staging"}]]
(log/infof "test-case %s" test-case)
(with-redefs-fn {#'config/environment (fn [] (:env test-case))}
(let [actual (fs/derive-s3-environment (config/environment))
_ (log/infof "within redefs :env %s :expect %s" (:env test-case) (:expect test-case))]
#(is (= actual (:expect test-case))))))))
...
lein test com.climate.test.mapbook.filestore
2016-05-03 16:16:29,353 INFO filestore:288 - test-case {:env "qa1", :expect "qa1"}
2016-05-03 16:16:29,355 INFO EnvConfig:98 - Loading config properties from /export/disk0/wb/etc/env.properties
2016-05-03 16:16:29,357 INFO EnvConfig:98 - Loading config properties from /export/disk0/wb/etc/local.properties
2016-05-03 16:16:29,358 INFO filestore:288 - within redefs :env qa1 :expect qa1
2016-05-03 16:16:29,359 INFO filestore:288 - test-case {:env "staging", :expect "staging"}
2016-05-03 16:16:29,359 INFO filestore:288 - within redefs :env staging :expect staging
lein test :only com.climate.test.mapbook.filestore/test-bad-derive-s3-environment
FAIL in (test-bad-derive-s3-environment) (filestore.clj:29)
variants of props environments
expected: (= actual (:expect test-case))
actual: (not (= "qa1" "staging"))
2016-05-03 16:16:29,364 INFO filestore:288 - test-case {:env "dev", :expect "qa1"}
2016-05-03 16:16:29,364 INFO filestore:288 - within redefs :env dev :expect qa1
Why does my with-redefs-fn fail to redefine the config/environment function in terms of the current test-case?
First of all, notice that your final test instance has an :expect of "qa1" – the same as the first test instance – so it should actually fail if the code worked as you intended it to; its passing is a symptom of the same problem as the second instance's failing.
Now for the fix – there are two options:
Just use with-redefs instead of with-redefs-fn:
(with-redefs [config/environment (fn [] (:env test-case))]
…)
Most of the time this is what you want to do and you can consider with-redefs-fn to be an implementation detail behind with-redefs – although strictly speaking it does have some utility of its own in that it can redefine dynamically constructed collections of Vars.
Use with-redefs-fn, but move the inner let form inside the anonymous function:
(with-redefs-fn {…}
#(let […]
(is …)))
Finally, the reason these work and the version from the question text does not:
with-redefs-fn is a function, so at runtime its arguments will be evaluated before it is actually invoked with their runtime values passed in. In particular, the let expression that you pass in as the second argument will be evaluated before the redefinition takes place, and so the local called actual will get the result of evaluating (config/environment) before the redefinition as its value, and that value will be installed in the anonymous closure created in the let's body. That closure, however, will then be called with the redefinition in place, and so it will take its notion of the "actual" value from before the redefinition and compare it with the expectation set after the redefinition, resulting in the observed behaviour.
Moving the let inside the closure, as in the second approach above, fixes this mismatch problem – the let local's value is computed with the redefinition in place and all is well. The first approach using with-redefs expands to the second approach.
The log printouts are fine, because they are only concerned with the doseq local and never examine any Vars. If they did, they would only see the pre-redefinition values.
I'm trying out writing some elisp for the first time. I'd like to be able to run unit tests on it.
I have a buffer with the following in it:
(ert-deftest addition-test ()
(should (= (+ 1 2) 4)))
And I ran M-x eval-buffer to install the ert-deftest definition into my emacs. When I run M-x ert RET t RET, I see:
Selector: t
Passed: 0
Failed: 0
Total: 0/1
Started at: 2015-11-16 00:27:46-0800
Aborted.
Aborted at: 2015-11-16 00:27:46-0800
A
A addition-test
aborted
And I see the message "Test failed: ...." in the minibuffer at the bottom of the screen. Why is the failure reported there rather in the ERT buffer?
EDIT:
It appears that the "aborted" message occurs after a failing test is hit, then any remaining tests aren't run. Example:
(ert-deftest test1 ()
(should 1))
(ert-deftest test2 ()
(should nil))
(ert-deftest test3 ()
(should 1))
M-x eval-buffer RET M-x ert RET opens up a buffer named *ert* that says:
Selector: t
Passed: 1
Failed: 0
Total: 1/3
Started at: 2015-11-16 02:06:57-0800
Aborted.
Aborted at: 2015-11-16 02:06:57-0800
.A-
A test2
aborted
And in *Messages* I see:
Aborted: Ran 3 tests, 1 results were as expected
ert-fail: Test failed: ((should nil) :form nil :value nil)
If I go in the *ert* and press d over test2, I get the following in *Messages*:
Running test test2...ABORTED
ert-fail: Test failed: ((should nil) :form nil :value nil)
I can convince it to run test3 if I hit enter on the - in .A-, expand test3, then hit d on it. That looks like:
Selector: t
Passed: 2
Failed: 0
Total: 2/3
Started at: 2015-11-16 02:08:40-0800
Aborted.
Aborted at: 2015-11-16 02:08:40-0800
.A.
A test2
aborted
. test3
passed
In terms of getting a backtrace, hitting b over test2 gives me:
ert-results-pop-to-backtrace-for-test-at-point: cl-etypecase failed: [cl-struct-ert-test-aborted-with-non-local-exit (((should nil) :form nil :value nil))], (ert-test-passed ert-test-result-with-condition)
I don't understand the output of the backtrace at all. This behavior doesn't seem to match what the ERT documentation says so I'm really puzzled. What's going on here?
The code you have posted works in a clean Emacs started with emacs -Q. Most likely, it is caused by some library that has been loaded somehow.
I have a compojure route which returns a file. I want to test -
1) If a file is returned.
2) The specific file that was returned.
When I run (app (ring.mock/request :get "/myroute")) I get
{:body #<File resources/public/templates/index.html>, :headers {"Content-Length" "2349", "Last-Modified" "Sat, 16 Mar 2013 11:01:03 GMT"}, :status 200}
How do I check that the returned value in the body is of a type file ? And getting more ambitious can I check it is the file located at 'resources/public/templates/index.html' ?
Ring requests are just maps, so you can extract the body with the :body keyword and then check it's type with type
(type (:body (app (ring.mock/request :get "/myroute"))))
(perhaps I'm not understanding the question though?)
Newbie Clojure and leiningen question:
Given the code snippet in my project below, this works from the lein repl :
==> (-main "something")
produces the expected "Command: something ... running ... done"
but doesn't work from the command line:
me pallet1]lein run "something"
produces "Command: something ... error: not resolved as a command"
Why? / how do I fix it?
To reproduce:
lein new eg
Then edit the generated project file, adding :main eg.core to define the main function, and edit the generated src/eg/core.clj file, and paste this in:
core.clj
(ns eg.core)
(defn something [] (println "Something!"))
(defn run-command-if-any [^String commandname]
(printf "Command: %s ..." commandname)
(if-let [cmd (ns-resolve *ns* (symbol commandname))]
(
(println "running ...") (cmd) (println "done.")
)
(println "error: not resolved as a command.")
))
(defn -main [ commandname ] (run-command-if-any commandname))
Then
lein repl
eg.core=> (-main "something")
works (ie prints "Something!) , but
lein run something
doesn't (ie prints the "error: not resolved" message)
The problem is that when you run it from lein your default namespace is "user" namespace:
(defn -main [ commandname ] (println *ns*))
Prints #<Namespace user>. So it doesn't contain something function because it is from another namespace. You have several choices:
Pass fully qualified function name: your-namespace/something instead of something.
Use your-namespace instead of *ns*: (ns-resolve 'your-namespace (symbol commandname))
Change namespace to your-namespace in -main.
Example of method 3:
(defn -main [ commandname ]
(in-ns 'your-namespace)
(run-command-if-any commandname))
Also you if you want to call several functions one by one you should use do:
(do (println "Hello")
(println "World"))
Not just braces like ( (println "hello") (println "World"))
the lein exec plugin is very useful for scripting such things in the context of a project. I have used this extensively for writing Jenkins jobs in clojure and other scripting situations
lein exec -pe '(something ...) (something-else) (save-results)'
I'm trying to use an MPD library in Java in Clojure. Everything has gone well, but these methods that return a java.util.Collection get bad reception in REPL. Let's define
(def mpd (org.bff.javampd.MPD. "localhost" ))
(def pl (.getMPDPlaylist mpd))
(def db (.getMPDDatabase mpd))
And now some methods play ok:
(.getSongList pl) ; returns List<MPDSong>
works well. But for instance every db (MPDDatabase) method return Collection<MPDSong> (according to their API):
(.findAlbum db "Crises") ; returns Collection<MPDSong>
java.lang.ClassCastException (NO_SOURCE_FILE:0)
Doesn't work that well. Why is that, how to fix it?
Stack trace follows:
hello.hello=> (.findAlbum db "Crises")
java.lang.ClassCastException (NO_SOURCE_FILE:0)
hello.hello=> (.printStackTrace *e)
java.lang.ClassCastException (NO_SOURCE_FILE:0)
at clojure.lang.Compiler.eval(Compiler.java:5440)
at clojure.lang.Compiler.eval(Compiler.java:5391)
at clojure.core$eval.invoke(core.clj:2382)
at clojure.main$repl$read_eval_print__5624.invoke(main.clj:183)
at clojure.main$repl$fn__5629.invoke(main.clj:204)
at clojure.main$repl.doInvoke(main.clj:204)
at clojure.lang.RestFn.invoke(RestFn.java:422)
at user$eval13$acc__808__auto____14$fn__16.invoke(NO_SOURCE_FILE:1)
at clojure.lang.AFn.run(AFn.java:24)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at clojure.lang.Reflector.boxArg(Reflector.java:364)
at clojure.lang.Reflector.boxArgs(Reflector.java:397)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:55)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at hello.hello$eval44.invoke(NO_SOURCE_FILE:8)
at clojure.lang.Compiler.eval(Compiler.java:5424)
... 9 more
nil
Looks like the API Documentation is invalid (checking using clojure.contrib.repl-utils):
user> (show MPDDatabase "findAlbum$")
=== public org.bff.javampd.MPDDatabase ===
[ 1] findAlbum : Collection (MPDAlbum)
and you need to a intermediate MPDAlbum object:
user> (.findAlbum db (MPDAlbum. "Crisis"))
#<ArrayList []>