Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change log level in unittest

Tags:

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:

  • be able to specify the logging level for all loggers, from the command line (when running the tests) or from the test module itself
  • avoid unittest messing around with the application logging level: when running the tests I want to have the same logging output (same levels) as when running the application

How can I achieve this?

like image 272
blueFast Avatar asked May 19 '16 06:05

blueFast


People also ask

How do you change the log level?

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)

How do you set the log level to debug in junit tests?

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 .

How do I change the log level without restarting?

There are 3 ways to do it. Spring Actuator - leveraging /loggers endpoint. Spring Boot Admin. LogBack auto-scan.

How do you change the log level in slf4j?

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.


1 Answers

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).

like image 195
Vinay Sajip Avatar answered Nov 02 '22 01:11

Vinay Sajip