[Python-Dev] Proposed change to logging (original) (raw)
Vinay Sajip vinay_sajip at yahoo.co.uk
Tue Aug 24 15:57:18 CEST 2004
- Previous message: [Python-Dev] SF Priorities, 2.4 release
- Next message: [Python-Dev] Proposed change to logging
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
An improvement to logging has been proposed by Miki Tebeka, which involves the creation of file-like wrapper objects for loggers. This makes it possible to use the print idiom for logging:
import logging
logging.basicConfig() logger = logging.getLogger("my.app") err = logger.getFileObject(logging.ERROR) dbg = logger.getFileObject(logging.DEBUG) ...
print >> err, "My logging message with %s" % "arguments" print >> dbg, "A message at %s level" % "debug"
Miki has helpfully sent in a patch (#1001864), and I propose to check it in by the 27th of August. I just wanted to get comments from python-dev first. There's one wart in the implementation - to avoid printing out blank lines caused by the "\n" appended by print, we ignore messages which are == "\n". This works only because print does a separate write for the "\n". Can anyone see a problem with this approach?
class LoggerFileObject: """ Simulate a file object for a given logging level. """ def init(self, logger, level): """ Initialize file object with logger and level """ self.logger = logger self.level = level self.closed = 0
def write(self, msg):
"""
Write message to log.
"""
if self.closed:
raise ValueError("I/O operation on closed
file") # Avoid printing blank lines. if msg != "\n": self.logger.log(self.level, msg)
def flush(self):
"""
Flush the file object. This flushes the
logger's handlers. """ if self.closed: return for handler in self.logger.handlers: handler.flush()
def close(self):
"""
Close file object.
Calling "write" after "close" will raise a
ValueError. """ self.closed = 1
Regards,
Vinay
___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com
- Previous message: [Python-Dev] SF Priorities, 2.4 release
- Next message: [Python-Dev] Proposed change to logging
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]