/
easylogger.py
104 lines (83 loc) · 3 KB
/
easylogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import logging
import functools
import os
import sys
def set_srcfile():
if hasattr(sys, 'frozen'): #support for py2exe
_srcfile = "logging%s__init__%s" % (os.sep, __file__[-4:])
elif (__file__[-4:]).lower() in ['.pyc', '.pyo']:
_srcfile = __file__[:-4] + '.py'
else:
_srcfile = __file__
_srcfile = os.path.normcase(_srcfile)
return _srcfile
# http://stackoverflow.com/questions/4957858/
# Ugly, ugly hack. I'm sorry.
# We have no self here because it seems not to be passed to
# the monkeypatched method.
def find_caller_monkeypatch(self, stack_info=False):
# pylint: disable=invalid-name, protected-access
"""
Find the stack frame of the caller so that we can note the source
file name, line number and function name.
"""
f = logging.currentframe().f_back
rv = "(unknown file)", 0, "(unknown function)"
while hasattr(f, "f_code"):
co = f.f_code
filename = os.path.normcase(co.co_filename)
# This line is modified.
if filename in (set_srcfile(), logging._srcfile):
f = f.f_back
continue
# changes for 3.4
sinfo = None
if stack_info:
sio = io.StringIO()
sio.write('Stack (most recent call last):\n')
traceback.print_stack(f, file=sio)
sinfo = sio.getvalue()
if sinfo[-1] == '\n':
sinfo = sinfo[:-1]
sio.close()
rv = (filename, f.f_lineno, co.co_name, sinfo)
break
return rv
class EasyLogger(object):
# OVERRIDDEN = ['critical', 'error', 'warning', 'info', 'debug']
# this gets angry in 2.7
SEP = " "
def __init__(self, logger=logging.getLogger(__name__)):
self.logger = logger
# Ugly, ugly, ugly dirty hack to fix line numbers
self.logger.findCaller = find_caller_monkeypatch
def _format_str(self, *args):
return self.SEP.join([str(a) for a in args])
def debug(self, *args):
self.logger.debug(self._format_str(*args))
def info(self, *args):
self.logger.info(self._format_str(*args))
def warning(self, *args):
self.logger.warning(self._format_str(*args))
def error(self, *args):
self.logger.error(self._format_str(*args))
def critical(self, *args):
self.logger.critical(self._format_str(*args))
def __getattr__(self, name):
return getattr(self.logger, name)
LOGGING_FMT = "<%(filename)s:%(lineno)s(%(levelname)s) - %(funcName)s()> "\
"%(message)s"
logging.basicConfig(level=logging.DEBUG, format=LOGGING_FMT)
# LOG = logging.getLogger(__name__)
LOG = EasyLogger()
def log_at(new_level=logging.ERROR, logger=LOG):
def wrap(func):
@functools.wraps(func)
def wrapped_func(*args, **kwargs):
old_level = logger.level
logger.setLevel(new_level)
to_ret = func(*args, **kwargs)
logger.setLevel(old_level)
return to_ret
return wrapped_func
return wrap