X

Geertjan's Blog

  • November 17, 2005

Readable Log Files in NetBeans IDE 5.0

Geertjan Wielenga
Product Manager
First, a picture of my log file:

If you want your log files to look like this too (i.e, if you want XML instead of text in your log files), read on. The above screenshot is the result of JULI (java.util.logging) logging in Tomcat 5.5.9, which is bundled with NetBeans IDE 5.0. Before going further, a little bit of background reading on JULI:

So, this is how to set things up so that the IDE generates JULI log files in XML:

  1. Make your log files a bit more accessible. Right-click your project node, choose Properties, and then, in the Sources panel, add a new source root that points to Tomcat's logs folder in the NetBeans user directory. Note that the logs folder and its parent is created only when you start the server.

  2. Set your logging properties. By default, with JULI logging support in Tomcat 5.5.9 in NetBeans IDE 5.0, java.util.logging.Logger file output goes to this file:

    $catalina_base/logs/localhost-YYYY-MM-DD.log

    Server output goes to this file:

    $catalina_base/logs/catalina-YYYY-MM-DD.log

    Both of these files open in separate tabs in the Output window when you deploy to Tomcat 5.5.9. (And there are two pop-up menu items on the Tomcat Web Server node in the Runtime window, one is called 'View Server Log' and the other is called 'View Server Output'.) However, you can modify a lot of things in a file that must be called logging.properties, which must be found in the default package:

    So now we're going to add key-value pairs to the logging.properties file. We want the Output window (handled by java.util.logging.ConsoleHandler) to contain XML instead of text. And we want the content of the server log file (handled by org.apache.juli.FileHandler) to be reformatted in XML too. Here are my key-value pairs:

    #------------------------------------------------------------------------
    #- Global Settings
    #------------------------------------------------------------------------
    handlers=org.apache.juli.FileHandler,java.util.logging.ConsoleHandler
    .level=INFO
    org.apache.juli.FileHandler.level = FINEST
    org.apache.juli.FileHandler.directory = ${catalina.base}/logs
    org.apache.juli.FileHandler.prefix = localhost.
    org.apache.juli.FileHandler.limit = 5242880
    org.apache.juli.FileHandler.count = 5
    org.apache.juli.FileHandler.formatter = java.util.logging.XMLFormatter
    java.util.logging.ConsoleHandler.level = FINEST
    java.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter
    #------------------------------------------------------------------------
    #- Classes
    #------------------------------------------------------------------------
    mypackage.mysubpackage.level=FINEST

Note: The above will generate XML tags for the localhost-YYYY-MM-DD.log file (with a maximum size of 5242880, after which it will roll to another file, creating up to 5 rolled log files). It's very important to realize that the only server log file that opens in the Output window is localhost-YYYY-MM-DD.log, therefore you cannot (in NetBeans IDE 5.0) set a different prefix (if you want the file to open in the Output window). This is a known limitation that prevents you from creating a different server log file per application (if you want the file to open in the Output window).

Finally, by adding this line, you change the suffix (i.e., the extension) from .log to .xml, which lets you open the log file in the IDE's XML editor (where you can make use of some of this editor's cool features, such as syntax highlighting and code folding as shown in the screenshot at the start of this blog entry):

org.apache.juli.FileHandler.suffix = .xml

However, note that if you change the suffix, then the server log will not be opened in a separate tab in the Output window anymore, because the only log file that opens in the separate Output window tab (apart from the server output file) is the one that is called localhost-YYYY-MM-DD.log (i.e., with a .log suffix). However, I've found that in this case the server log file outputs to the same tab where the server output is displayed. Therefore, even though one of the two tabs remains empty, the other tab seems to contain the output of both the server output and the log file (in XML). So, finally, this is what my new source root looks like after I deployed my application to Tomcat 5.5.9:

When you're trying it out, here's a scriptlet that you can put in a JSP page:

<%
// this goes into the server I/O
java.util.logging.Logger log = java.util.logging.Logger.getAnonymousLogger();
log.warning("my warning message");
log.log(java.util.logging.Level.SEVERE, "My NPE", new NullPointerException());
// this goes into the log file
log("Standard servlet logging");
%>

When you add the above to a new web application's index.jsp, and then deploy the application using the logging.properties file shown above, a localhost-YYYY-MM-DD.xml with the following content will be generated:

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.