View comments | RSS feed

Customizing the logging configuration

JRun lets you customize and extend the logging configuration, as follows:

Using level-specific log files

The file writer can write to multiple files, based on message level. This makes it easy to isolate messages by type. To enable multiple-file logging, add {log.level} to the FileLogEventHandler service's filename attribute, as the following example shows:

<service class="jrunx.logger.FileLogEventHandler" name="FileLogEventHandler">
  <attribute name="filename">{jrun.rootdir}/logs/{jrun.server.name}-{log.level}-event.log</attribute>
  <attribute name="rotationSize">200000</attribute>
  <attribute name="rotationFiles">3</attribute>
  <attribute name="heading"># Created by JRun on {date MM/dd HH:mm:ss}</attribute>
  <attribute name="closeDelay">5000</attribute>
��<attribute name="deleteOnExit">false</attribute>
</service>

For example, when using level-specific log files, the default JRun server writes the following log files:

Using the console writer

Directing log output to the screen can be useful during the development and testing phases of a project. In addition to viewing the JRun debug messages, you can add log methods to the code and view them in the console window in real time.

This section explains how to use the console log writer to direct log messages to a console window. It provides separate instructions for Windows and Unix.

Windows

To enable the console log writer, perform the following steps:

  1. Stop the JRun server.

    To use the console log writer, you must run JRun as a application. The console log writer does not work when running JRun as a Windows service.

  2. Open the jrun.xml file of the JRun server for which you want to enable the console log writer.
  3. Uncomment the lines for the ConsoleLogEventHandler service, located inside the ThreadedLogEventHandler service.
  4. Save the jrun.xml file.
  5. Restart the JRun server as an application.

    The console displays log entries.

Unix/Linux

To enable the console log writer, perform the following steps:

  1. Stop the JRun server.
  2. Open the jrun.xml file of the JRun server for which you want to enable the console log writer.
  3. Uncomment the lines for the ConsoleLogEventHandler service, located inside the ThreadedLogEventHandler service.
  4. Save the jrun.xml file.
  5. Restart the JRun server.

    The console displays log entries.

Defining a customized logging mechanism

The JRun logging architecture lets you write customized log event handlers. For example, you might create a log event handler that writes messages in a site-specific format or to a database. You can also use this capability to write log messages using an alternative logging API, such as log4j or the JDK 1.4 java.util.logging package.

To create a customized log event handler, you must perform the following steps:

Coding an MBean interface

The MBean interface, which is required as part of the JRun JMX-based architecture, defines methods that correlate to properties in the jrun.xml file. When you specify values for these properties in the jrun.xml file, JRun initializes these values automatically when the server starts. A log event handler MBean interface must extend the LogEventHandlerMBean interface and must also include the following:

Example

The following code defines an MBean interface for the XML log event handler:

import jrunx.logger.*;
import jrunx.kernel.*;

/**
 * MBean interface for the XML Log Event Handler
 *
 * @author Macromedia, Inc.
 * @date 5December2001
 */
public interface XMLLogEventHandlerMBean
  extends LogEventHandlerMBean {

  /**
   * Sets the name of the file to be used for XML logging
   * @param filename The filename
   */
  public void setFilename(String filename);

  /**
   * Gets the name of the file to be used for XML logging
   * @return The filename
   */
  public String getFilename();
}

Coding a log event handler

A log event handler is a Java class that must include the following items:

JRun calls log event handler methods in the following order:

Example

The following code defines a log event handler that writes log messages in a simple XML-like format:

import java.io.*;
import java.util.*;
import java.text.*;
import jrunx.logger.*;

/**
 * Log events in XML format to a file
 * This extends LogEventHandler, which requires that this
 * class implement the logEvent method.
 * It implements XMLLogEventHandlerMBean, which defines the
 * getFilename and setFilename methods.
 */
public class XMLLogEventHandler extends LogEventHandler
   implements XMLLogEventHandlerMBean {
    protected String filename;
    protected SimpleDateFormat dateFormat = new SimpleDateFormat("MM/HH hh:mm:ss");
    protected String defaultFormat = null;

    // JRun initializes the writer at server startup
    public void init(Properties props) {
        dateFormat = new SimpleDateFormat("MM/HH hh:mm:ss");
    }

   /**
    * Sets the name of the file to be used for XML logging.
    * @param filename The filename
    */
   public void setFilename(String filename) {
        if (filename == null) {
            this.filename = "xml.log";
        }
        else {
         this.filename = filename;
        }
   }

  /**
   * Gets the name of the file to be used for XML logging
   * @return The filename
   */
  public String getFilename() {
     return filename;
  }

    // JRun calls the logEvent method when a log action is requested.
    public synchronized boolean logEvent(LogEvent event) {
        PrintWriter out = null;

        try {
            FileOutputStream fos = new FileOutputStream(filename, true);
            out = new PrintWriter(fos, true);

            // Get the pieces and parts.
            Properties p = event.getVariables(defaultFormat);
            String level = (String)p.get("log.level");
            String msg = (String)p.get("log.message");
            String exception = (String)p.get("log.exception");
            Date date = new Date();

            String formattedDate;
            synchronized(dateFormat) {
                formattedDate = dateFormat.format(date);
            }

            // Crude format.
            out.println("<log>");
             out.println(" <time>" + date + "</time>");
             out.println(" <level>" + level + "</level>");
             out.println(" <message>" + msg + "</message>");
             out.println(" <exception>" + exception + "</exception>");
            out.println("</log>");
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
            if (out != null) {
                out.close();
            } // end if
        }  // end finally
       return true;
    }  // end logEvent method

    public void flush()
    {
        // No-op
    }
}

Defining a service element

You define the XMLLogEventHandler as a service within ThreadedLogEventHandler service in the jrun.xml file. The service contains a filename attribute, as the following example shows:

...
<service class="jrunx.logger.ThreadedLogEventHandler" name="ThreadedLogEventHandler">

  <service class="jrunx.logger.FileLogEventHandler" name="FileLogEventHandler">
    <attribute name="filename">{jrun.rootdir}/logs/{jrun.server.name}-event.log</attribute>
    <attribute name="rotationSize">200000</attribute>
    <attribute name="rotationFiles">3</attribute>
    <attribute name="heading"># Created by JRun on {date MM/dd HH:mm:ss}</attribute>
    <attribute name="closeDelay">5000</attribute>
  </service>

  <service class="XMLLogEventHandler" name="XMLLogEventHandler">
    <attribute name="filename">{jrun.rootdir}/logs/{jrun.server.name}-event.xml
</attribute>
  </service>
</service>
...

Comments


jrunrandy said on Aug 1, 2003 at 1:34 PM :
The JRun JavaDocs, available in your jrun_root/docs directory, can help you figure out the methods to use here.
urmila said on Sep 18, 2003 at 11:42 AM :
Does the Threaded Logger
automatically forward all the
event requests to the Custom
logger in this case? Can you
have both File logger and
Custom logger working in parallel?
or any modifications/settings
that needs to be made for this to happen?
Techek said on Nov 26, 2003 at 1:23 PM :
I changed some of the attributes in FileLogEventHandler to this :

<attribute name="filename">{jrun.rootdir}/logs/{jrun.server.name}/{date yyyyMMdd}_{log.level}_event.log</attribute>
<attribute name="rotationSize">0k</attribute>
<attribute name="rotationFiles">0</attribute>

... which seems to allow an unlimited of logfiles, for each day of the year and log-level. Pretty nifty!

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/jrun/4/JRun_Administrators_Guide/logging4.htm