License Public Domain
Lines 298
Keywords
comment (1) logging (1) performance (1) preprocess (1) Python (6) script (1)
Permissions
Viewable by Everyone
Editable by All Siafoo Users

Automatic replacement of logging statements with pass (and vice versa) Atom Feed 0

In Brief This script parses a Python file and comments out logging statements, replacing them with a pass statement (or vice versa). The purpose of commenting out these statements is to improve performance. Even if logging is disabled, arguments to logging method calls must still be evaluated, which can be expensive.... more

This script parses a Python file and comments out logging statements, replacing them with a pass statement (or vice versa). The purpose of commenting out these statements is to improve performance. Even if logging is disabled, arguments to logging method calls must still be evaluated, which can be expensive.

This tool handles most common cases:
  • Log statements may span multiple lines.
  • Custom logging levels may be added (LEVELS, LEVEL_VALUES).
  • Integral logging levels & named logging levels (DEBUG, etc.) are recognized.
  • Logging statements log(), debug(), ..., critical() are all recognized.
  • Statements with unrecognized logging levels will be left as-is.
  • 'logging' is the assumed logging module name (LOGGING_MODULE_NAME).
However, its ability to parse files is limited:
  • It only operates on logging statements in the form logging.log(<level>, ...) and logging.<level>(...).
  • The <level> must either be an integral constant or contain one of the names from the LEVELS constant below.
  • If a logging statement is made, it is assumed that no other statement is made on the same line as logging statement (except for statements made in between the open and close parenthesis of the logging call). For example, a semi-colon and then a second statement on the same line as a logging call will not be handled properly.
  • Logging methods must be called through SOME module, e.g., logging.log(), not just log().
  • For simplicity, undoing the commenting process relies on a comment left by the program on the pass statements it adds when commenting out logging statements. (So don't change the comment it outputs by the pass statement).
To run this command on all of the Python files in a particular folder and its sub-folders at once, try this (replace '/path/to' as appropriate):
find . -name '*.py' | xargs -i{} /path/to/logging_statement_modifier.py {}

You can learn more about the motivation for the script at this link: http://dound.com/2010/02/python-logging-performance/