-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
104 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,53 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# std | ||
import colorlog | ||
import logging | ||
from typing import Union | ||
|
||
try: | ||
import colorlog | ||
except ImportError: | ||
colorlog = None | ||
|
||
LOG_DEFAULT_LEVEL = logging.WARNING | ||
|
||
def get_logger(name="AnkiPandas", level=logging.WARNING, | ||
sh_level=logging.WARNING): | ||
"""Sets up a logging.Logger. | ||
|
||
If the colorlog module is available, the logger will use colors, | ||
otherwise it will be in b/w. The colorlog module is available at | ||
https://github.com/borntyping/python-colorlog but can also easily be | ||
installed with e.g. 'sudo pip3 colorlog' or similar commands. | ||
def get_logger(): | ||
""" Sets up global logger. """ | ||
_log = colorlog.getLogger("AnkiPandas") | ||
|
||
Args: | ||
name: name of the logger | ||
level: General logging level | ||
sh_level: Logging level of stream handler | ||
Returns: | ||
Logger | ||
""" | ||
if colorlog: | ||
_logger = colorlog.getLogger(name) | ||
else: | ||
_logger = logging.getLogger(name) | ||
|
||
if _logger.handlers: | ||
if _log.handlers: | ||
# the logger already has handlers attached to it, even though | ||
# we didn't add it ==> logging.get_logger got us an existing | ||
# logger ==> we don't need to do anything | ||
return _logger | ||
|
||
_logger.setLevel(level) | ||
if colorlog is not None: | ||
sh = colorlog.StreamHandler() | ||
log_colors = { | ||
'DEBUG': 'cyan', | ||
'INFO': 'green', | ||
'WARNING': 'yellow', | ||
'ERROR': 'red', | ||
'CRITICAL': 'red' | ||
} | ||
formatter = colorlog.ColoredFormatter( | ||
'%(log_color)s%(name)s:%(levelname)s:%(message)s', | ||
log_colors=log_colors) | ||
else: | ||
# no colorlog available: | ||
sh = logging.StreamHandler() | ||
formatter = logging.Formatter('%(name)s:%(levelname)s:%(message)s') | ||
return _log | ||
|
||
_log.setLevel(LOG_DEFAULT_LEVEL) | ||
|
||
sh = colorlog.StreamHandler() | ||
log_colors = { | ||
'DEBUG': 'cyan', | ||
'INFO': 'green', | ||
'WARNING': 'yellow', | ||
'ERROR': 'red', | ||
'CRITICAL': 'red' | ||
} | ||
formatter = colorlog.ColoredFormatter( | ||
'%(log_color)s%(levelname)s: %(message)s', | ||
log_colors=log_colors | ||
) | ||
sh.setFormatter(formatter) | ||
sh.setLevel(sh_level) | ||
_logger.addHandler(sh) | ||
# Controlled by overall logger level | ||
sh.setLevel(logging.DEBUG) | ||
|
||
_log.addHandler(sh) | ||
|
||
return _log | ||
|
||
|
||
# todo: doc | ||
def set_log_level(level: Union[str, int]): | ||
lvl = level | ||
if isinstance(level, str): | ||
lvl = getattr(logging, level.upper()) | ||
get_logger().setLevel(lvl) | ||
|
||
if colorlog is None: | ||
_logger.debug("Module colorlog not available. Log will be b/w.") | ||
|
||
return _logger | ||
log = get_logger() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# std | ||
import unittest | ||
|
||
# ours | ||
from ankipandas.util.log import log, get_logger, set_log_level | ||
|
||
|
||
class TestLogging(unittest.TestCase): | ||
""" Only tests that things run without error. """ | ||
def test_log(self): | ||
log.info("Test info") | ||
log.warning("Test warning") | ||
|
||
def test_get_logger(self): | ||
get_logger().info("Test info") | ||
get_logger().warning("Test warning") | ||
|
||
def test_set_log_level(self): | ||
set_log_level("warning") | ||
set_log_level("WARNING") | ||
set_log_level(0) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Utilities | ||
========= | ||
|
||
.. module:: ankipandas.util | ||
|
||
.. automodule:: ankipandas.util | ||
|
||
.. automodule:: ankipandas.util.log | ||
:members: | ||
:undoc-members: | ||
|
||
.. automodule:: ankipandas.util.dataframe | ||
:members: | ||
:undoc-members: | ||
|
||
.. automodule:: ankipandas.util.docstrings | ||
:members: | ||
:undoc-members: | ||
|