Use textwrap to dedent multi-line strings

This is kind of an expensive thing to do unconditionally, but it lets us make
multi-line strings fit into the code with less ugliness.  Basically, if you're
four levels in, you can do something like this:

                log.warn("""\
                There was a problem.
                It was probably wasn't fatal because this
                is only a warning, but it is enough to have
                a multiline string.
                """)

This will print without the indentation.  It's not quite as clean as how
docutils handles docstrings (allowing the first line to be unindented, hence
the line-continuation), but it's still an improvement.  If you can improve upon
this, please feel free to PR it!
This commit is contained in:
T. Joseph Carter 2017-07-07 02:32:20 -07:00
parent 82d851e39a
commit d4d9cc8072
1 changed files with 3 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import sys
import logging
import textwrap
### LOGGING
# *sigh* No clean/simple way to use str.format() type log strings without
@ -20,11 +21,12 @@ class StyleAdapter(logging.LoggerAdapter):
def log(self, level, msg, *args, **kwargs):
if self.isEnabledFor(level):
msg, kwargs = self.process(msg, kwargs)
msg, kwargs = self.process(textwrap.dedent(msg), kwargs)
self.logger._log(level, Message(str(msg), args), (), **kwargs)
log = StyleAdapter(logging.getLogger(__name__))
# Set up our logging facility
_handler = logging.StreamHandler(sys.stdout)
_formatter = logging.Formatter('%(levelname)s: %(message)s')