/
logging.sage
58 lines (45 loc) · 1.93 KB
/
logging.sage
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
# Make sure current directory is in path.
# That's not true while doctesting (sage -t).
if '' not in sys.path:
sys.path = [''] + sys.path
from igp import *
import logging
# this is a version of Python's StreamHandler which prints log
# messages to the stream *currently* pointed to by sys.stderr (not the
# one when StreamHandler is set up). This is useful in a Sage notebook,
# where every cell has its own set of streams.
class DynamicStdErrStreamHandler(logging.StreamHandler):
"""
A handler class which writes logging records, appropriately formatted,
to a stream. Note that this class does not close the stream, as
sys.stdout or sys.stderr may be used.
"""
def __init__(self):
logging.StreamHandler.__init__(self, sys.stderr)
self.parent_class = logging.StreamHandler # save in object because name logging.StreamHandler is not available at exit
def flush(self):
try:
self.stream = sys.stderr
except NameError: # happens at exit in terminal
pass
self.parent_class.flush(self)
def emit(self, record):
try:
self.stream = sys.stderr
except NameError: # happens at exit in terminal
pass
self.parent_class.emit(self, record)
#logging.basicConfig(format='%(levelname)s: %(asctime)s %(message)s', level=logging.INFO)
if not logging.getLogger().handlers:
fmt = logging.Formatter('%(levelname)s: %(asctime)s %(message)s', None)
hdlr = DynamicStdErrStreamHandler()
hdlr.setFormatter(fmt)
logging.getLogger().addHandler(hdlr)
logging.getLogger().setLevel(logging.INFO)
def logger(func):
def inner(*args, **kwargs): #1
print "Arguments to %s were: %s, %s" % (func, args, kwargs)
result = func(*args, **kwargs) #2
print "Result is: %s" % (result)
return result
return inner