Created
June 19, 2015 00:01
-
-
Save feth/91b20437968478d81903 to your computer and use it in GitHub Desktop.
python logging wrapper
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
""" | |
logging tools | |
logging is mainly configured in app.ini | |
Here lie only a few wrappers/tools, including those used by the app.ini config | |
""" | |
import inspect | |
import logging | |
def get_logger(stackoverhead=0): | |
""" | |
wrapper for getLogger | |
Typical use: | |
>>> l = get_logger() | |
>>> l | |
<logging.Logger at ... > | |
>>> l.name | |
euphrate.utils.logging | |
:param string name: Name for the logger. | |
.. note:: | |
this function may be expanded | |
""" | |
frm = inspect.stack()[1 + stackoverhead] | |
mod = inspect.getmodule(frm[0]) | |
return logging.getLogger(mod.__name__) | |
def _log_with_level(func_name, *args, **kwargs): | |
stackoverhead = kwargs.pop('stackoverhead', 0) | |
logger = get_logger(stackoverhead=stackoverhead + 1) | |
logfunc = getattr(logger, func_name) | |
logfunc(*args, **kwargs) | |
def debug(*args, **kwargs): | |
_log_with_level('debug', stackoverhead=1, *args, **kwargs) | |
def info(*args, **kwargs): | |
_log_with_level('info', stackoverhead=1, *args, **kwargs) | |
def warning(*args, **kwargs): | |
_log_with_level('warning', stackoverhead=1, *args, **kwargs) | |
def error(*args, **kwargs): | |
_log_with_level('error', stackoverhead=1, *args, **kwargs) | |
def critical(*args, **kwargs): | |
_log_with_level('critical', stackoverhead=1, *args, **kwargs) | |
def exception(*args, **kwargs): | |
_log_with_level('exception', stackoverhead=1, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment