pvl/syslog/rule.pp
changeset 75 2e675eda5e33
parent 74 952ee07efd7a
child 76 60bdff4bedfb
equal deleted inserted replaced
74:952ee07efd7a 75:2e675eda5e33
     1 class SyslogRule (SyslogFilter) :
       
     2     """
       
     3         A rule matches syslog lines, and formats them.
       
     4             
       
     5             tag         - apply given tag to matches
       
     6     """
       
     7         
       
     8     def __init__ (self, tag, program=None, pattern=None, format=None, flags=None, **opts) :
       
     9         if pattern and not isinstance(pattern, re.RegexObject) :
       
    10             pattern = re.compile(pattern, flags)
       
    11 
       
    12         super(SyslogRule, self).__init__(prog=program)
       
    13 
       
    14         self.tag = tag
       
    15         self.format = format
       
    16         self.pattern = pattern
       
    17 
       
    18     def apply (self, item) :
       
    19         """
       
    20             Apply rule against given item.
       
    21         """
       
    22         
       
    23         # filter
       
    24         match = self.filter(item)
       
    25 
       
    26         if not match :
       
    27             # ignore
       
    28             return None
       
    29         
       
    30         match = self.pattern.match(item['msg'])
       
    31 
       
    32         if not match :
       
    33             # ignore
       
    34             return None
       
    35         
       
    36         # apply
       
    37         item.update(match.groupdict())
       
    38 
       
    39         if self.tag is False :
       
    40             # drop
       
    41             return False
       
    42             
       
    43         if self.format :
       
    44             # return
       
    45             return self.tag, self.format.format(**item)
       
    46