Skip to content

Commit

Permalink
Remove support for old-style super() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
borntyping committed Apr 13, 2021
1 parent 68314fd commit eba231f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 54 deletions.
66 changes: 20 additions & 46 deletions colorlog/colorlog.py
Expand Up @@ -93,23 +93,16 @@ def __init__(
Map secondary ``log_color`` attributes. (*New in version 2.6.*)
"""
if fmt is None:
if sys.version_info > (3, 2):
fmt = default_formats[style]
else:
fmt = default_formats["%"]
fmt = default_formats[style]

if (
sys.version_info > (3, 8)
and isinstance(self, LevelFormatter)
and isinstance(fmt, dict)
):
super(ColoredFormatter, self).__init__(fmt, datefmt, style, validate=False)
elif sys.version_info > (3, 2):
super(ColoredFormatter, self).__init__(fmt, datefmt, style)
elif sys.version_info > (2, 7):
super(ColoredFormatter, self).__init__(fmt, datefmt)
else:
logging.Formatter.__init__(self, fmt, datefmt)
super(ColoredFormatter, self).__init__(fmt, datefmt, style)

self.log_colors = log_colors if log_colors is not None else default_log_colors
self.secondary_log_colors = secondary_log_colors
Expand All @@ -131,10 +124,7 @@ def format(self, record):
setattr(record, name + "_log_color", color)

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

# Add a reset code to the end of the message
# (if it wasn't explicitly added in format str)
Expand Down Expand Up @@ -178,47 +168,31 @@ def __init__(
'CRITICAL': '%(log_color)sCRIT: %(msg)s (%(module)s:%(lineno)d)',
})
"""
if sys.version_info > (2, 7):
super(LevelFormatter, self).__init__(
fmt=fmt,
datefmt=datefmt,
style=style,
log_colors=log_colors,
reset=reset,
secondary_log_colors=secondary_log_colors,
)
else:
ColoredFormatter.__init__(
self,
fmt=fmt,
datefmt=datefmt,
style=style,
log_colors=log_colors,
reset=reset,
secondary_log_colors=secondary_log_colors,
)
super(LevelFormatter, self).__init__(
fmt=fmt,
datefmt=datefmt,
style=style,
log_colors=log_colors,
reset=reset,
secondary_log_colors=secondary_log_colors,
)
self.style = style
self.fmt = fmt

def format(self, record):
"""Customize the message format based on the log level."""
if isinstance(self.fmt, dict):
self._fmt = self.fmt[record.levelname]
if sys.version_info > (3, 2):
# Update self._style because we've changed self._fmt
# (code based on stdlib's logging.Formatter.__init__())
if self.style not in logging._STYLES:
raise ValueError(
"Style must be one of: %s" % ",".join(logging._STYLES.keys())
)
self._style = logging._STYLES[self.style][0](self._fmt)

if sys.version_info > (2, 7):
message = super(LevelFormatter, self).format(record)
else:
message = ColoredFormatter.format(self, record)

return message
# Update self._style because we've changed self._fmt
# (code based on stdlib's logging.Formatter.__init__())
if self.style not in logging._STYLES:
raise ValueError(
"Style must be one of: %s" % ",".join(logging._STYLES.keys())
)
self._style = logging._STYLES[self.style][0](self._fmt)

return super(LevelFormatter, self).format(record)


class TTYColoredFormatter(ColoredFormatter):
Expand Down
18 changes: 14 additions & 4 deletions colorlog/tests/test_colorlog.py
Expand Up @@ -2,8 +2,6 @@

import sys

import pytest

import colorlog


Expand Down Expand Up @@ -74,20 +72,32 @@ def test_percent_style(create_and_test_logger):
)


@pytest.mark.skipif(sys.version_info < (3, 2), reason="requires python3.2")
def test_braces_style(create_and_test_logger):
create_and_test_logger(
fmt="{log_color}{levelname}{reset}:{name}:{message}", style="{"
)


@pytest.mark.skipif(sys.version_info < (3, 2), reason="requires python3.2")
def test_template_style(create_and_test_logger):
create_and_test_logger(
fmt="${log_color}${levelname}${reset}:${name}:${message}", style="$"
)


class TestLevelFormatter:
def test_level_formatter(self, create_and_test_logger):
create_and_test_logger(
formatter_class=colorlog.LevelFormatter,
fmt={
"DEBUG": "%(message)s",
"INFO": "%(message)s",
"WARNING": "%(message)s",
"ERROR": "%(message)s",
"CRITICAL": "%(message)s",
},
)


def test_ttycolorlog(create_and_test_logger, monkeypatch):
monkeypatch.setattr(sys.stderr, "isatty", lambda: True)
create_and_test_logger(
Expand Down
4 changes: 0 additions & 4 deletions colorlog/tests/test_config.py
Expand Up @@ -3,9 +3,6 @@
import logging
import logging.config
import os.path
import sys

import pytest


def path(filename):
Expand All @@ -18,7 +15,6 @@ def test_build_from_file(test_logger):
test_logger(logging.getLogger(), lambda l: ":test_config.ini" in l)


@pytest.mark.skipif(sys.version_info < (2, 7), reason="requires python2.7")
def test_build_from_dictionary(test_logger):
logging.config.dictConfig(
{
Expand Down

0 comments on commit eba231f

Please sign in to comment.