Date: 2003-12-08T11:21:14
Editor: 81.225.9.144 <>
Wiki: Apache Avalon Wiki
Page: AvalonNoLogging
Fixed some code formatting issues.
Change Log:
------------------------------------------------------------------------------
@@ -35,23 +35,23 @@
If you have a piece of code, and you have some log statements in it:
-{{{ FileInputStream input = new FileInputStream( myFile ); }}}
-{{{ getLogger().debug( "Opened " + myFile.getPath() + " successfully."); }}}
+{{{ FileInputStream input = new FileInputStream( myFile );
+getLogger().debug( "Opened " + myFile.getPath() + " successfully.");
-{{{ ... do stuff ... }}}
+... do stuff ...
-{{{ input.close(); }}}
-{{{ getLogger().debug( "Closed " + myFile.getPath() + "."); }}}
+input.close();
+getLogger().debug( "Closed " + myFile.getPath() + "."); }}}
You can replace that with a listener that you call at the same points:
-{{{ FileInputStream input = new FileInputStream( myFile ); }}}
-{{{ listener.onOpen( myFile ); }}}
+{{{ FileInputStream input = new FileInputStream( myFile );
+listener.onOpen( myFile );
-{{{ ... do stuff ... }}}
+... do stuff ...
-{{{ input.close(); }}}
-{{{ listener.onClose( myFile ); }}}
+input.close();
+listener.onClose( myFile ); }}}
The listener can then output the name of the method being called, and the arguments
to it. Usually, instead of outputting {{{ onOpen,/home/user/myfile }}}, it is more
user friendly to output {{{ Opened /home/user/myfile }}} - as we said, logging is for
humans.
@@ -63,35 +63,35 @@
Look at the last statement. What if logging had been implemented in the
java.lang.String class. You open up your server logs and see this:
-{{{ ["DEBUG"] Creating new instance of String class. }}}
-{{{ ["DEBUG"] String.charAt() called with index 0 }}}
-{{{ ["DEBUG"] String.charAt() called with index 1 }}}
-{{{ ["DEBUG"] String.charAt() called with index 2 }}}
-{{{ ["DEBUG"] String.charAt() called with index 3 }}}
-{{{ ["DEBUG"] String.charAt() called with index 4 }}}
-{{{ ["DEBUG"] String.charAt() called with index 5 }}}
-{{{ ["DEBUG"] String.charAt() called with index 6 }}}
-{{{ ["DEBUG"] String.charAt() called with index 7 }}}
-{{{ ["ERROR"] String.charAt() called with invalid index 8 }}}
-{{{ ["DEBUG"] Throwing ArrayIndexOutOfBoundsException }}}
+{{{ ["DEBUG"] Creating new instance of String class.
+["DEBUG"] String.charAt() called with index 0
+["DEBUG"] String.charAt() called with index 1
+["DEBUG"] String.charAt() called with index 2
+["DEBUG"] String.charAt() called with index 3
+["DEBUG"] String.charAt() called with index 4
+["DEBUG"] String.charAt() called with index 5
+["DEBUG"] String.charAt() called with index 6
+["DEBUG"] String.charAt() called with index 7
+["ERROR"] String.charAt() called with invalid index 8
+["DEBUG"] Throwing ArrayIndexOutOfBoundsException }}}
Imagine the size of that log file. Big doesn't adequately describe it. However, if
you are testing your new String class, you may well find it useful to output debugging
information. The issue here is that you have logging at too fine a granularity. This
causes not only headaches in terms of log file size, but also of API concerns: How do
you provide a logger to a String? Will you have to pass it in a constructor?
-{{{ String myString = new String( "myText", myLogger ); }}}
+{{{ String myString = new String( "myText", myLogger ); }}}
Or will all strings get a logger via static accessor methods, and log to the same
category?
-{{{ public final class String { }}}
+{{{ public final class String {
-{{{ private final static stringLogger = Logger.getLogger( "java.lang.String"
); }}}
+ private final static stringLogger = Logger.getLogger( "java.lang.String" );
-{{{ public String( char[] text ) { }}}
-{{{ stringLogger.debug( "Creating new instance of String." ); }}}
-{{{ ... }}}
-{{{ } }}}
+ public String( char[] text ) {
+ stringLogger.debug( "Creating new instance of String." );
+ ...
+ }
-{{{ ... }}}
-{{{ } }}}
+ ...
+} }}}
If the latter, are you sure that'll always be the right thing to do?
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]