@@ -77,6 +77,36 @@ def format(self, record):
7777 return super (ColoredLevelFormatter , self ).format (record )
7878
7979
80+ if not six .PY2 :
81+ # Formatter classes don't support format styles in PY2
82+
83+ class PercentStyleMultiline (logging .PercentStyle ):
84+ """A logging style with special support for multiline messages.
85+
86+ If the message of a record consists of multiple lines, this style
87+ formats the message as if each line were logged separately.
88+ """
89+
90+ @staticmethod
91+ def _update_message (record_dict , message ):
92+ tmp = record_dict .copy ()
93+ tmp ["message" ] = message
94+ return tmp
95+
96+ def format (self , record ):
97+ if "\n " in record .message :
98+ lines = record .message .splitlines ()
99+ formatted = self ._fmt % self ._update_message (record .__dict__ , lines [0 ])
100+ # TODO optimize this by introducing an option that tells the
101+ # logging framework that the indentation doesn't
102+ # change. This allows to compute the indentation only once.
103+ indentation = _remove_ansi_escape_sequences (formatted ).find (lines [0 ])
104+ lines [0 ] = formatted
105+ return ("\n " + " " * indentation ).join (lines )
106+ else :
107+ return self ._fmt % record .__dict__
108+
109+
80110def get_option_ini (config , * names ):
81111 for name in names :
82112 ret = config .getoption (name ) # 'default' arg won't work as expected
@@ -444,6 +474,9 @@ def _create_formatter(self, log_format, log_date_format):
444474 )
445475 else :
446476 formatter = logging .Formatter (log_format , log_date_format )
477+
478+ if not six .PY2 :
479+ formatter ._style = PercentStyleMultiline (formatter ._style ._fmt )
447480 return formatter
448481
449482 def _setup_cli_logging (self ):
0 commit comments