I have the impression (but do not find the documentation for it) that unittest sets the logging level to WARNING
for all loggers. I would like to:
How can I achieve this?
To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)
properties goes in the directory src/main/resources . My test version goes in src/test/resources . The Eclipse build path (classpath) is set up to search src/test/resources before src/main/resources , so your unit tests use the test file. The JAR (or WAR) build instructions use the files from src/main/resources .
There are 3 ways to do it. Spring Actuator - leveraging /loggers endpoint. Spring Boot Admin. LogBack auto-scan.
When using log4j, the Logger. log(Priority p, Object message) method is available and can be used to log a message at a log level determined at runtime. We're using this fact and this tip to redirect stderr to a logger at a specific log level. slf4j doesn't have a generic log() method that I can find.
I don't believe unittest
itself does anything to logging, unless you use a _CapturingHandler
class which it defines. This simple program demonstrates:
import logging import unittest logger = logging.getLogger(__name__) class MyTestCase(unittest.TestCase): def test_something(self): logger.debug('logged from test_something') if __name__ == '__main__': # DEBUG for demonstration purposes, but you could set the level from # cmdline args to whatever you like logging.basicConfig(level=logging.DEBUG, format='%(name)s %(levelname)s %(message)s') unittest.main()
When run, it prints
__main__ DEBUG logged from test_something . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
showing that it is logging events at DEBUG
level, as expected. So the problem is likely to be related to something else, e.g. the code under test, or some other test runner which changes the logging configuration or redirects sys.stdout
and sys.stderr
. You will probably need to provide more information about your test environment, or better yet a minimal program that demonstrates the problem (as my example above shows that unittest
by itself doesn't cause the problem you're describing).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With