provide first simple logging implementation
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@1935 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
381ee37285
commit
d564ff0de1
5 changed files with 50 additions and 13 deletions
|
@ -307,8 +307,26 @@ Configuration
|
||||||
- log format(s)
|
- log format(s)
|
||||||
- log file(s) (or other forms of persistence)
|
- log file(s) (or other forms of persistence)
|
||||||
|
|
||||||
|
Example
|
||||||
|
-------
|
||||||
|
|
||||||
|
We set the logging configuration to log level 20 (INFO) using the
|
||||||
|
standard log handler that prints to ``sys.stdout``.
|
||||||
|
|
||||||
|
>>> agent.config.logging.standard = 20
|
||||||
>>> logger = agent.logger
|
>>> logger = agent.logger
|
||||||
|
>>> logger.setup()
|
||||||
|
|
||||||
|
The we can log an event providing a dictionary with the data to be logged.
|
||||||
|
|
||||||
>>> logger.log(dict(object='job', event='start'))
|
>>> logger.log(dict(object='job', event='start'))
|
||||||
|
20... event:start object:job
|
||||||
|
|
||||||
|
We can also look at the logging records collected in the logger.
|
||||||
|
|
||||||
|
>>> len(logger)
|
||||||
|
1
|
||||||
|
|
||||||
>>> print logger[-1]
|
>>> print logger[-1]
|
||||||
20... event:start object:job
|
20... event:start object:job
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ class Agent(object):
|
||||||
transportTypes = transportTypes
|
transportTypes = transportTypes
|
||||||
|
|
||||||
def __init__(self, conf=None):
|
def __init__(self, conf=None):
|
||||||
config = self.config = Configurator('ui', 'crawl', 'transport', 'logger')
|
config = self.config = Configurator('ui', 'crawl', 'transport', 'logging')
|
||||||
config.load(conf)
|
config.load(conf)
|
||||||
self.scheduler = Scheduler(self)
|
self.scheduler = Scheduler(self)
|
||||||
self.stopper = Stopper()
|
self.stopper = Stopper()
|
||||||
|
|
|
@ -98,6 +98,10 @@ class ILogger(Interface):
|
||||||
externalLoggers = Attribute('A collection of logger objects '
|
externalLoggers = Attribute('A collection of logger objects '
|
||||||
'to which the logging records should be written.')
|
'to which the logging records should be written.')
|
||||||
|
|
||||||
|
def setup():
|
||||||
|
""" Initialize the logger with the current configuration settings.
|
||||||
|
"""
|
||||||
|
|
||||||
def log(data):
|
def log(data):
|
||||||
""" Record the information given by the ``data`` argument
|
""" Record the information given by the ``data`` argument
|
||||||
(a mapping).
|
(a mapping).
|
||||||
|
@ -108,9 +112,6 @@ class ILogRecord(Interface):
|
||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
|
|
||||||
format = Attribute('An optional format string for rendering the '
|
|
||||||
'recorded information.')
|
|
||||||
|
|
||||||
def __str__():
|
def __str__():
|
||||||
""" Return a string representation suitable for writing on a
|
""" Return a string representation suitable for writing on a
|
||||||
log file.
|
log file.
|
||||||
|
@ -126,7 +127,7 @@ class ICrawlingJob(IScheduledJob):
|
||||||
|
|
||||||
def collect():
|
def collect():
|
||||||
""" Return a deferred that upon callback will provide a
|
""" Return a deferred that upon callback will provide a
|
||||||
collection of resource/metadata pairs that should be transferred
|
collection of resource objects that should be transferred
|
||||||
to the server.
|
to the server.
|
||||||
|
|
||||||
Use the selection criteria given to filter the resources that
|
Use the selection criteria given to filter the resources that
|
||||||
|
|
29
agent/log.py
29
agent/log.py
|
@ -22,6 +22,8 @@ Log information management.
|
||||||
$Id$
|
$Id$
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
from zope.interface import implements
|
from zope.interface import implements
|
||||||
|
|
||||||
|
@ -32,15 +34,15 @@ class LogRecord(object):
|
||||||
|
|
||||||
implements(ILogRecord)
|
implements(ILogRecord)
|
||||||
|
|
||||||
format = None
|
datefmt = '%Y-%m-%dT%H:%S'
|
||||||
timeFormat = '%Y-%m-%dT%H:%S'
|
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, logger, data):
|
||||||
|
self.logger = logger
|
||||||
self.data = data
|
self.data = data
|
||||||
self.timeStamp = time.time()
|
self.timeStamp = time.time()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
msg = [str(time.strftime(self.timeFormat, time.localtime(self.timeStamp)))]
|
msg = [str(time.strftime(self.datefmt, time.localtime(self.timeStamp)))]
|
||||||
for k in sorted(self.data):
|
for k in sorted(self.data):
|
||||||
msg.append('%s:%s' % (str(k), str(self.data[k])))
|
msg.append('%s:%s' % (str(k), str(self.data[k])))
|
||||||
return ' '.join(msg)
|
return ' '.join(msg)
|
||||||
|
@ -52,10 +54,23 @@ class Logger(list):
|
||||||
|
|
||||||
recordFactory = LogRecord
|
recordFactory = LogRecord
|
||||||
|
|
||||||
def __init__(self, agent, externalLoggers=[]):
|
|
||||||
|
def __init__(self, agent):
|
||||||
self.agent = agent
|
self.agent = agent
|
||||||
self.externalLoggers = externalLoggers
|
self.setup()
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
self.externalLoggers = []
|
||||||
|
conf = self.agent.config.logging
|
||||||
|
if conf.standard:
|
||||||
|
logger = logging.getLogger()
|
||||||
|
logger.level = conf.standard
|
||||||
|
logger.addHandler(logging.StreamHandler(sys.stdout))
|
||||||
|
self.externalLoggers.append(logger)
|
||||||
|
|
||||||
def log(self, data):
|
def log(self, data):
|
||||||
self.append(self.recordFactory(data))
|
record = self.recordFactory(self, data)
|
||||||
|
self.append(record)
|
||||||
|
for logger in self.externalLoggers:
|
||||||
|
logger.info(str(record))
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,10 @@ class Scheduler(object):
|
||||||
def __init__(self, agent):
|
def __init__(self, agent):
|
||||||
self.agent = agent
|
self.agent = agent
|
||||||
self.queue = {}
|
self.queue = {}
|
||||||
self.logger = None
|
|
||||||
|
@property
|
||||||
|
def logger(self):
|
||||||
|
return self.agent.logger()
|
||||||
|
|
||||||
def schedule(self, job, startTime=None):
|
def schedule(self, job, startTime=None):
|
||||||
if startTime is None:
|
if startTime is None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue