-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logging.py
146 lines (117 loc) · 3.57 KB
/
Logging.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'''
Created on Jan 30, 2011
@author: snail
'''
import logging
import logging.handlers
import os
import sys
from os.path import join
from os import getcwd
from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL
from pickle import dumps
LogPath = "Logs"
#ensure the logging path exists.
try:
from os import mkdir
mkdir(join(getcwd(), LogPath))
del mkdir
except:
pass
def currentframe():
"""Return the frame object for the caller's stack frame."""
try:
raise Exception
except:
return sys.exc_info()[2].tb_frame.f_back
def CreateLogger(name, level=None):
l = logging.getLogger(name)
l.setLevel(DEBUG)
if level != None:
l.setLevel(level)
handler = logging.handlers.RotatingFileHandler(join(
LogPath, "%s.log" % name), maxBytes=10240, backupCount=10)
formatter = logging.Formatter("%(asctime)s|%(thread)d|%(levelno)s|%(module)s:%(funcName)s:%(lineno)d|%(message)s")
handler.setFormatter(formatter)
l.addHandler(handler)
return l
class LogFile:
def __init__(self, output, minLevel=WARNING):
self.minLevel = minLevel
self._log = CreateLogger(output)
self._log.findCaller = self.findCaller
def findCaller(self):
"""
Find the stack frame of the caller so that we can note the source
file name, line number and function name.
"""
f = currentframe()
if f is not None:
f = f.f_back
rv = "(unknown file)", 0, "(unknown function)"
i = 5
while hasattr(f, "f_code") and i > 0:
i = i - 1
co = f.f_code
rv = (co.co_filename, f.f_lineno, co.co_name)
f = f.f_back
return rv
def debug(self, *vals, **kws):
self.log(DEBUG, *vals, **kws)
def note(self, *vals, **kws):
self.log(INFO, *vals, **kws)
def info(self, *vals, **kws):
self.log(INFO, *vals, **kws)
def warning(self, *vals, **kws):
self.log(WARNING, *vals, **kws)
def error(self, *vals, **kws):
self.log(ERROR, *vals, **kws)
def critical(self, *vals, **kws):
self.log(CRITICAL, *vals, **kws)
def dict(self, d, *vals):
if d:
lines = map(lambda (x, y): str(x) + " => " + str(y), d.items())
else:
lines = ["None"]
lines+=vals
self.log(DEBUG, *lines)
def exception(self, *vals):
lines = list(vals)
import sys
import traceback
tb = sys.exc_info()
tbLines = (traceback.format_exception(*tb))
for l in tbLines:
lines += l[:-1].split("\n")
self.log(ERROR,*lines)
global ExceptionLog
ExceptionLog.log(ERROR,*lines)
def log(self, level, *vals, **kws):
self._log.log(level, "\t".join(map(str, vals)))
ExceptionLog = LogFile("Exceptions")
if __name__ == "__main__":
import threading
import time
import random
class Worker(threading.Thread):
log = None
def run(self):
for i in range(20):
time.sleep(random.random() * .1)
if self.log:
self.foo()
self.log.debug("Exception time!")
try:
self.bar()
except:
self.log.exception("Exception while doing math!")
def bar(self):
i = 1 / 0
def foo(self):
self.log.warning(i, "abc", "123")
logger = LogFile("test")
for i in range(20):
w = Worker()
w.log = logger
w.start()
logger.dict({"a":"a","foo":"bar",1:[1]})