globality-corp / flake8-logging-format

Flake8 extension to validate (lack of) logging format strings
Apache License 2.0
134 stars 21 forks source link

Detect format string incomplete format #25

Open AlexandreYang opened 4 years ago

AlexandreYang commented 4 years ago

Hi, first, thanks for amazing this linter!

We got some cases of incomplete format issues due to oversight. It could be useful to detect them too.

Examples:

log.debug("my message: %", 'abc')
log.debug("my message:", 'abc')

Can lead to lead to ValueError and TypeError:

--- Logging error ---
Traceback (most recent call last):
  File "XXXX/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 1081, in emit
    msg = self.format(record)
  File "XXXX/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 925, in format
    return fmt.format(record)
  File "XXXX/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 664, in format
    record.message = record.getMessage()
  File "XXXX/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 369, in getMessage
    msg = msg % self.args
ValueError: incomplete format
Call stack:
  File "XXXXX/logging_tester.py", line 119, in <module>
    c.check({})
  File "XXXXX/logging_tester.py", line 116, in check
    self.log.debug("Squid version is unknown: %", 'abc')
Message: 'my message: %'
Arguments: ('abc',)

or

TypeError: not all arguments converted during string formatting