From 08839e144cb77f7f6e7193867ab66c59775b0b8b Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Thu, 14 Nov 2013 02:11:57 +0100 Subject: Coloured logging --- pyload/Core.py | 14 ++++++- pyload/utils/colorlog/__init__.py | 8 ++++ pyload/utils/colorlog/colorlog.py | 76 +++++++++++++++++++++++++++++++++++ pyload/utils/colorlog/escape_codes.py | 37 +++++++++++++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 pyload/utils/colorlog/__init__.py create mode 100644 pyload/utils/colorlog/colorlog.py create mode 100644 pyload/utils/colorlog/escape_codes.py diff --git a/pyload/Core.py b/pyload/Core.py index 324494500..96421d862 100644 --- a/pyload/Core.py +++ b/pyload/Core.py @@ -59,6 +59,8 @@ from utils.fs import free_space, exists, makedirs, join, chmod from codecs import getwriter +from utils.colorlog import ColoredFormatter + # test runner overwrites sys.stdout if hasattr(sys.stdout, "encoding"): enc = get_console_encoding(sys.stdout.encoding) else: enc = "utf8" @@ -517,7 +519,17 @@ class Core(object): if not tfrm: tfrm = "%Y-%m-%d %H:%M:%S" - frm = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s", tfrm) + frm = ColoredFormatter( + format="%(asctime)s %(log_color)s%(bold)s%(black)s %(levelname)+8s %(reset)s %(message)s", + datefmt=tfrm, + log_colors={ + 'DEBUG': 'bg_cyan', + 'INFO': 'bg_green', + 'WARNING': 'bg_yellow', + 'ERROR': 'bg_red', + 'CRITICAL': 'bg_purple', + } + ) console.setFormatter(frm) self.log = logging.getLogger("log") # setable in config diff --git a/pyload/utils/colorlog/__init__.py b/pyload/utils/colorlog/__init__.py new file mode 100644 index 000000000..abf5532fb --- /dev/null +++ b/pyload/utils/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/utils/colorlog/colorlog.py b/pyload/utils/colorlog/colorlog.py new file mode 100644 index 000000000..5491676b8 --- /dev/null +++ b/pyload/utils/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/utils/colorlog/escape_codes.py b/pyload/utils/colorlog/escape_codes.py new file mode 100644 index 000000000..8d057e9e4 --- /dev/null +++ b/pyload/utils/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") -- cgit v1.2.3 From 3966d263b86f44477762b63123f91ef27fb37677 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Thu, 14 Nov 2013 12:37:09 +0100 Subject: White foreground instead black for the colored log level labels --- pyload/Core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyload/Core.py b/pyload/Core.py index 96421d862..bdf8af35f 100644 --- a/pyload/Core.py +++ b/pyload/Core.py @@ -520,7 +520,7 @@ class Core(object): tfrm = "%Y-%m-%d %H:%M:%S" frm = ColoredFormatter( - format="%(asctime)s %(log_color)s%(bold)s%(black)s %(levelname)+8s %(reset)s %(message)s", + format="%(asctime)s %(log_color)s%(bold)s%(white)s %(levelname)+8s %(reset)s %(message)s", datefmt=tfrm, log_colors={ 'DEBUG': 'bg_cyan', -- cgit v1.2.3 From 73a1a2ff63cd3a2372b8c5c7cb959f51babe9222 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 16 Nov 2013 21:51:12 +0100 Subject: Fixed logfile color codes (colorize console only) + settable color log type + cleaned pyload init_logger code --- pyload/Core.py | 56 ++++++++++++++++++++++++++++++++---------------- pyload/config/default.py | 1 + 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/pyload/Core.py b/pyload/Core.py index bdf8af35f..52e2a4045 100644 --- a/pyload/Core.py +++ b/pyload/Core.py @@ -503,7 +503,7 @@ class Core(object): console = logging.StreamHandler(sys.stdout) # try to get a time formatting depending on system locale - tfrm = None + datefmt = None try: # change current locale to default if it is not set current_locale = locale.getlocale() if current_locale == (None, None): @@ -511,26 +511,45 @@ class Core(object): # We use timeformat provided by locale when available if current_locale != (None, None): - tfrm = locale.nl_langinfo(locale.D_FMT) + " " + locale.nl_langinfo(locale.T_FMT) + datefmt = locale.nl_langinfo(locale.D_FMT) + " " + locale.nl_langinfo(locale.T_FMT) except: # something did go wrong, locale is heavily platform dependant pass # default formatting when no one was obtained (ex.: 2013-10-22 18:27:46) - if not tfrm: - tfrm = "%Y-%m-%d %H:%M:%S" - - frm = ColoredFormatter( - format="%(asctime)s %(log_color)s%(bold)s%(white)s %(levelname)+8s %(reset)s %(message)s", - datefmt=tfrm, - log_colors={ - 'DEBUG': 'bg_cyan', - 'INFO': 'bg_green', - 'WARNING': 'bg_yellow', - 'ERROR': 'bg_red', - 'CRITICAL': 'bg_purple', - } - ) - console.setFormatter(frm) + if not datefmt: + datefmt = "%Y-%m-%d %H:%M:%S" + + # file handler formatter + fhfmt = "%(asctime)s %(levelname)-8s %(message)s" + fh_frm = logging.Formatter(fhfmt, datefmt) + + # console formatter + if self.config['log']['console_color'] == "No": + console_frm = fh_frm + else: + if self.config['log']['console_color'] == "Light": + cfmt = "%(asctime)s %(log_color)s%(bold)s%(white)s %(levelname)+8s %(reset)s %(message)s" + clr = { + 'DEBUG': 'bg_cyan', + 'INFO': 'bg_green', + 'WARNING': 'bg_yellow', + 'ERROR': 'bg_red', + 'CRITICAL': 'bg_purple', + } + elif self.config['log']['console_color'] == "Full": + cfmt = "%(asctime)s %(log_color)s[%(levelname)-8s] %(message)s" + clr = { + 'DEBUG': 'cyan', + 'INFO': 'green', + 'WARNING': 'yellow', + 'ERROR': 'red', + 'CRITICAL': 'purple', + } + console_frm = ColoredFormatter(cfmt, datefmt, clr) + + #: set console formatter + console.setFormatter(console_frm) + self.log = logging.getLogger("log") # setable in config if not exists(self.config['log']['log_folder']): @@ -545,7 +564,8 @@ class Core(object): else: file_handler = logging.FileHandler(join(self.config['log']['log_folder'], 'log.txt'), encoding="utf8") - file_handler.setFormatter(frm) + #: set file handler formatter + file_handler.setFormatter(fh_frm) self.log.addHandler(file_handler) self.log.addHandler(console) #if console logging diff --git a/pyload/config/default.py b/pyload/config/default.py index 0ae1a2649..8e2dcae74 100644 --- a/pyload/config/default.py +++ b/pyload/config/default.py @@ -25,6 +25,7 @@ def make_config(config): ("file_log", "bool", _("File Log"), True), ("log_count", "int", _("Count"), 5), ("log_rotate", "bool", _("Log Rotate"), True), + ("console_color", "No;Light;Full", _("Colorize Console"), "Light"), ]) config.addConfigSection("permission", _("Permissions"), _("Description"), _("Long description"), -- cgit v1.2.3 From 5edf4597efba463d57039d337b3656c8fa5fea78 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 16 Nov 2013 22:31:35 +0100 Subject: Applied RaNaN advice --- .gitignore | 2 +- pyload/Core.py | 4 +- pyload/lib/colorlog/__init__.py | 8 ++++ pyload/lib/colorlog/colorlog.py | 76 +++++++++++++++++++++++++++++++++++ pyload/lib/colorlog/escape_codes.py | 37 +++++++++++++++++ pyload/utils/colorlog/__init__.py | 8 ---- pyload/utils/colorlog/colorlog.py | 76 ----------------------------------- pyload/utils/colorlog/escape_codes.py | 37 ----------------- 8 files changed, 124 insertions(+), 124 deletions(-) create mode 100644 pyload/lib/colorlog/__init__.py create mode 100644 pyload/lib/colorlog/colorlog.py create mode 100644 pyload/lib/colorlog/escape_codes.py delete mode 100644 pyload/utils/colorlog/__init__.py delete mode 100644 pyload/utils/colorlog/colorlog.py delete mode 100644 pyload/utils/colorlog/escape_codes.py diff --git a/.gitignore b/.gitignore index 52969376b..ea0e5508f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,7 @@ var sdist develop-eggs .installed.cfg -lib +lib/* lib64 __pycache__ diff --git a/pyload/Core.py b/pyload/Core.py index 52e2a4045..8897764a7 100644 --- a/pyload/Core.py +++ b/pyload/Core.py @@ -59,8 +59,6 @@ from utils.fs import free_space, exists, makedirs, join, chmod from codecs import getwriter -from utils.colorlog import ColoredFormatter - # test runner overwrites sys.stdout if hasattr(sys.stdout, "encoding"): enc = get_console_encoding(sys.stdout.encoding) else: enc = "utf8" @@ -527,6 +525,8 @@ class Core(object): if self.config['log']['console_color'] == "No": console_frm = fh_frm else: + from lib.colorlog import ColoredFormatter + if self.config['log']['console_color'] == "Light": cfmt = "%(asctime)s %(log_color)s%(bold)s%(white)s %(levelname)+8s %(reset)s %(message)s" clr = { 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") diff --git a/pyload/utils/colorlog/__init__.py b/pyload/utils/colorlog/__init__.py deleted file mode 100644 index abf5532fb..000000000 --- a/pyload/utils/colorlog/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -"""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/utils/colorlog/colorlog.py b/pyload/utils/colorlog/colorlog.py deleted file mode 100644 index 5491676b8..000000000 --- a/pyload/utils/colorlog/colorlog.py +++ /dev/null @@ -1,76 +0,0 @@ -"""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/utils/colorlog/escape_codes.py b/pyload/utils/colorlog/escape_codes.py deleted file mode 100644 index 8d057e9e4..000000000 --- a/pyload/utils/colorlog/escape_codes.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -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") -- cgit v1.2.3