Thanks to visit codestin.com
Credit goes to github.com

Skip to content

cli: add --logformat and --logdateformat#6138

Merged
bastimeyer merged 3 commits into
streamlink:masterfrom
bastimeyer:cli/logformat
Aug 26, 2024
Merged

cli: add --logformat and --logdateformat#6138
bastimeyer merged 3 commits into
streamlink:masterfrom
bastimeyer:cli/logformat

Conversation

@bastimeyer

Copy link
Copy Markdown
Member

This allows changing the default log format and log date format, which can be useful in some cases, or for specific applications using Streamlink via its CLI which parse the log output.

There's currently no validation of the format, so if the user inputs invalid variables, the error messages with tracelog will be pretty ugly...

$ streamlink -l debug | grep Streamlink
[cli][debug] Streamlink: 6.9.0+10.g9d323e3e
$ streamlink -l all | grep Streamlink
[13:31:07.558270][cli][debug] Streamlink: 6.9.0+10.g9d323e3e
$ streamlink -l debug --logformat '[{asctime}][{filename}][{name}][{levelname}] {message}' --logdateformat '%Y-%m-%dT%H:%M:%S.%f' | grep Streamlink
[2024-08-25T13:33:53.689482][main.py][cli][debug] Streamlink: 6.9.0+10.g9d323e3e

@bastimeyer bastimeyer added the CLI label Aug 25, 2024

@gravyboat gravyboat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with the output being ugly in the event the format is wrong. This is quite niche so I don't see any reason to go beyond what you have in the argument help. If someone can't figure that out they shouldn't be messing with this setting any way.

@bastimeyer

Copy link
Copy Markdown
Member Author

Without making changes to the CLI's main module and how the logger and console output are initialized, logging format errors are uncaught ValueErrors raised by the logging module of the stdlib. That's fine though and much better than printing an error stack for each attempted log call.

$ streamlink --logformat 'foo'
Traceback (most recent call last):
  File "/home/basti/venv/streamlink-312/bin/streamlink", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 980, in main
    setup(parser)
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 893, in setup
    setup_logger_and_console(
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 864, in setup_logger_and_console
    streamhandler = logger.basicConfig(
                    ^^^^^^^^^^^^^^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink/logger.py", line 211, in basicConfig
    formatter = StringFormatter(
                ^^^^^^^^^^^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink/logger.py", line 98, in __init__
    super().__init__(*args, **kwargs)
  File "/usr/lib/python3.12/logging/__init__.py", line 614, in __init__
    self._style.validate()
  File "/usr/lib/python3.12/logging/__init__.py", line 500, in validate
    raise ValueError('invalid format: no fields')
ValueError: invalid format: no fields
$ streamlink --logformat '{foo}'
Traceback (most recent call last):
  File "/usr/lib/python3.12/logging/__init__.py", line 464, in format
    return self._format(record)
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/logging/__init__.py", line 482, in _format
    return self._fmt.format(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/basti/venv/streamlink-312/bin/streamlink", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 980, in main
    setup(parser)
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 893, in setup
    setup_logger_and_console(
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 864, in setup_logger_and_console
    streamhandler = logger.basicConfig(
                    ^^^^^^^^^^^^^^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink/logger.py", line 211, in basicConfig
    formatter = StringFormatter(
                ^^^^^^^^^^^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink/logger.py", line 104, in __init__
    super().format(rec)
  File "/usr/lib/python3.12/logging/__init__.py", line 706, in format
    s = self.formatMessage(record)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/logging/__init__.py", line 675, in formatMessage
    return self._style.format(record)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/logging/__init__.py", line 466, in format
    raise ValueError('Formatting field not found in record: %s' % e)
ValueError: Formatting field not found in record: 'foo'

@bastimeyer bastimeyer merged commit 18e3039 into streamlink:master Aug 26, 2024
@bastimeyer bastimeyer deleted the cli/logformat branch August 26, 2024 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants