parent
7034b507a3
commit
27e05e257a
|
@ -2,15 +2,20 @@ import py
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
class File(object):
|
class File(object):
|
||||||
|
""" log consumer wrapping a file(-like) object
|
||||||
|
"""
|
||||||
def __init__(self, f):
|
def __init__(self, f):
|
||||||
assert hasattr(f, 'write')
|
assert hasattr(f, 'write')
|
||||||
assert isinstance(f, file) or not hasattr(f, 'open')
|
assert isinstance(f, file) or not hasattr(f, 'open')
|
||||||
self._file = f
|
self._file = f
|
||||||
|
|
||||||
def __call__(self, msg):
|
def __call__(self, msg):
|
||||||
|
""" write a message to the log """
|
||||||
print >>self._file, str(msg)
|
print >>self._file, str(msg)
|
||||||
|
|
||||||
class Path(object):
|
class Path(object):
|
||||||
|
""" log consumer able to write log messages into
|
||||||
|
"""
|
||||||
def __init__(self, filename, append=False, delayed_create=False,
|
def __init__(self, filename, append=False, delayed_create=False,
|
||||||
buffering=1):
|
buffering=1):
|
||||||
self._append = append
|
self._append = append
|
||||||
|
@ -25,17 +30,22 @@ class Path(object):
|
||||||
self._file = f
|
self._file = f
|
||||||
|
|
||||||
def __call__(self, msg):
|
def __call__(self, msg):
|
||||||
|
""" write a message to the log """
|
||||||
if not hasattr(self, "_file"):
|
if not hasattr(self, "_file"):
|
||||||
self._openfile()
|
self._openfile()
|
||||||
print >> self._file, msg
|
print >> self._file, msg
|
||||||
|
|
||||||
def STDOUT(msg):
|
def STDOUT(msg):
|
||||||
|
""" consumer that writes to sys.stdout """
|
||||||
print >>sys.stdout, str(msg)
|
print >>sys.stdout, str(msg)
|
||||||
|
|
||||||
def STDERR(msg):
|
def STDERR(msg):
|
||||||
|
""" consumer that writes to sys.stderr """
|
||||||
print >>sys.stderr, str(msg)
|
print >>sys.stderr, str(msg)
|
||||||
|
|
||||||
class Syslog:
|
class Syslog:
|
||||||
|
""" consumer that writes to the syslog daemon """
|
||||||
|
|
||||||
for priority in "LOG_EMERG LOG_ALERT LOG_CRIT LOG_ERR LOG_WARNING LOG_NOTICE LOG_INFO LOG_DEBUG".split():
|
for priority in "LOG_EMERG LOG_ALERT LOG_CRIT LOG_ERR LOG_WARNING LOG_NOTICE LOG_INFO LOG_DEBUG".split():
|
||||||
try:
|
try:
|
||||||
exec("%s = py.std.syslog.%s" % (priority, priority))
|
exec("%s = py.std.syslog.%s" % (priority, priority))
|
||||||
|
@ -48,10 +58,12 @@ class Syslog:
|
||||||
self.priority = priority
|
self.priority = priority
|
||||||
|
|
||||||
def __call__(self, msg):
|
def __call__(self, msg):
|
||||||
|
""" write a message to the log """
|
||||||
py.std.syslog.syslog(self.priority, str(msg))
|
py.std.syslog.syslog(self.priority, str(msg))
|
||||||
|
|
||||||
|
|
||||||
def setconsumer(keywords, consumer):
|
def setconsumer(keywords, consumer):
|
||||||
|
""" create a consumer for a set of keywords """
|
||||||
# normalize to tuples
|
# normalize to tuples
|
||||||
if isinstance(keywords, str):
|
if isinstance(keywords, str):
|
||||||
keywords = tuple(map(None, keywords.split()))
|
keywords = tuple(map(None, keywords.split()))
|
||||||
|
|
|
@ -61,9 +61,11 @@ class Logger(object):
|
||||||
setattr(self, name, Processor(self, name, dest))
|
setattr(self, name, Processor(self, name, dest))
|
||||||
|
|
||||||
def get(ident="global", **kwargs):
|
def get(ident="global", **kwargs):
|
||||||
|
""" return the Logger with id 'ident', instantiating if appropriate """
|
||||||
try:
|
try:
|
||||||
log = Logger._key2logger[ident]
|
log = Logger._key2logger[ident]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
log = Logger(ident)
|
log = Logger(ident)
|
||||||
log.ensure_sub(**kwargs)
|
log.ensure_sub(**kwargs)
|
||||||
return log
|
return log
|
||||||
|
|
||||||
|
|
|
@ -49,11 +49,18 @@ class Producer(object):
|
||||||
return producer
|
return producer
|
||||||
|
|
||||||
def __call__(self, *args):
|
def __call__(self, *args):
|
||||||
|
""" write a message to the appropriate consumer(s) """
|
||||||
func = self.get_consumer(self.keywords)
|
func = self.get_consumer(self.keywords)
|
||||||
if func is not None:
|
if func is not None:
|
||||||
func(self.Message(self.keywords, args))
|
func(self.Message(self.keywords, args))
|
||||||
|
|
||||||
def get_consumer(self, keywords):
|
def get_consumer(self, keywords):
|
||||||
|
""" return a consumer matching keywords
|
||||||
|
|
||||||
|
tries to find the most suitable consumer by walking, starting from
|
||||||
|
the back, the list of keywords, the first consumer matching a
|
||||||
|
keyword is returned (falling back to py.log.default)
|
||||||
|
"""
|
||||||
for i in range(len(self.keywords), 0, -1):
|
for i in range(len(self.keywords), 0, -1):
|
||||||
try:
|
try:
|
||||||
return self.keywords2consumer[self.keywords[:i]]
|
return self.keywords2consumer[self.keywords[:i]]
|
||||||
|
@ -62,6 +69,7 @@ class Producer(object):
|
||||||
return self.keywords2consumer.get('default', default_consumer)
|
return self.keywords2consumer.get('default', default_consumer)
|
||||||
|
|
||||||
def set_consumer(self, consumer):
|
def set_consumer(self, consumer):
|
||||||
|
""" register a consumer matching our own keywords """
|
||||||
self.keywords2consumer[self.keywords] = consumer
|
self.keywords2consumer[self.keywords] = consumer
|
||||||
|
|
||||||
default = Producer('default')
|
default = Producer('default')
|
||||||
|
@ -74,6 +82,7 @@ def _setstate(state):
|
||||||
Producer.keywords2consumer.update(state)
|
Producer.keywords2consumer.update(state)
|
||||||
|
|
||||||
def default_consumer(msg):
|
def default_consumer(msg):
|
||||||
|
""" the default consumer, prints the message to stdout (using 'print') """
|
||||||
print str(msg)
|
print str(msg)
|
||||||
|
|
||||||
Producer.keywords2consumer['default'] = default_consumer
|
Producer.keywords2consumer['default'] = default_consumer
|
||||||
|
|
Loading…
Reference in New Issue