[Checkins] SVN: zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/ Changed the stats to use a formatted log message and omit time.

Jim Fulton jim at zope.com
Fri Sep 5 19:10:49 EDT 2008


Log message for revision 90910:
  Changed the stats to use a formatted log message and omit time.
  
  Added a monitor plugin.  Still need to document and implement the
  clear functionality.
  

Changed:
  U   zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/README.txt
  U   zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/__init__.py

-=-
Modified: zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/README.txt
===================================================================
--- zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/README.txt	2008-09-05 22:32:18 UTC (rev 90909)
+++ zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/README.txt	2008-09-05 23:10:49 UTC (rev 90910)
@@ -44,18 +44,21 @@
     >>> handler.start_time
     datetime.datetime(2008, 9, 5, 21, 10, 14)
 
-    >>> for level, count, time, message in handler.statistics:
-    ...     print level, count, time
+    >>> for level, count, message in handler.statistics:
+    ...     print level, count
     ...     print `message`
-    20 21 2008-09-05 21:11:01
+    20 21
     'yawn'
-    30 12 2008-09-05 21:10:40
+    30 12
     'hm'
-    40 9 2008-09-05 21:10:28
+    40 9
     'oops'
-    50 5 2008-09-05 21:10:19
+    50 5
     'Yipes'
 
+The statistics consist of the log level, the count of log messages,
+and the formatted text of last message.
+
 We can also ask it to clear it's statistics:
 
     >>> handler.clear()
@@ -63,12 +66,12 @@
     ...     logging.getLogger('foo').critical('Eek')
 
     >>> handler.start_time
-    datetime.datetime(2008, 9, 5, 21, 11, 2)
+    datetime.datetime(2008, 9, 5, 21, 10, 15)
 
-    >>> for level, count, time, message in handler.statistics:
-    ...     print level, count, time
+    >>> for level, count, message in handler.statistics:
+    ...     print level, count
     ...     print `message`
-    50 3 2008-09-05 21:11:05
+    50 3
     'Eek'
 
 There's ZConfig support for defining counting handlers:
@@ -88,6 +91,7 @@
     ...     name test
     ...     level INFO
     ...     <counter>
+    ...        format %(name)s %(message)s
     ...     </counter>
     ... </logger>
     ... """))
@@ -96,23 +100,72 @@
 
     >>> for i in range(2):
     ...     logging.getLogger('test').critical('Waaa')
+    >>> for i in range(22):
+    ...     logging.getLogger('test.foo').info('Zzzzz')
 
-    >>> for level, count, time, message in handler.statistics:
-    ...     print level, count, time
+    >>> for level, count, message in handler.statistics:
+    ...     print level, count
     ...     print `message`
-    50 5 2008-09-05 21:11:10
+    20 22
+    'Zzzzz'
+    50 5
     'Waaa'
 
-
-    >>> for level, count, time, message in testhandler.statistics:
-    ...     print level, count, time
+    >>> for level, count, message in testhandler.statistics:
+    ...     print level, count
     ...     print `message`
-    50 2 2008-09-05 21:11:09
-    'Waaa'
+    20 22
+    'test.foo Zzzzz'
+    50 2
+    'test Waaa'
 
+Note that the message output from the test handler reflects the format
+we used when we set it up. 
+
 The example above illustrates that you can install as many counting
 handlers as you want to.
 
+Monitor Plugin
+--------------
+
+The zc.monitorlogstats Monitor plugin can be used to query log statistics.
+
+    >>> import sys
+    >>> plugin = zc.monitorlogstats.monitor(sys.stdout)
+    2008-09-05T21:10:15
+    20 22 'Zzzzz'
+    50 5 'Waaa'
+
+The output consists of the start time and line for each log level for
+which there are statistics.  Each statistics line has the log level,
+entry count, and a repr of the last log message.
+
+By default, the root logger will be used. You can specify a logger name:
+
+    >>> plugin = zc.monitorlogstats.monitor(sys.stdout, 'test')
+    2008-09-05T21:10:16
+    20 22 'test.foo Zzzzz'
+    50 2 'test Waaa'
+
+You can use '.' for the root logger:
+
+    >>> plugin = zc.monitorlogstats.monitor(sys.stdout, '.')
+    2008-09-05T21:10:15
+    20 22 'Zzzzz'
+    50 5 'Waaa'
+
+Note that if there are multiple counting handlers for a logger, only
+the first will be used. (So don't define more than one. :)
+
+It is an error to name a logger without a counting handler:
+
+    >>> plugin = zc.monitorlogstats.monitor(sys.stdout, 'test.foo')
+    Traceback (most recent call last):
+    ...
+    ValueError: Invalid logger name: test.foo
+
+
+
 .. Cleanup:
 
     >>> logging.getLogger().removeHandler(handler)

Modified: zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/__init__.py
===================================================================
--- zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/__init__.py	2008-09-05 22:32:18 UTC (rev 90909)
+++ zc.monitorlogstats/branches/dev/src/zc/monitorlogstats/__init__.py	2008-09-05 23:10:49 UTC (rev 90910)
@@ -23,12 +23,11 @@
         levelno = record.levelno
         statistics = self._statitistics.get(levelno)
         if statistics is None:
-            statistics = [levelno, 0, None, None]
+            statistics = [levelno, 0, '']
             self._statitistics[levelno] = statistics
 
         statistics[1] += 1
-        statistics[2] = datetime.datetime.utcnow()
-        statistics[3] = record.getMessage()
+        statistics[2] = self.format(record)
 
     @property
     def statistics(self):
@@ -38,4 +37,17 @@
     def clear(self):
         self._statitistics = {}
         self.start_time = datetime.datetime.utcnow()
-    
+
+def monitor(f, loggername='.', clear=''):
+    if loggername == '.':
+        logger = logging.getLogger(None)
+    else:
+        logger = logging.getLogger(loggername)
+    for handler in logger.handlers:
+        if isinstance(handler, CountingHandler):
+            f.write(handler.start_time.isoformat('T')+'\n')
+            for record in handler.statistics:
+                f.write("%s %s %r\n" % record)
+            break
+    else:
+        raise ValueError("Invalid logger name: "+loggername)



More information about the Checkins mailing list