lambdaisland / kaocha

Full featured next gen Clojure test runner
https://cljdoc.org/d/lambdaisland/kaocha/1.0.861/doc/1-introduction
Eclipse Public License 1.0
792 stars 81 forks source link

capture-output doesn't respect logback.xml config #419

Closed AlexChalk closed 1 year ago

AlexChalk commented 1 year ago

I use ring-logger and tools.logging, and I have this configuration line in my logback.xml: <logger name="ring.logger" level="error" />.

This configuration is respected when I run my code in the repl/in production, but when I run kaocha, all ring-logger logs are output for any test that fails. This remains true if I set level="off".

Any guidance would be much appreciated. I don't mind using something other than logback.xml for kaocha, any solution that lets me set log levels would work. Thanks!

AlexChalk commented 1 year ago

I've played with this some more.

I had this in my tests.edn:

                  :kaocha/source-paths ["src" "resources"],
                  :kaocha/test-paths ["env/test" "env/test/resources" "test"],

I'd guess this isn't recommended as the env directories don't actually contain tests, but the problem is kaocha doesn't seem to load them at all if I put them under source-paths as I get missing namespace errors.

As an alternative I tried this in my tests.edn:

                  :kaocha/source-paths []
                  :kaocha.testable/skip-add-classpath? true
                  :kaocha/test-paths ["test"]

this in deps.edn:

  :kaocha-paths {:extra-paths ["env/test" "env/test/resources"]}

and this in bin/kaocha:

clojure -M:test:kaocha-paths -m kaocha.runner "$@"

This is a workable solution when calling kaocha from the command line, but it doesn't work from within the repl because I've already loaded conflicting namespaces.

I opened an issue about this scenario a few months ago https://github.com/lambdaisland/kaocha/issues/330, and the solution suggested there does work for test namespaces, but not for my logback configuration. I assume that's because the logback config is read once on repl startup and not re-read after that. To reiterate, I'm looking for a way to configure a test-specific log level (including within the repl). If it doesn't exist I'm happy to try and help add it, but would probably require guidance.

AlexChalk commented 1 year ago

I experimented some more and found a solution I like :dark_sunglasses: :sweat_smile:

For ./bin/kaocha I'm using the solution from my previous comment. For running in the repl, I found a way of passing a new logback config programatically:

(defn reconfigure-logback [path]
  (let [url (clojure.java.io/resource path)
        logger-context (org.slf4j.LoggerFactory/getILoggerFactory)
        configurator (ch.qos.logback.classic.joran.JoranConfigurator.)]
    (.reset logger-context)
    (.setContext configurator logger-context)
    (.doConfigure configurator url)))

(reconfigure-logback "logback.xml")

Hope this helps someone!