pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.8k stars 2.62k forks source link

Add more info on set_log_path() #8086

Open FirePing32 opened 3 years ago

FirePing32 commented 3 years ago

Live logs says

You can call set_log_path() to customize the log_file path dynamically. This functionality is considered experimental.

It does not explain how to use set_log_path() as a hook.(Maybe which can be done by adding an example)

Related: #5428, #4707

FirePing32 commented 3 years ago

@nicoddemus https://github.com/pytest-dev/pytest/issues/4707#issuecomment-460415617 Shouldn't this example be added to Docs ? (set_log_filename be replaced with set_log_path)

nicoddemus commented 3 years ago

Hi @prakhargurunani, sorry for the delay!

Indeed those docs are incomplete, we should add an example on how to customize it. set_log_path() is a method of LoggingPlugin, so it can be accessed from hooks or fixtures. We could add an example like this:

def pytest_configure(config):
    log_name = datetime.now().strftime("%Y%m%d-%I%M%S%p.log")
    log_path = config.rootpath.joinpath("logs")
    log_path.mkdir(exists_ok=True)

    logging_plugin = config.pluginmanager.get_plugin("logging-plugin")
    logging_plugin.set_log_path(str(log_path / log_name))

This configures the logging file to always write next to the root path using the current datetime.

FirePing32 commented 3 years ago

@nicoddemus Ok. I will make a PR adding the example in #4707 with your addition.

jay746 commented 3 years ago

I was trying to check set_log_path module. pytest version: 6.2.2 it seems config.pluginmanager.get_plugin("logging-plugin") gives out None def pytest_configure(config): logging_plugin = config.pluginmanager.get_plugin("logging-plugin") logging_plugin.set_log_path(str(dir/ filename))

I get Error as: INTERNALERROR> Traceback (most recent call last): INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/_pytest/main.py", line 265, in wrap_session INTERNALERROR> config._do_configure() INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/_pytest/config/init.py", line 982, in _do_configure INTERNALERROR> self.hook.pytest_configure.call_historic(kwargs=dict(config=self)) INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/hooks.py", line 308, in call_historic INTERNALERROR> res = self._hookexec(self, self.get_hookimpls(), kwargs) INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/manager.py", line 93, in _hookexec INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs) INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/manager.py", line 87, in INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False, INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/callers.py", line 208, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result INTERNALERROR> raise ex[1].with_traceback(ex[2]) INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "/home/jthakur/trees/npu-stack/habanaqa/tests/graph_compiler/workloads/conftest.py", line 180, in pytest_configure INTERNALERROR> logging_plugin.set_log_path(str(os.path.join(pytest.log_dir, "synpase_test.log"))) INTERNALERROR> AttributeError: 'NoneType' object has no attribute 'set_log_path'

jonathan-krpt commented 2 years ago

I was trying to check set_log_path module. pytest version: 6.2.2 it seems config.pluginmanager.get_plugin("logging-plugin") gives out None def pytest_configure(config): logging_plugin = config.pluginmanager.get_plugin("logging-plugin") logging_plugin.set_log_path(str(dir/ filename))

I get Error as: INTERNALERROR> Traceback (most recent call last): INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/_pytest/main.py", line 265, in wrap_session INTERNALERROR> config._do_configure() INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/_pytest/config/init.py", line 982, in _do_configure INTERNALERROR> self.hook.pytest_configure.call_historic(kwargs=dict(config=self)) INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/hooks.py", line 308, in call_historic INTERNALERROR> res = self._hookexec(self, self.get_hookimpls(), kwargs) INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/manager.py", line 93, in _hookexec INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs) INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/manager.py", line 87, in INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False, INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/callers.py", line 208, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result INTERNALERROR> raise ex[1].with_traceback(ex[2]) INTERNALERROR> File "/home/jthakur/vnv_syn_new/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "/home/jthakur/trees/npu-stack/habanaqa/tests/graph_compiler/workloads/conftest.py", line 180, in pytest_configure INTERNALERROR> logging_plugin.set_log_path(str(os.path.join(pytest.log_dir, "synpase_test.log"))) INTERNALERROR> AttributeError: 'NoneType' object has no attribute 'set_log_path'

I see the same. is there a solution for this?

symonk commented 2 years ago

logging plugin is registered in a configure hook marked as trylast if I'm not mistaken which is likely the reason, try mark your hook with @pytest.hookimpl(trylast=True). Let me know if it helps, if not I'll take a look

jonathan-krpt commented 2 years ago

logging plugin is registered in a configure hook marked as trylast if I'm not mistaken which is likely the reason, try mark your hook with @pytest.hookimpl(trylast=True). Let me know if it helps, if not I'll take a look

yeah its fixed mt issue, but didn't solve what I was trying to do (logging colours customisation): https://docs.pytest.org/en/7.1.x/how-to/logging.html

@pytest.hookimpl(trylast=True)
def pytest_configure(config):
    logging_plugin = config.pluginmanager.get_plugin("logging-plugin")

    # Change color on existing log level
    logging_plugin.log_cli_handler.formatter.add_color_level(logging.INFO, "cyan")

    # Add color to a custom log level (a custom log level `SPAM` is already set up)
    logging_plugin.log_cli_handler.formatter.add_color_level(logging.SPAM, "blue")

strange as this isn't documented. unfortunately, it didn't colour my logs in pytest stdout output.