108

I am using prospector to examine my code. Pylint returned a logging-not-lazy warning about my debug message.

Line: 31
  pylint: logging-not-lazy / Specify string format arguments as logging function parameters (col 16)   Line: 42
  pylint: logging-not-lazy / Specify string format arguments as logging function parameters (col 12)

My code is:

logging.debug("detect mimetypes faild because %s" % e )

How do I fix logging-not-lazy in pylint?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Valeriy Solovyov
  • 5,384
  • 3
  • 27
  • 45

1 Answers1

165

This means, that you should rewrite your code as:

logging.debug("detect mimetypes faild because %s", e)

According to https://docs.python.org/2/library/logging.html

Logger.debug(msg, *args, **kwargs)

... Logs a message with level DEBUG on this logger. The msg is the message format string, and the args are the arguments which are merged into msg using the string formatting operator. (Note that this means that you can use keywords in the format string, together with a single dictionary argument.) ...

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Zada Zorg
  • 2,778
  • 1
  • 21
  • 25
  • 6
    Is there a reason why this format is preferred for debug logs? – Stevoisiak Mar 20 '18 at 20:05
  • 45
    It's preferred because it's lazy. The logging library doesn't format the string at all if the level is not set to `DEBUG`. That means it doesn't have to do extra work unless the user really wants to see those log messages. – kichik Mar 20 '18 at 20:17