11

Ok so in my environment.py file I am able to log stuff by:

logging.basicConfig(level=logging.DEBUG, filename="example.log")

def before_feature(context, feature):
    logging.info("test logging")

but when I am inside the steps file I cannot perform logging:

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

@given("we have a step")
def step_impl(context):
    logger.debug("Test logging 2")

The logging message inside the step does not show up. I am using the python behave module. Any ideas?

I have tried enabling and disabling logcapture when I run behave but it makes no difference.

mcepl
  • 2,688
  • 1
  • 23
  • 38
nnja
  • 325
  • 3
  • 15
  • Do you mean it is not appearing in the console output or in the example.log file? – lucrib Feb 09 '19 at 20:55
  • Do a full-text search through `*.py` to check who *else* is calling `logging.basicConfig` in your project. I've seen it called in places like `__init__.py` silently affecting anyone who imports a module from the package where such a file is present. – V-R Feb 19 '19 at 17:44

3 Answers3

5

By default, behave tends to capture logs during feature execution, and only display them in cases of failure.

To disable this, you can set log_capture=false in behave.ini

Or, you can use the --no-logcapture command line option

Further Reading : Behave API Reference, Behave LogCapture

pixie999
  • 468
  • 5
  • 11
  • Thank you; saved my day! Maybe it's just me but I don't find the official documentation very helpful. – alwayslearning Aug 06 '20 at 12:16
  • Still `behave` captures logging even with `--no-logcapture`, when using the default formatter. Only when I set `--format=plain --no-logcapture`, I can see the logs. This is terribly annoying. – Janos Oct 06 '21 at 09:25
4

what worked for me:

behave --no-capture --no-capture-stderr --no-logcapture

and add in the environment.py the follwing snipest:

    def after_step(context, step):
        print("")

Why: I disovered that behave does not log the last print statement of a step. So I just added a empty print after each step with the previous snipest.

Hope it helped

zarathoustra
  • 440
  • 3
  • 15
0

Importing logging from environment.py in steps.py solved problem for me.

from features.environment import logging

I am not sure but I guess the problem is that every time you import logging it rewrites your previous configs because disable_existing_loggers is True by default. (Here is the documentation paragraph explaining this)