[Logging-commits] r29 - pkg/R www

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Apr 9 12:11:37 CEST 2010


Author: mariotomo
Date: 2010-04-09 12:11:36 +0200 (Fri, 09 Apr 2010)
New Revision: 29

Modified:
   pkg/R/logger.R
   www/additions.css
   www/index.php
   www/sample_session.html
Log:
adding the formatting explaination.


Modified: pkg/R/logger.R
===================================================================
--- pkg/R/logger.R	2010-04-08 15:14:34 UTC (rev 28)
+++ pkg/R/logger.R	2010-04-09 10:11:36 UTC (rev 29)
@@ -28,8 +28,8 @@
 ##
 
 ## TODO: these constants must be exported and documented
-loglevels <- c(0, 1, 4, 7, 10, 20, 30, 40, 50, 50)
-names(loglevels) <- c('NOTSET', 'FINEST', 'FINER', 'FINE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL', 'FATAL')
+loglevels <- c(0, 1, 4, 7, 10, 20, 30, 30, 40, 50, 50)
+names(loglevels) <- c('NOTSET', 'FINEST', 'FINER', 'FINE', 'DEBUG', 'INFO', 'WARNING', 'WARN', 'ERROR', 'CRITICAL', 'FATAL')
 
 namedLevel <- function(value)
   UseMethod('namedLevel')

Modified: www/additions.css
===================================================================
--- www/additions.css	2010-04-08 15:14:34 UTC (rev 28)
+++ www/additions.css	2010-04-09 10:11:36 UTC (rev 29)
@@ -13,6 +13,10 @@
        line-height: 18px;
        font-weight: normal!important;
 }
+R-global {color: #00f;}
+R-reserved {color: #a0a;}
+R-constant, R-operator {color: #090;}
+R-string {color: #a44;}
 user { font-weight: bold }
 h3, h4, p { font-family: helvetica, sans-serif;} 
 p { line-height: 20pt;

Modified: www/index.php
===================================================================
--- www/index.php	2010-04-08 15:14:34 UTC (rev 28)
+++ www/index.php	2010-04-09 10:11:36 UTC (rev 29)
@@ -59,12 +59,14 @@
 
 <p>This package owes a lot to 
 <ul>
-<li>Brian Lee Yung Rowe's futile package, </li>
-<li>the stackoverflow community </li>
-<li>and the documentation of the Python logging package.</li>
+<li>Brian Lee Yung Rowe's futile package (v1.1), </li>
+<li>the stackoverflow community, </li>
+<li>the documentation of the Python logging package.</li>
 </ul></p>
 
 <p>The <strong>project summary page</strong> you can find <a href="http://<?php echo $domain; ?>/projects/<?php echo $group_name; ?>/"><strong>here</strong></a>. </p>
 
+<p>The <strong>released packages</strong> you can find <a href="http://r-forge.r-project.org/R/?group_id=672"><strong>here</strong></a>. </p>
+
 </body>
 </html>

Modified: www/sample_session.html
===================================================================
--- www/sample_session.html	2010-04-08 15:14:34 UTC (rev 28)
+++ www/sample_session.html	2010-04-09 10:11:36 UTC (rev 29)
@@ -49,7 +49,7 @@
 [1] "basic.stdout"<br/>
 R> <user>loginfo('does it work?')</user><br/>
 2010-04-08 11:28:35 INFO::does it work?<br/>
-R> <user>logwarn('%s %d', 'my name is', 5)</user><br/>
+R> <user>logwarn('my %s is %d', 'name', 5)</user><br/>
 2010-04-08 11:28:48 WARN::my name is 5<br/>
 R> <user>logdebug('I am a silent child')</user><br/>
 R>
@@ -100,10 +100,12 @@
 <h4>hierarchical loggers</h4>
 
 <p>in the previous section we have worked -implicitly- with one
-logger, the root logger.  we can refer to it explicitly: its name is
-the empty string.  this also explains that "::" in the messages sent
-to the console, between the first and the second ":" there's the name
-of the logger that is associated to the log record shown.</p>
+logger, the root logger.  we can refer to it explicitly by specifying
+the 'logger' parameter in our function calls.  the name of the root
+logger is the empty string.  this also explains that "::" in the
+messages sent to the console, between the first and the second ":"
+there's the name of the logger that is associated to the log record
+shown.</p>
 
 <code>
 R> <user>with(getLogger(logger=''), names(handlers))</user><br/>
@@ -208,5 +210,70 @@
 
 <h4>formatting your log records</h4>
 
+<p>in this session we are going to see how to generate a diagnostics
+file for a system that organizes logrecords in a different way than
+Python.  let's jump into the implementation, if you can write R you
+surely won't need more explaination but will want to tell me how to
+make this function faster, more readable, shorter...</p>
+
+<code>
+<R-global>formatter.fewsdiagnostics</> <R-operator><-</> <R-reserved>function</>(record) {<br/>
+&nbsp; <R-reserved>if</>(record$level <= loglevels[[<R-string>'INFO'</>]])<br/>
+&nbsp; &nbsp; level <R-operator><-</> 3<br/>
+&nbsp; <R-reserved>else if</>(record$level <= loglevels[[<R-string>'WARNING'</>]])<br/>
+&nbsp; &nbsp; level <R-operator><-</> 2<br/>
+&nbsp; <R-reserved>else if</>(record$level <= loglevels[[<R-string>'ERROR'</>]])<br/>
+&nbsp; &nbsp; level <R-operator><-</> 1<br/>
+&nbsp; <R-reserved>else</><br/>
+&nbsp; &nbsp; level <R-operator><-</> 0<br/>
+<br/>
+&nbsp; sprintf(<R-string>'&nbsp; &lt;line level="%d" description="LizardScripter :: %s :: %s"/>\n'</>, level, record$timestamp, record$msg)<br/>
+}
+</code>
+
+<p>notice that the field <tt>$msg</tt> of a record is already
+"formatted", as we have seen with <tt>logwarn('my %s is %d', 'name',
+5)</tt>.  that part can be used but not undone any more.  the
+formatter you can associate to a handler can combine the tags in the
+logrecord to produce a string.</p>
+
+<p>when you add a handler to a logger, you can use
+the <tt>formatter</tt> parameter to associate to the handler a
+function that takes a logrecord and returns a string.  the above
+example function is such a function.  </p>
+
+
+<p>if you don't specify the <tt>formatter</tt> parameter, the default
+formatter is used, which looks like this:</p>
+
+<code>
+<R-global>defaultFormat</> <R-operator><-</> <R-reserved>function</>(record) {<br/>
+&nbsp; text <R-operator><-</> paste(record$timestamp, paste(record$levelname, record$logger, record$msg, sep=<R-string>':'</>))<br/>
+}
+</code>
+
+<p>the rest of the code, just slightly simplified, showing how we (me
+at my company) actually use this capability is this:</p>
+
+<code>
+<R-global>setup.fewsdiagnostics</> <R-operator><-</> <R-reserved>function</>(filename) {<br/>
+&nbsp; cat(<R-string>'&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n'</>, file=filename, append=<R-constant>FALSE</>)<br/>
+&nbsp; cat(<R-string>'&lt;Diag version="1.2" xmlns="..." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="...">\n'</>, file=filename, append=<R-constant>FALSE</>)<br/>
+&nbsp; addHandler(<R-string>'diagnostics'</>,<br/>
+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  writeToFile, file=filename,<br/>
+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  logger=<R-string>'fews.diagnostics'</>,<br/>
+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  formatter=formatter.fewsdiagnostics)<br/>
+}<br/>
+<br/>
+<R-global>teardown.fewsdiagnostics</> <R-operator><-</> <R-reserved>function</>(filename) {<br/>
+&nbsp; cat(<R-string>'&lt;/Diag>\n'</>, file=filename, append=<R-constant>TRUE</>)<br/>
+&nbsp; removeHandler(<R-string>'diagnostics'</>, logger=<R-string>'fews.diagnostics'</>)<br/>
+}
+</code>
+
+<p></p>
+<p></p>
+<p></p>
+
 </body>
 </html>



More information about the Logging-commits mailing list