Back to index

plone3  3.1.7
logger.py
Go to the documentation of this file.
00001 import logging
00002 
00003 from OFS.SimpleItem import SimpleItem
00004 from persistent import Persistent 
00005 
00006 from zope.interface import implements, Interface
00007 from zope.component import adapts
00008 from zope.formlib import form
00009 from zope import schema
00010 
00011 from zope.component.interfaces import IObjectEvent
00012 
00013 from plone.contentrules.rule.interfaces import IExecutable, IRuleElementData
00014 
00015 from plone.app.contentrules.browser.formhelper import AddForm, EditForm 
00016 
00017 from Products.CMFPlone import PloneMessageFactory as _
00018 
00019 logger = logging.getLogger("plone.contentrules.logger")
00020 handler = logging.StreamHandler()
00021 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s -  %(message)s")
00022 handler.setFormatter(formatter)
00023 logger.addHandler(handler)
00024 
00025 class ILoggerAction(Interface):
00026     """Interface for the configurable aspects of a logger action.
00027     
00028     This is also used to create add and edit forms, below.
00029     """
00030     
00031     targetLogger = schema.ASCIILine(title=_(u"Logger name"),
00032                                     default="rule_log")
00033                                    
00034     loggingLevel = schema.Int(title=_(u"Logging level"), 
00035                               default=1000)
00036                               
00037     message = schema.TextLine(title=_(u"Message"),
00038                                     description=u"&e = the triggering event, &c = the context",
00039                                     default=u"Caught &e at &c")
00040          
00041 class LoggerAction(SimpleItem):
00042     """The actual persistent implementation of the logger action element.
00043     
00044     Note that we must mix in Explicit to keep Zope 2 security happy.
00045     """
00046     implements(ILoggerAction, IRuleElementData)
00047     
00048     targetLogger = ''
00049     loggingLevel = ''
00050     message = ''
00051 
00052     element = 'plone.actions.Logger'
00053     
00054     @property
00055     def summary(self):
00056         return _(u"Log message ${message}", mapping=dict(message=self.message))
00057 
00058 class LoggerActionExecutor(object):
00059     """The executor for this action.
00060     
00061     This is registered as an adapter in configure.zcml
00062     """
00063     implements(IExecutable)
00064     adapts(Interface, ILoggerAction, Interface)
00065          
00066     def __init__(self, context, element, event):
00067         self.context = context
00068         self.element = element
00069         self.event = event
00070 
00071     def __call__(self):
00072         logger = logging.getLogger(self.element.targetLogger)
00073         processedMessage = self.element.message.replace("&e", "%s.%s" % (self.event.__class__.__module__, self.event.__class__.__name__))
00074         if IObjectEvent.providedBy(self.event):
00075             processedMessage = processedMessage.replace("&c", repr(self.event.object))
00076         logger.log(self.element.loggingLevel, processedMessage)
00077         return True 
00078         
00079 class LoggerAddForm(AddForm):
00080     """An add form for logger rule actions.
00081     """
00082     form_fields = form.FormFields(ILoggerAction)
00083     label = _(u"Add Logger Action")
00084     description = _(u"A logger action can output a message to the system log.")
00085     form_name = _(u"Configure element")
00086     
00087     def create(self, data):
00088         a = LoggerAction()
00089         form.applyChanges(a, self.form_fields, data)
00090         return a
00091 
00092 class LoggerEditForm(EditForm):
00093     """An edit form for logger rule actions.
00094     
00095     Formlib does all the magic here.
00096     """
00097     form_fields = form.FormFields(ILoggerAction)
00098     label = _(u"Edit Logger Action")
00099     description = _(u"A logger action can output a message to the system log.")
00100     form_name = _(u"Configure element")