summaryrefslogtreecommitdiffstats
path: root/pyload/lib/colorlog
diff options
context:
space:
mode:
Diffstat (limited to 'pyload/lib/colorlog')
-rw-r--r--pyload/lib/colorlog/__init__.py8
-rw-r--r--pyload/lib/colorlog/colorlog.py76
-rw-r--r--pyload/lib/colorlog/escape_codes.py37
3 files changed, 121 insertions, 0 deletions
diff --git a/pyload/lib/colorlog/__init__.py b/pyload/lib/colorlog/__init__.py
new file mode 100644
index 000000000..abf5532fb
--- /dev/null
+++ b/pyload/lib/colorlog/__init__.py
@@ -0,0 +1,8 @@
+"""A logging formatter for colored output"""
+
+from __future__ import absolute_import
+
+__all__ = ['ColoredFormatter', 'default_log_colors', 'escape_codes']
+
+from colorlog.colorlog import (
+ ColoredFormatter, default_log_colors, escape_codes)
diff --git a/pyload/lib/colorlog/colorlog.py b/pyload/lib/colorlog/colorlog.py
new file mode 100644
index 000000000..5491676b8
--- /dev/null
+++ b/pyload/lib/colorlog/colorlog.py
@@ -0,0 +1,76 @@
+"""The ColoredFormatter class"""
+
+from __future__ import absolute_import
+
+import sys
+import logging
+
+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',
+}
+
+
+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, datefmt=None,
+ log_colors=default_log_colors, reset=True, style='%'):
+ """
+ :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.
+
+ The ``format``, ``datefmt`` and ``style`` args are passed on to the
+ Formatter constructor.
+ """
+ if sys.version_info > (3, 2):
+ super(ColoredFormatter, self).__init__(
+ format, datefmt, style=style)
+ elif sys.version_info > (2, 7):
+ super(ColoredFormatter, self).__init__(format, datefmt)
+ else:
+ logging.Formatter.__init__(self, format, datefmt)
+ self.log_colors = log_colors
+ self.reset = reset
+
+ def format(self, record):
+ # Add the color codes to the 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
diff --git a/pyload/lib/colorlog/escape_codes.py b/pyload/lib/colorlog/escape_codes.py
new file mode 100644
index 000000000..8d057e9e4
--- /dev/null
+++ b/pyload/lib/colorlog/escape_codes.py
@@ -0,0 +1,37 @@
+"""
+Generates a dictionary of ANSI escape codes
+
+http://en.wikipedia.org/wiki/ANSI_escape_code
+"""
+
+__all__ = ['escape_codes']
+
+# Returns escape codes from format codes
+esc = lambda *x: '\033[' + ';'.join(x) + 'm'
+
+# The initial list of escape codes
+escape_codes = {
+ 'reset': esc('39', '49', '0'),
+ 'bold': esc('01'),
+}
+
+# The color names
+colors = [
+ 'black',
+ 'red',
+ 'green',
+ 'yellow',
+ 'blue',
+ 'purple',
+ 'cyan',
+ 'white'
+]
+
+# Create foreground and background colors...
+for lcode, lname in [('3', ''), ('4', 'bg_')]:
+ # ...with the list of colors...
+ for code, name in enumerate(colors):
+ code = str(code)
+ # ...and both normal and bold versions of each color
+ escape_codes[lname + name] = esc(lcode + code)
+ escape_codes[lname + "bold_" + name] = esc(lcode + code, "01")