This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Copyright 2009 Vinay Sajip | |
# | |
import logging | |
logger = logging.getLogger(__name__) | |
class LogWriter: | |
def __init__(self, logger): | |
self.logger = logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import time | |
logger = logging.getLogger(__name__) | |
def useful(): | |
logger.debug('Hello from webapplib!') | |
time.sleep(0.01) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import time | |
logger = logging.getLogger(__name__) | |
def useful(): | |
logger.debug('Hello from webapplib!') | |
time.sleep(0.01) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import timeit | |
logger = logging.getLogger(__name__) | |
def log_noop(): | |
pass | |
def log_simple(): | |
logger.info("Testing") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logbook | |
import timeit | |
logger = logbook.Logger(__name__) | |
def log_noop(): | |
pass | |
def log_simple(): | |
logger.info("Testing") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer