[Logging-commits] r24 - pkg/R www

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Apr 8 15:52:28 CEST 2010


Author: mariotomo
Date: 2010-04-08 15:52:28 +0200 (Thu, 08 Apr 2010)
New Revision: 24

Modified:
   pkg/R/logger.R
   www/sample_session.html
Log:
corrected and tested the writeToFile commodity function.


Modified: pkg/R/logger.R
===================================================================
--- pkg/R/logger.R	2010-04-08 12:52:41 UTC (rev 23)
+++ pkg/R/logger.R	2010-04-08 13:52:28 UTC (rev 24)
@@ -31,7 +31,16 @@
 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')
 
-namedLevel <- function(value) {
+namedLevel <- function(value)
+  UseMethod('namedLevel')
+
+namedLevel.character <- function(value) {
+  position <- which(names(loglevels) == value)
+  if(length(position) == 1)
+    loglevels[position]
+}
+
+namedLevel.numeric <- function(value) {
   if(is.null(names(value))) {
     position <- which(loglevels == value)
     if(length(position) == 1)
@@ -203,12 +212,12 @@
 
 writeToFile <- function(msg, handler)
 {
-  if (! 'file' %in% names(handler))
+  if (!exists('file', envir=handler))
   {
     cat("handler with writeToFile 'action' must have a 'file' element.\n")
     return()
   }
-  cat(paste(msg, '\n', sep=''), file=handler$file, append=TRUE)
+  cat(paste(msg, '\n', sep=''), file=with(handler, file), append=TRUE)
 }
 
 #################################################################################
@@ -221,8 +230,8 @@
 
 #################################################################################
 
-basicConfig <- function() {
-  updateOptions('', level=loglevels['INFO'])
+basicConfig <- function(level=20) {
+  updateOptions('', level=namedLevel(level))
   addHandler('basic.stdout', writeToConsole)
   invisible()
 }
@@ -247,6 +256,7 @@
 {
   name <- handler # parameter 'handler' identifies the name
   handler <- new.env()
+  updateOptions.environment(handler, ...)
   assign('level', namedLevel(level), handler)
   assign('action', action, handler)
   assign('formatter', formatter, handler)

Modified: www/sample_session.html
===================================================================
--- www/sample_session.html	2010-04-08 12:52:41 UTC (rev 23)
+++ www/sample_session.html	2010-04-08 13:52:28 UTC (rev 24)
@@ -40,7 +40,7 @@
 
 <h4>the basics</h4>
 
-<p>in this session we work with one single logger, the root logger, and we use only console handlers</p>
+<p>in this session we work with one single logger, the root logger, and we use only console handlers.</p>
 
 <p>start up R, load the library, use the basic configuration.</p>
 <code>
@@ -169,10 +169,37 @@
 
 <h4>logging to file</h4>
 
-<B>TODO</B>
-use the exported commodity function <tt>writeToFile</tt>.  you specify the name
-of the file as an extra parameter <tt>file</tt> to the function <tt>addHandler</tt>.
+<p>actually the name of this paragraph is misleading.  a more correct
+name would be <em>handling to file</em>, since it's a handler which is
+going to send some representation of your logrecords to a file.  </p>
 
+<p>to make sure log records are sent to a file, you choose a logger
+and attach to it a handler with action a function that writes to your
+file.  the logging package exports the commodity
+function <tt>writeToFile</tt> for this purpouse.  the name of the file
+is given as an extra parameter in the call to <tt>addHandler</tt>.</p>
+
+<code>
+R> <user>logReset()</user><br/>
+R> <user>basicConfig()</user><br/>
+R> <user>addHandler(writeToFile, file="~/testing.log", level='DEBUG')</user><br/>
+R> <user>with(getLogger(), names(handlers))</user><br/>
+[1] "basic.stdout"   "writeToFile"<br/>
+R> <user>loginfo('test %d', 1)</user><br/>
+2010-04-07 11:31:06 INFO::test 1<br/>
+R> <user>logdebug('test %d', 2)</user><br/>
+R> <user>logwarn('test %d', 3)</user><br/>
+2010-04-07 11:31:15 WARN::test 3<br/>
+R> 
+</code>
+
+<p>if the file was not existing or empty, this would be its content after the above steps:</p>
+<code>
+2010-04-07 11:31:06 INFO::test 1<br/>
+2010-04-07 11:31:11 DEBUG::test 2<br/>
+2010-04-07 11:31:15 WARN::test 3<br/>
+</code>
+
 <h4>formatting your log records</h4>
 
 </body>



More information about the Logging-commits mailing list