#2545 util::FileLogger

SlimerDude Tue 21 Jun 2016

Hi,

util::FileLogger is used for, um, logging to a file!

It usually works great, but this morning I was in a situation where I couldn't open an OutStream for the file. At that point my console was flooded with NPE stacktraces and I couldn't make head nor tail of anything.

The following patch prevents the NPEs from spamming the screen, but I imagine there's a more elegant way of achieving the same.

diff -r f5e659e8f7c7 src/util/fan/FileLogger.fan
--- a/src/util/fan/FileLogger.fan	Wed Apr 20 14:01:49 2016 -0400
+++ b/src/util/fan/FileLogger.fan	Tue Jun 21 19:48:56 2016 +0100
@@ -79,16 +79,20 @@
         Actor.locals["state"] = state = FileLoggerState(this)
 
       // append to current file
-      if (msg is LogRec)
+      out := state.out
+      if (out != null)
       {
-        rec := (LogRec)msg
-        state.out.printLine(rec)
-        if (rec.err != null) rec.err.trace(state.out)
-        state.out.flush
-      }
-      else
-      {
-        state.out.printLine(msg).flush
+        if (msg is LogRec)
+        {
+          rec := (LogRec) msg
+          out.printLine(rec)
+          if (rec.err != null) rec.err.trace(out)
+          out.flush
+        }
+        else
+        {
+          out.printLine(msg).flush
+        }
       }
     }
     catch (Err e)
@@ -117,7 +121,7 @@
       open(dir + filename.toUri)
   }
 
-  OutStream out()
+  OutStream? out()
   {
     // check if we need to open a new file
     if (pattern != null && DateTime.now.toLocale(pattern) != curPattern)
@@ -138,13 +142,14 @@
     return curOut
   }
 
-  OutStream open(File curFile)
+  OutStream? open(File curFile)
   {
     this.curOut = curFile.out(true)
     try
-      logger.onOpen?.call(this.curOut)
+      if (this.curOut != null)
+        logger.onOpen?.call(this.curOut)
     catch (Err e)
-      curOut.printLine("ERROR: FileLogger.onOpen\n${e.traceToStr}")
+      curOut?.printLine("ERROR: FileLogger.onOpen\n${e.traceToStr}")
     return this.curOut
   }

brian Mon 11 Jul 2016

I don't think that is the right way to fix it - I pushed a change to just note the error to stdout and then use a nil output stream that just ignores writes so that you just get your normal stdout logging

Login or Signup to reply.