From d4d9cc807283b917d937fec5a451b1192d86c227 Mon Sep 17 00:00:00 2001 From: "T. Joseph Carter" Date: Fri, 7 Jul 2017 02:32:20 -0700 Subject: [PATCH] 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! --- blocksfree/logging.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/blocksfree/logging.py b/blocksfree/logging.py index 406868c..2dd5c18 100644 --- a/blocksfree/logging.py +++ b/blocksfree/logging.py @@ -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')