Skip to content

Add more info on set_log_path() #8086

Open

Description

@FirePing32

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

Activity

  1. FirePing32 commented on Nov 28, 2020

    @FirePing32
    ContributorAuthor

    @nicoddemus #4707 (comment)
    Shouldn't this example be added to Docs ? (set_log_filename be replaced with set_log_path)

  2. added
    plugin: logging
    type: docs
    on Nov 29, 2020
  3. nicoddemus commented on Dec 12, 2020

    @nicoddemus
    Member

    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.

  4. FirePing32 commented on Dec 12, 2020

    @FirePing32
    ContributorAuthor

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

  5. jay746 commented on Feb 2, 2021

    @jay746

    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'

  6. jonathan-krpt commented on May 4, 2022

    @jonathan-krpt

    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?

  7. symonk commented on May 4, 2022

    @symonk
    Member

    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

  8. jonathan-krpt commented on May 4, 2022

    @jonathan-krpt

    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.

  9. added a commit that references this issue on Sep 19, 2026
    dd76b0a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: loggingtype: docs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions