Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Captured log to outfile #979

Open
janoskut opened this issue Dec 7, 2021 · 5 comments
Open

Captured log to outfile #979

janoskut opened this issue Dec 7, 2021 · 5 comments
Labels

Comments

@janoskut
Copy link

janoskut commented Dec 7, 2021

There doesn't seem to be a way to get the captured logs into a log file, using the --outfile option.

I can get it to the console using --no-logcapture and --format=plain, but not to the output file using any formatter.

How would I go ahead if I wanted:

  • --format=pretty in the console (no log output on success)
  • --format=plain with --logging-level=DEBUG to an --outfile output file (on success and failure, no difference)

?

Related to: #412

@jenisys
Copy link
Member

jenisys commented Dec 7, 2021

The --outfile option is used to redirect the output of a formatter to a file.
This means:

  • You have a primary formatter. It uses either console/stdout or an output-file.
  • You may have many (0..N) secondary formatters. Each formatter will use its own output-file.
    Secondary formatters are often defined in the "behave.ini" config-file.

OTHERWISE: Logging is for diagnostics.
To redirect the logging output to a file, setup the logging subsystem accordingly.
This setup is best done in the before_all() hook (otherwise it is too late for log-capture, etc.).

SEE ALSO:

@janoskut
Copy link
Author

janoskut commented Dec 8, 2021

Thanks for your explanations @jenisys . I think that is exactly as far as I got.

So how would I go about putting formatter-output and logging output to the same file? I need the full output of behave --format plain --logging-level=DEBUG --no-logcapture in a logfile, but I don't want to have it printed to stdout. So also redirecting stdout to a file is not an option.

I tried: using the same file for -o and the logging file handler, but behave formatters overwrite the file. Even with an own formatter which appends to a file ('a' instead of 'w' file mode), the logging is written first into the file, and the formatter output is added afterwards. I guess this could be somehow fixed, but writing into the same file from different directions doesn't seem right to me.

Do I need to write a formatter which makes use of the captured logs at each step? How would I do that?

@jenisys
Copy link
Member

jenisys commented Dec 8, 2021

Why would you want to do that?

  • The result would garbled text.
  • The Scenario/Step output with the formatter would be garbled with log-record statements

If you really want to that:

  • Formatter output should use stdout
  • Logging output should use stdout/console
  • You redirect the output to a file (and stderr probably too)
# -- ASSUMPTION: Logging handler(s) write output to stdout
$ behave -f plain features > combined_output.txt

@janoskut
Copy link
Author

janoskut commented Dec 9, 2021

Why would you want to do that?

Why would I want to do what exactly? For me it's a pretty obvious use case: I want a logfile which has behave's explanation of what steps are executed, and in between the log output of the thing under test. Just as I have when I use plain --logging-level=DEBUG --no-logcapture. Because it is too verbose, I want this detail in a logfile, not in the console. On the console, I'm happy with the "pretty" formatter.

I guess I could use the before_step() etc. hooks to keep track of what is executed in the log. It doesn't look like the formatter API is capable of knowing when a step is started, or would that be a way to write a formatter which does the logging?

@jenisys
Copy link
Member

jenisys commented May 27, 2024

Mmh, how is step execution then tracked and output for it generated by a Formatter, especially in the PrettyFormatter ?!?
TLDR, the Formatter.step() is called when the step execution starts.

DETAILS:

  • A trivial Formatter only reports the step after it is executed, because it needs to report the step-result, too.
  • The PrettyFormatter reports the start of the step execution and overwrites this/these line(s) after the step-result is known.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants