Skip to content

Commit a572b91

Browse files
committed
Remove some duplicate code. Also beautified some parts.
1 parent 9bb5027 commit a572b91

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

friendlylog/colored_logger.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,33 @@
33

44
from colored import fg, attr
55
from copy import copy
6+
from friendlylog.constants import (
7+
DEBUG,
8+
INFO,
9+
WARNING,
10+
ERROR,
11+
CRITICAL,
12+
13+
LOG_LEVEL_LIST
14+
)
615
from friendlylog.utils import do_not_propagate
716

817

918
# Where the logs should be sent.
1019
_STREAM = sys.stdout
1120

21+
1222
class _ColoredFormatter(logging.Formatter):
1323

1424
def __init__(self, *args, **kwargs):
1525
super(_ColoredFormatter, self).__init__(*args, **kwargs)
1626

1727
@staticmethod
1828
def _colorize(msg, loglevel):
19-
DEBUG = "debug"
20-
INFO = "info"
21-
WARNING = "warning"
22-
ERROR = "error"
23-
CRITICAL = "critical"
24-
2529
loglevel = str(loglevel).lower()
26-
if loglevel not in [DEBUG, INFO, WARNING, ERROR, CRITICAL]:
30+
if loglevel not in LOG_LEVEL_LIST:
2731
raise RuntimeError("{} should be oneof {}.".format(
28-
loglevel, [DEBUG, INFO, WARNING, ERROR, CRITICAL])) # pragma: no cover
32+
loglevel, LOG_LEVEL_LIST)) # pragma: no cover
2933
msg = str(loglevel).upper() + ": " + str(msg)
3034

3135
if loglevel == DEBUG:
@@ -37,7 +41,7 @@ def _colorize(msg, loglevel):
3741
if loglevel == ERROR:
3842
return "{}{}{}{}{}".format(fg(202), attr(1), msg, attr(21), attr(0)) # noqa: E501
3943
if loglevel == CRITICAL:
40-
return "{}{}{}{}{}".format(fg(196), attr(1), msg, attr(21), attr(0))
44+
return "{}{}{}{}{}".format(fg(196), attr(1), msg, attr(21), attr(0)) # noqa: E501
4145

4246
def format(self, record):
4347
record = copy(record)

friendlylog/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DEBUG = "debug"
2+
INFO = "info"
3+
WARNING = "warning"
4+
ERROR = "error"
5+
CRITICAL = "critical"
6+
7+
LOG_LEVEL_LIST = [DEBUG, INFO, WARNING, ERROR, CRITICAL]

friendlylog/simple_logger.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,28 @@
22
import sys
33

44
from copy import copy
5+
from friendlylog.constants import (
6+
LOG_LEVEL_LIST
7+
)
58
from friendlylog.utils import do_not_propagate
69

710

811
# Where the logs should be sent.
912
_STREAM = sys.stdout
1013

14+
1115
class _SimpleFormatter(logging.Formatter):
1216

1317
def __init__(self, *args, **kwargs):
1418
super(_SimpleFormatter, self).__init__(*args, **kwargs)
1519

1620
@staticmethod
1721
def _process(msg, loglevel):
18-
DEBUG = "debug"
19-
INFO = "info"
20-
WARNING = "warning"
21-
ERROR = "error"
22-
CRITICAL = "critical"
23-
2422
loglevel = str(loglevel).lower()
25-
if loglevel not in [DEBUG, INFO, WARNING, ERROR, CRITICAL]:
23+
if loglevel not in LOG_LEVEL_LIST:
2624
raise RuntimeError("{} should be oneof {}.".format(
27-
loglevel, [DEBUG, INFO, WARNING, ERROR, CRITICAL])) # pragma: no cover
28-
msg = str(loglevel).upper() + ": " + str(msg)
29-
30-
return msg
25+
loglevel, LOG_LEVEL_LIST)) # pragma: no cover
26+
return str(loglevel).upper() + ": " + str(msg)
3127

3228
def format(self, record):
3329
record = copy(record)

0 commit comments

Comments
 (0)