question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Capturing logs with log_capture in behave

See original GitHub issue

Problem

I run my test with behave (https://behave.readthedocs.io/en/stable/api.html?highlight=log_capture#logging-capture) and after changing to the loguru my tests fail.

I have read this post Issue 59 (regarding basically the same but with pytest), and I understand that loguru doesn’t use standardlib logging which behave log capture probably utilizes, and the way to solve the problem is to propagte the logs to the logging lib

however, I don’t understand how to replicate the fix.

I tried using the fixture in an analogous way but it didn’t seem to work:

Example of what I tried

environment.py

from api.features.fixtures import log_capture_fix


def before_all(context):
    use_fixture(log_capture_fix, context)
    context.config.setup_logging()

fixtures.py

import logging
from behave import fixture
from loguru import logger


@fixture
def log_capture_fix(context):
    class PropogateHandler(logging.Handler):
        def emit(self, record):
            logging.getLogger(record.name).handle(record)

    handler_id = logger.add(PropogateHandler(), format="{message} {extra}")
    yield context.log_capture
    logger.remove(handler_id)

Could you help me configure this properly, or point me in the right directions?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Tsubaneecommented, Feb 24, 2021

Yeah! Right that might be it 🚀 In hindsight it seems kinda obvious 🤦 . Anyway, thank you very much for helping out Delgan. I will fix it tomorrow to run in correct order during tests, and update here how it went (though I wouldn’t expect further problems now 😄)

UPDATE: Yep. Running the init() at the beggining fo the fixture solved the problem. Once again, thank you very much Delgan 😄

0reactions
Delgancommented, Mar 16, 2021

I’m closing this issue since it’s resolved, then. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging not captured on behave steps - Stack Overflow
I am using the python behave module. Any ideas? I have tried enabling and disabling logcapture when I run behave but it makes...
Read more >
Command-Line Arguments - Behave
Capture logging. All logging during a step will be captured and displayed in the event of a failure. This is the default behaviour....
Read more >
Capturing logs with log_capture in behave - Bountysource
Problem. I run my test with behave (https://behave.readthedocs.io/en/stable/api.html?highlight=log_capture#logging-capture) and after ...
Read more >
behave Documentation - Read the Docs
This switch is used to override a configuration file setting. --no-logcapture. Don't capture logging. Logging configuration will be left intact.
Read more >
Behave - Command Line - Tutorialspoint
This is an in-built characteristic and can overrule a configuration file setting. --no-logcapture. Exclude log from capturing. --logcapture. Include log ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found