本文整理汇总了Python中logging.handlers.BufferingHandler类的典型用法代码示例。如果您正苦于以下问题:Python BufferingHandler类的具体用法?Python BufferingHandler怎么用?Python BufferingHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BufferingHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, config, level=None):
BufferingHandler.__init__(self, 1000)
self.config = config
self.old_handlers = []
self.old_level = None
# set my formatter
fmt = datefmt = None
if config.logging_format:
fmt = config.logging_format
else:
fmt = '%(levelname)s:%(name)s:%(message)s'
if config.logging_datefmt:
datefmt = config.logging_datefmt
fmt = logging.Formatter(fmt, datefmt)
self.setFormatter(fmt)
# figure the level we're logging at
if level is not None:
self.level = level
elif config.logging_level:
self.level = config.logging_level
else:
self.level = logging.NOTSET
# construct my filter
if config.logging_filter:
self.addFilter(RecordFilter(config.logging_filter))
示例2: __init__
def __init__(self, config, level=None):
BufferingHandler.__init__(self, 1000)
self.config = config
self.old_handlers = []
# set my formatter
fmt = datefmt = None
if config.logging_format:
fmt = config.logging_format
else:
fmt = "%(levelname)s:%(name)s:%(message)s"
if config.logging_datefmt:
datefmt = config.logging_datefmt
fmt = logging.Formatter(fmt, datefmt)
self.setFormatter(fmt)
# figure the level we're logging at
if level is not None:
self.level = level
elif config.logging_level:
self.level = getattr(logging, config.logging_level.upper(), None)
if self.level is None:
raise ConfigError('Invalid log level: "%s"' % config.logging_level)
else:
self.level = logging.NOTSET
# construct my filter
if config.logging_filter:
self.addFilter(RecordFilter(config.logging_filter))
示例3: DebugDumper
class DebugDumper(BCRelay):
def __init__(self, *argz, **kwz):
super(DebugDumper, self).__init__(*argz, **kwz)
# Simple buffered handler that never triggers flush
self.handler = BufferingHandler(capacity=self.conf.capacity)
self.buffer = self.handler.buffer = deque(maxlen=self.handler.capacity)
self.handler.capacity += 1
self.handler.setLevel(self.conf.level)
self.handler.setFormatter(
logging.Formatter(self.conf.format, self.conf.datefmt) )
logging.root.addHandler(self.handler)
# Signal log-dump interface
if self.conf.signal and isinstance(self.conf.signal, str):
signum = getattr(signal, self.conf.signal, None)
if not signum: signum = getattr(signal, 'SIG{}'.format(self.conf.signal), None)
self.conf.signal = signum
if self.conf.signal:
def signal_handler(sig, frm):
# Supress buffering of re-issued messages
self.handler._emit, self.handler.emit = self.handler.emit, lambda *a, **k: None
for msg in list(self.buffer): log.fatal(self.handler.format(msg))
self.handler.emit = self.handler._emit
signal.signal(self.conf.signal, signal_handler)
def dispatch(self, msg, source=None):
if msg != self.conf.command:
log.noise('Ignoring unknown command: {!r} (source: {})'.format(msg, source))
return
if not self.conf.direct: source = None # reply to whatever destination channel
msg = '\n'.join(it.imap(self.handler.format, list(self.buffer)))
reactor.callLater( 0, self.interface.dispatch,
msg, source=self, user=source, direct=True )
示例4: __init__
def __init__(self):
# BufferingHandler takes a "capacity" argument
# so as to know when to flush. As we're overriding
# shouldFlush anyway, we can set a capacity of zero.
# You can call flush() manually to clear out the
# buffer.
BufferingHandler.__init__(self, 0)
示例5: __init__
def __init__(self, target, thresholdLevel, retainLevel):
BufferingHandler.__init__(self, capacity=0)
self.target = target
self.thresholdLevel = thresholdLevel
if retainLevel is None:
retainLevel = thresholdLevel
self.setLevel(retainLevel)
示例6: __init__
def __init__(self, testcase):
"""Create a logging handler for the given test case.
Args:
testcase (unittest.TestCase): Owner of this logging handler.
"""
BufferingHandler.__init__(self, capacity=0)
self.setLevel(logging.DEBUG)
self.testcase = testcase
示例7: __init__
def __init__(self, level, capacity, host, port,
fromaddr, toaddrs, user=None, passwd=None,
subject='logs', encoding='utf-8'):
BufferingHandler.__init__(self, capacity)
self.setLevel(level)
self.host = host
self.port = port
self.fromaddr = fromaddr
self.toaddrs = toaddrs if isinstance(toaddrs, (list, tuple)) else [toaddrs]
self.user = user
self.passwd = passwd
self.subject = Header(subject)
self.encoding = encoding
示例8: create_session_logger
def create_session_logger(format="CPC"):
logger = logging.getLogger("")
try:
logger.addHandler(HANDLER["%s-buffer" % format])
except KeyError:
_formatter = logging.Formatter(format)
handl = BufferingHandler(10000)
handl.setFormatter(_formatter)
logger.addHandler(handl)
logger.setLevel(logging.INFO)
return logger
示例9: __init__
def __init__(self, capacity, fd_target):
"""
:param int capacity: Amount of records to store in memory
https://github.com/python/cpython/blob/3.3/Lib/logging/handlers.py#L1161-L1176
:param object fd_target: File descriptor to write output to (e.g. `sys.stdout`)
"""
# Call our BufferingHandler init
if issubclass(BufferingTargetHandler, object):
super(BufferingTargetHandler, self).__init__(capacity)
else:
BufferingHandler.__init__(self, capacity)
# Save target for later
self._fd_target = fd_target
示例10: process_request
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
示例11: close
def close(self):
"""Upon `close`, flush our internal info to the target"""
# Flush our buffers to the target
# https://github.com/python/cpython/blob/3.3/Lib/logging/handlers.py#L1185
# https://github.com/python/cpython/blob/3.3/Lib/logging/handlers.py#L1241-L1256
self.acquire()
try:
for record in self.buffer:
if record.levelno < self.level:
continue
msg = self.format(record)
print(msg, file=self._fd_target)
finally:
self.release()
# Then, run our normal close actions
if issubclass(BufferingTargetHandler, object):
super(BufferingTargetHandler, self).close()
else:
BufferingHandler.close(self)
示例12: create_logger
def create_logger(filename, base_dir):
"""
Creates a logger with a given filename.
:param filename: File name for the log
:return: A logger class.
"""
logger = logging.getLogger("")
logfile_name = base_dir + filename
handler = logging.FileHandler(logfile_name)
base_formatter = logging.Formatter(
"%(asctime)s %(name)s:%(levelname)s %(message)s")
cpc = ('%(asctime)s %(name)s:%(levelname)s '
'[%(client)s,%(path)s,%(cid)s] %(message)s')
handler.setFormatter(base_formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
_formatter = logging.Formatter(cpc)
fil_handler = logging.FileHandler(logfile_name)
fil_handler.setFormatter(_formatter)
buf_handler = BufferingHandler(10000)
buf_handler.setFormatter(_formatter)
return logger
示例13: process_request
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)
示例14: create_logger
def create_logger(filename):
"""
Creates a logger with a given filename.
:param filename: File name for the log
:return: A logger class.
"""
logger = logging.getLogger("")
LOGFILE_NAME = filename
hdlr = logging.FileHandler(LOGFILE_NAME)
base_formatter = logging.Formatter(
"%(asctime)s %(name)s:%(levelname)s %(message)s")
CPC = ('%(asctime)s %(name)s:%(levelname)s '
'[%(client)s,%(path)s,%(cid)s] %(message)s')
cpc_formatter = logging.Formatter(CPC)
hdlr.setFormatter(base_formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)
_formatter = logging.Formatter(CPC)
fil_handl = logging.FileHandler(LOGFILE_NAME)
fil_handl.setFormatter(_formatter)
buf_handl = BufferingHandler(10000)
buf_handl.setFormatter(_formatter)
return logger
示例15: __init__
def __init__(self, *argz, **kwz):
super(DebugDumper, self).__init__(*argz, **kwz)
# Simple buffered handler that never triggers flush
self.handler = BufferingHandler(capacity=self.conf.capacity)
self.buffer = self.handler.buffer = deque(maxlen=self.handler.capacity)
self.handler.capacity += 1
self.handler.setLevel(self.conf.level)
self.handler.setFormatter(
logging.Formatter(self.conf.format, self.conf.datefmt) )
logging.root.addHandler(self.handler)
# Signal log-dump interface
if self.conf.signal and isinstance(self.conf.signal, str):
signum = getattr(signal, self.conf.signal, None)
if not signum: signum = getattr(signal, 'SIG{}'.format(self.conf.signal), None)
self.conf.signal = signum
if self.conf.signal:
def signal_handler(sig, frm):
# Supress buffering of re-issued messages
self.handler._emit, self.handler.emit = self.handler.emit, lambda *a, **k: None
for msg in list(self.buffer): log.fatal(self.handler.format(msg))
self.handler.emit = self.handler._emit
signal.signal(self.conf.signal, signal_handler)