Skip to content

Instantly share code, notes, and snippets.

Avatar

Vinay Sajip vsajip

View GitHub Profile
View logwriter
#
# Copyright 2009 Vinay Sajip
#
import logging
logger = logging.getLogger(__name__)
class LogWriter:
def __init__(self, logger):
self.logger = logger
View bufferingsmtphandler.py
import logging.handlers
import smtplib
class BufferingSMTPHandler(logging.handlers.BufferingHandler):
def __init__(self, mailhost, fromaddr, toaddrs, subject, capacity):
logging.handlers.BufferingHandler.__init__(self, capacity)
self.mailhost = mailhost
self.mailport = None
self.fromaddr = fromaddr
if isinstance(toaddrs, (str, unicode)):
View pct2fmt.py
#
# Copyright (C) 2009 Vinay Sajip.
# Licensed under the PSF licence.
# Exploratory code to convert %-format strings to {}-format.
#
import re
PATTERN = re.compile(r'''
% # specifier start
View zmqlog.py
import json
import logging
import zmq
class QueueHandler(logging.Handler):
"""
This handler sends events to a queue. Typically, it would be used together
with a multiprocessing Queue to centralise logging to file in one process
(in a multi-process application), so as to avoid file write contention
between processes.
View webapplib.py
import logging
import time
logger = logging.getLogger(__name__)
def useful():
logger.debug('Hello from webapplib!')
time.sleep(0.01)
View webapplib.py
import logging
import time
logger = logging.getLogger(__name__)
def useful():
logger.debug('Hello from webapplib!')
time.sleep(0.01)
View time_logging.py
import logging
import timeit
logger = logging.getLogger(__name__)
def log_noop():
pass
def log_simple():
logger.info("Testing")
View time_logbook.py
import logbook
import timeit
logger = logbook.Logger(__name__)
def log_noop():
pass
def log_simple():
logger.info("Testing")
View gist:586637
vinay@zeta-lucid:~/projects/scratch$ python time_logging.py
log_noop 0.20 microseconds
log_simple 84.81 microseconds
log_filtered 6.17 microseconds
log_mitigated 5.72 microseconds
log_disabled 1.21 microseconds
No caller, thread, process info...
log_simple 75.38 microseconds
log_filtered 5.98 microseconds
log_mitigated 6.03 microseconds
View lbmod.py
If I run the latter script, noting that I want only WARNING and more severe messages in the log, then test.log is perhaps as expected:
[2010-09-16 09:31] WARNING: lbprivate: Warning
[2010-09-16 09:31] ERROR: lbprivate: Error
[2010-09-16 09:31] CRITICAL: lbprivate: Critical
but I get spurious output on the console:
Keep this line with the next, no gaps wanted
[2010-09-16 09:31] DEBUG: lbprivate: Debug