本文整理汇总了Python中logging.handlers.BufferingHandler.flush方法的典型用法代码示例。如果您正苦于以下问题:Python BufferingHandler.flush方法的具体用法?Python BufferingHandler.flush怎么用?Python BufferingHandler.flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logging.handlers.BufferingHandler
的用法示例。
在下文中一共展示了BufferingHandler.flush方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_request
# 需要导入模块: from logging.handlers import BufferingHandler [as 别名]
# 或者: from logging.handlers.BufferingHandler import flush [as 别名]
class QueryMiddleware:
def process_request(self, request):
if (settings.DEBUG or request.user.is_superuser) and request.REQUEST.has_key('query'):
self.time_started = time.time()
self.sql_offset_start = len(connection.queries)
if not hasattr(self, 'loghandler'):
self.loghandler = BufferingHandler(1000) # Create and a handler
logging.getLogger('').addHandler(self.loghandler)
else:
self.loghandler.flush() # Empty it of all messages
def process_response(self, request, response):
if (settings.DEBUG or request.user.is_superuser) and request.REQUEST.has_key('query'):
sql_queries = connection.queries[self.sql_offset_start:]
# Reformat sql queries a bit
sql_total = 0.0
for query in sql_queries:
query['sql'] = reformat_sql(query['sql'])
sql_total += float(query['time'])
# Count the most-executed queries
most_executed = {}
for query in sql_queries:
reformatted = reformat_sql(query['sql_no_params'])
most_executed.setdefault(reformatted, []).append(query)
most_executed = most_executed.items()
most_executed.sort(key = lambda v: len(v[1]), reverse=True)
most_executed = most_executed[:10]
template_context = Context({
'sql': sql_queries,
'sql_total': sql_total,
'bad_sql_count': len([s for s in sql_queries if s['bad']]),
'most_executed': most_executed,
'server_time': time.time() - self.time_started,
})
response.content = Template(TEMPLATE).render(template_context)
return response
示例2: process_request
# 需要导入模块: from logging.handlers import BufferingHandler [as 别名]
# 或者: from logging.handlers.BufferingHandler import flush [as 别名]
class TemplateMiddleware:
def process_request(self, request):
if (settings.DEBUG or request.user.is_superuser) and request.REQUEST.has_key('template'):
self.time_started = time.time()
self.templates_used = []
self.contexts_used = []
if not hasattr(self, 'loghandler'):
self.loghandler = BufferingHandler(1000) # Create and a handler
logging.getLogger('').addHandler(self.loghandler)
else:
self.loghandler.flush() # Empty it of all messages
template_rendered.connect(
self._storeRenderedTemplates
)
def process_response(self, request, response):
if (settings.DEBUG or request.user.is_superuser) and request.REQUEST.has_key('template'):
templates = [
(t.name, t.origin and t.origin.name or 'No origin')
for t in self.templates_used
]
template_context = Context({
'server_time': time.time() - self.time_started,
'templates': templates,
'template_dirs': settings.TEMPLATE_DIRS,
})
response.content = Template(TEMPLATE).render(template_context)
return response
def _storeRenderedTemplates(self, signal, sender, template, context, **kwargs):
self.templates_used.append(template)
self.contexts_used.append(context)
示例3: flush
# 需要导入模块: from logging.handlers import BufferingHandler [as 别名]
# 或者: from logging.handlers.BufferingHandler import flush [as 别名]
def flush(self):
"""
Clears out the `buffer` and `formatted` attributes.
"""
BufferingHandler.flush(self)
self.formatted = []
示例4: WorkerTest
# 需要导入模块: from logging.handlers import BufferingHandler [as 别名]
# 或者: from logging.handlers.BufferingHandler import flush [as 别名]
class WorkerTest(unittest.TestCase):
def setUp(self):
def set_ev(fu):
def new_fu(*args, **kwargs):
s = args[0]
s.event.set()
s.val = (args, kwargs)
return fu(*args, **kwargs)
return new_fu
class ATestWorker(Worker):
def __init__(self, name, message_queue):
Worker.__init__(self, name, message_queue)
self.event = Event()
self.val = None
self.started = False
self.stopped = False
@local_thread
@set_ev
def echo(self, val):
return val
@local_thread_blocking
@set_ev
def echo_block(self, val):
return val
def onStart(self):
self.started = True
def onStop(self):
self.stopped = True
@local_thread
def raise_(self, ex):
raise ex
@local_thread_blocking
def raise_blocking(self, ex):
raise ex
@set_ev
def call_me_by_name(self, arg1, arg2):
return
def call_me_by_name_blocking(self, arg1, arg2):
return arg1, arg2
self.buha = BufferingHandler(10000)
q = Queue()
self.q = q
NAME = "Test"
l = logging.getLogger(NAME)
self.w = ATestWorker(NAME, q)
self.assertEqual(self.w.log(), l)
l.propagate = 0
l.addHandler(self.buha)
self.assertFalse(self.w.started)
self.w.start()
sleep(0.05)
self.assertTrue(self.w.started)
def testName(self):
assert(self.w.name() == "Test")
def testMessageQueue(self):
assert(self.w.message_queue() == self.q)
def testLocalThread(self):
s = "Testing"
self.w.event.clear()
self.w.echo(s)
self.w.event.wait(5)
args, kwargs = self.w.val
assert(args[1] == s)
def testLocalThreadException(self):
self.buha.flush()
self.w.raise_(Exception())
sleep(0.1) # hard delay
assert(len(self.buha.buffer) != 0)
assert(self.buha.buffer[0].levelno == ERROR)
def testCallByName(self):
self.w.event.clear()
self.w.call_by_name(self.w, "call_me_by_name", "arg1", arg2="arg2")
self.w.event.wait(5)
args, kwargs = self.w.val
assert(args[1] == "arg1")
assert(kwargs["arg2"] == "arg2")
#.........这里部分代码省略.........