"""The ColoredFormatter class."""

from __future__ import absolute_import

import logging
import copy
import sys

from colorlog.escape_codes import escape_codes

__all__ = ('escape_codes', 'default_log_colors', 'ColoredFormatter')

# The default colors to use for the debug levels
default_log_colors = {
    'DEBUG': 'white',
    'INFO': 'green',
    'WARNING': 'yellow',
    'ERROR': 'red',
    'CRITICAL': 'bold_red',
}

# The default format to use for each style
default_formats = {
    '%': "%(log_color)s%(levelname)s:%(name)s:%(message)s",
    '{': '{log_color}{levelname}:{name}:{message}',
    '$': '${log_color}${levelname}:${name}:${message}'
}


class ColoredFormatter(logging.Formatter):
    """
    A formatter that allows colors to be placed in the format string.

    Intended to help in creating more readable logging output.
    """

    def __init__(self, format=None, datefmt=None,
                 log_colors=None, reset=True, style='%'):
        """
        Set the format and colors the ColoredFormatter will use.

        The ``format``, ``datefmt`` and ``style`` args are passed on to the
        ``logging.Formatter`` constructor.

        :Parameters:
        - format (str): The format string to use
        - datefmt (str): A format string for the date
        - log_colors (dict):
            A mapping of log level names to color names
        - reset (bool):
            Implictly append a color reset to all records unless False
        - style ('%' or '{' or '$'):
            The format style to use. No meaning prior to Python 3.2.
        """
        if format is None:
            if sys.version_info > (3, 2):
                format = default_formats[style]
            else:
                format = default_formats['%']

        if sys.version_info > (3, 2):
            super(ColoredFormatter, self).__init__(format, datefmt, style)
        elif sys.version_info > (2, 7):
            super(ColoredFormatter, self).__init__(format, datefmt)
        else:
            logging.Formatter.__init__(self, format, datefmt)

        self.log_colors = (
            default_log_colors if log_colors is None else log_colors)
        self.reset = reset

    def format(self, record):
        """Format a message from a record object."""
        record = copy.copy(record)
        record.__dict__.update(escape_codes)

        # If we recognise the level name,
        # add the levels color as `log_color`
        if record.levelname in self.log_colors:
            color = self.log_colors[record.levelname]
            record.log_color = escape_codes[color]
        else:
            record.log_color = ""

        # Format the message
        if sys.version_info > (2, 7):
            message = super(ColoredFormatter, self).format(record)
        else:
            message = logging.Formatter.format(self, record)

        # Add a reset code to the end of the message
        # (if it wasn't explicitly added in format str)
        if self.reset and not message.endswith(escape_codes['reset']):
            message += escape_codes['reset']

        return message

@@ -203,16 +203,46 @@
 colors',
+ 'default_log_message_colors',
  'Colore
@@ -446,16 +446,224 @@
 ed',%0A%7D%0A%0A
+%0A# The default colors to use in the message for the debug levels%0Adefault_log_message_colors = %7B%0A    'DEBUG': 'blue',%0A    'INFO': 'blue',%0A    'WARNING': 'blue',%0A    'ERROR': 'blue',%0A    'CRITICAL': 'blue'%0A%7D%0A%0A%0A
 # The de
@@ -1187,16 +1187,58 @@
 tyle='%25'
+,%0A                 log_message_colors=None
 ):%0A     
@@ -1840,16 +1840,127 @@
 on 3.2.%0A
+        - log_message_colors (dict):%0A            A mapping of log level names to color names used for the body%0A
         
@@ -2539,16 +2539,188 @@
 g_colors
+%0A        )%0A        self.log_message_colors = (%0A            default_log_message_colors%0A            if log_message_colors is None%0A            else log_message_colors%0A        
 )%0A      
@@ -3095,16 +3095,100 @@
 elname%5D%0A
+            split_color = color.split(%22 %22)%0A            if len(split_color) %3C 2:%0A    
         
@@ -3230,16 +3230,20 @@
 %5Bcolor%5D%0A
+    
         
@@ -3256,34 +3256,742 @@
             
-record.log
+    c0, c1 = split_color%5B0%5D, split_color%5B1%5D%0A                record.log_color = escape_codes%5Bc0%5D + escape_codes%5Bc1%5D%0A        else:%0A            record.log_color = %22%22%0A%0A        # If we recognise the level name,%0A        # add the levels color as %60log_message_color%60%0A        if record.levelname in self.log_message_colors:%0A            color = self.log_message_colors%5Brecord.levelname%5D%0A            split_color = color.split(%22 %22)%0A            if len(split_color) %3C 2:%0A                record.log_message_color = escape_codes%5Bcolor%5D%0A            else:%0A                c0, c1 = split_color%5B0%5D, split_color%5B1%5D%0A                record.log_message_color = escape_codes%5Bc0%5D + escape_codes%5Bc1%5D%0A        else:%0A            record.log_message
 _color = %22%22%0A

Support bg+fg for log_color/log_message_color