Skip to content

Conversation

@bastimeyer
Copy link
Member

@bastimeyer bastimeyer commented May 25, 2021

  1. tests: refactor TestCLIMainLogging
    • always exit streamlink_cli.main.main at the same function call, namely
      log_current_arguments
    • patch out dummy functions in context manager
    • slice debug log mock calls due to common test exit function
    • properly reset mocks
    • rename streamlink_cli.main.check_root to log_root_warning
    • add test for streamlink_cli.main.log_root_warning
  2. cli: implement --logfile
    • refactor streamlink_cli.main.{setup_logging,setup_console}
      and split into setup_logger_and_console and setup_signals
    • add LOG_DIR to streamlink_cli.constants
    • pass filename to logger.basicConfig
    • re-use write stream of logger in ConsoleOutput
    • fix escaped chars for percent-formatted argparse help strings in docs
    • add tests

Resolves #2485

Copy link
Member Author

@bastimeyer bastimeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, the tests are not as thorough and clean as I'd like them to be, because the entire CLI is a total mess.

The download progress stuff however is working correctly and the logfile doesn't affect it, which means it still gets written to stderr. Same as prompts for user input.

Copy link
Member

@gravyboat gravyboat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@bastimeyer
Copy link
Member Author

Would it make sense adding a prompt when trying to write to an existing file, similar to --output? It's currenly always appending to the logfile. That could also be changed, either with a prefix or secondary parameter.

@gravyboat
Copy link
Member

Hmm, I don't really see the benefit of a prompt when writing to an existing file. I would imagine most people writing out log files are going to be comfortable with log rotate or understand the basics of what they are doing.

@Billy2011
Copy link
Contributor

@bastimeyer,
why not use the RotatingFileHandler of logging.handlers?

@bastimeyer
Copy link
Member Author

The reason why I asked about a prompt and whether to append or not is that the log output is not that useful if it gets appended to an existing file, because there's no real "preamble" that would make it clear that a new Streamlink instance is writing to the log file. Maybe it'd make sense always logging the used arguments (log_current_arguments) when the logfile argument is set.

RotatingFileHandler

This doesn't solve the issue I just mentioned. It's meant for proper log rotation and requires a maxBytes and backupCount value. And then there are also other handlers which might be preferred by some user instead. I'm not sure this fits into the scope of Streamlink's CLI.

My idea of the logfile argument is being able to write the log output of a single Streamlink instance into a separate file, not having a continuous/shared logfile of multiple instances, because it's not really useful.

This also raises the question whether a default logfile path could be used when - is set as the logfile argument value, eg. ${XDG_STATE_HOME:${HOME}/.local/state}/streamlink/logs/$(date --iso-8601=seconds).txt.

@bastimeyer bastimeyer marked this pull request as draft May 28, 2021 10:56
@Billy2011
Copy link
Contributor

This doesn't solve the issue I just mentioned.

That wasn't meant to be either.

It's meant for proper log rotation and requires a maxBytes and backupCount value.

With default values of 0 you get the same behavior as with FileHandler.

- always exit streamlink_cli.main.main at the same function call, namely
  log_current_arguments
- patch out dummy functions in context manager
- slice debug log mock calls due to common test exit function
- properly reset mocks
- rename streamlink_cli.main.check_root to log_root_warning
- add test for streamlink_cli.main.log_root_warning
@bastimeyer bastimeyer force-pushed the cli/logfile branch 2 times, most recently from 506df4b to 8a7357a Compare May 28, 2021 21:58
@bastimeyer bastimeyer marked this pull request as ready for review May 28, 2021 22:05
- refactor streamlink_cli.main.{setup_logging,setup_console}
  and split into setup_logger_and_console and setup_signals
- add LOG_DIR to streamlink_cli.constants
- pass filename to logger.basicConfig
- re-use write stream of logger in ConsoleOutput
- fix escaped chars for percent-formatted argparse help strings in docs
- add tests
@gravyboat
Copy link
Member

@bastimeyer Are we good here or are you still working?

@bastimeyer
Copy link
Member Author

Yes, sorry about the confusion. After pushing the latest changes I updated the text of the OP, but didn't add a new comment. Unless someone has any concerns, this should be good. I was waiting for another review, as I've changed a ton since your review. The tests are now done properly and cover both POSIX and Windows paths.

To sum up the changes again:

  • adds the --logfile parameter
  • shares the same write stream between the logger and consoleoutput
  • argument value can be a relative or an absolute path
  • argument value can also be -, which makes it choose a default directory and file name
    • on Windows: %TEMP%\streamlink\logs\2021-05-31_19-57-00.log
    • on macOS: ${HOME}/Library/logs/streamlink/2021-05-31_19-57-00.log
    • on Linux/BSD: ${XDG_STATE_HOME:-${HOME}/.local/state}/streamlink/logs/2021-05-31_19-57-00.log
      (XDG_STATE_HOME is part of the XDG base directory standard 0.8 from 2021-05-08 and logs should go there now)
  • tries to create parent directories
  • always appends log data to the chosen log file
  • user prompts and download progress always get written to stderr and not the logfile
  • refactors the previous CLI-main logging tests and adds test for everything new (except the logdir constants)
  • fixes escaped double-percent chars in the rendered docs

What could have been done differently:

  • logrotation: unnecessary and if desired can be implemented later
  • path templates: the - argument value already results in a dynamic path based on the current time and relative/absolute paths can be made dynamic with shell substitution

@gravyboat
Copy link
Member

@bastimeyer I looked over the changes after you forced pushed 2 days ago and everything looks even better than it originally did. I'm going to merge this in, thanks for the improvements.

@gravyboat gravyboat merged commit 1ed85fc into streamlink:master May 31, 2021
@bastimeyer bastimeyer deleted the cli/logfile branch May 31, 2021 19:45
Billy2011 added a commit to Billy2011/streamlink-27 that referenced this pull request Jun 2, 2021
- always exit streamlink_cli.main.main at the same function call, namely
  log_current_arguments
- patch out dummy functions in context manager
- slice debug log mock calls due to common test exit function
- properly reset mocks
- rename streamlink_cli.main.check_root to log_root_warning
- add test for streamlink_cli.main.log_root_warning
Billy2011 added a commit to Billy2011/streamlink-27 that referenced this pull request Jun 9, 2021
based on streamlink#3753
cannot implement because many issues with py2, py3, no py2 pathlib

no tests applied yet
Billy2011 added a commit to Billy2011/streamlink-27 that referenced this pull request Jun 9, 2021
based on streamlink#3753

the implementation don't use pathlib and therefore not all tests applied
Billy2011 added a commit to Billy2011/streamlink-27 that referenced this pull request Jun 13, 2021
based on streamlink#3753
- os.path is used instead of pathlib
- only basic logfile tests w/o write_file_and_assert()
Billy2011 added a commit to Billy2011/streamlink-27 that referenced this pull request Jun 13, 2021
based on streamlink#3753
- os.path is used instead of pathlib
- only basic logfile tests w/o write_file_and_assert()
Billy2011 added a commit to Billy2011/streamlink-27 that referenced this pull request Sep 30, 2021
based on streamlink#3753
cannot implement because many issues with py2, py3, no py2 pathlib

no tests applied yet
snorkelopstesting1-a11y pushed a commit to snorkel-marlin-repos/streamlink_streamlink_pr_3753_7069badb-3d19-4f59-8ccd-accbff0fe004 that referenced this pull request Oct 22, 2025
Original PR #3753 by bastimeyer
Original: streamlink/streamlink#3753
snorkelopstesting1-a11y added a commit to snorkel-marlin-repos/streamlink_streamlink_pr_3753_7069badb-3d19-4f59-8ccd-accbff0fe004 that referenced this pull request Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Save output log to a file, while still displaying progress indicators

3 participants