Hi again,

I'm still trying to figure out a few things in this system, but I could use a little guidance as to if it's possible to accomplish my goal at all.

I'd like to use XMLBeans in a Formatter, replacing the existing XMLFormatter in the java.util.logging package. I have written an adaptation of the existing XML DTD as a schema document, but now I face a problem. The logging framework has no guarantee of closure. If the java runtime crashes or is killed unexpectedly, the framework may not be able to append a closing tag at the end.

There is a secondary application being developed which is expected to be able to parse and filter these log files.

I'd like to be able to use XMLBeans on both the write side and the read.

Now, I hope I've explained this well enough, I'm at a loss as to how to make this work, or even if it can work. Attached is the current schema definition file, and the current form of XMLFormatter replacement which attempts to use it. Unfortunately it outputs entries similar to:

<xml-fragment millis="1119901463411" date="2005-06-27T15:44:23.411-04:00" sequence="0" level="1000"><log:logger xmlns:log="logformat.util.physics.mcla.org"/><log:thread xmlns:log="logformat.util.physics.mcla.org">10</log:thread><log:class xmlns:log="logformat.util.physics.mcla.org">java.util.logging.LogManager$RootLogger</log:class><log:method xmlns:log="logformat.util.physics.mcla.org">log</log:method><log:message xmlns:log="logformat.util.physics.mcla.org">Testing severe warning</log:message></xml-fragment>

And this isn't quite what I hoped for obviously.

I'm open to any and all suggestions,

Thanks

 ~ Stephen
package org.mcla.physics.util.logging;


import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import java.io.IOException;
import java.io.StringWriter;
import org.mcla.physics.util.logformat.RecordType;


/**
 * @author Steve
 * @see java.util.logging.Formatter
 * @see java.util.logging.XMLFormatter
 */
public class XMLFormatter extends Formatter {

	Calendar cal;


	public XMLFormatter() {
		cal = Calendar.getInstance();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
	 */
	public String format(LogRecord record) {
		RecordType rec = RecordType.Factory.newInstance();
		long millis = record.getMillis();
		rec.setMillis(millis);

		cal.setTime(new Date(millis));
		rec.setDate(cal);

		rec.setSequence(record.getSequenceNumber());

		String text = record.getLoggerName();
		if(text != null){
			rec.setLogger(text);
		}

		rec.setLevel(record.getLevel().intValue());

		text = record.getSourceClassName();
		if(text != null){
			rec.setClass1(text);
		}

		text = record.getSourceMethodName();
		if(text != null){
			rec.setMethod(text);
		}

		rec.setThread(record.getThreadID());

		if(record.getMessage() != null){
			rec.setMessage(formatMessage(record));
		}

		//Segment copied and modified from java.util.logging.XMLFormatter
		// If the message is being localized, output the key, resource
		// bundle name, and params.
		ResourceBundle bundle = record.getResourceBundle();
		try{
			if(bundle != null && bundle.getString(record.getMessage()) != null){
				RecordType.Localized locale = rec.addNewLocalized();
				locale.setKey(record.getMessage());
				locale.setCatalog(record.getResourceBundleName());
				
				Object parameters[] = record.getParameters();
				String paramStrings[] = new String[parameters.length];				
				for(int i = 0; i < parameters.length; i++){
					//Just in case there is no toString defined
					try {
						paramStrings[i] = parameters[i].toString();
					}catch(Exception ex){
						paramStrings[i] = "???";
					}
				}
				
				locale.setParamArray(paramStrings);
			}
		}catch(Exception ex){
			// The message is not in the catalog. Drop through.
		}
		
		Throwable thrown = record.getThrown();
		if(thrown != null){
			RecordType.Exception ex = rec.addNewException();
			ex.setMessage(thrown.toString());
			
			StackTraceElement trace[] = thrown.getStackTrace();
			RecordType.Exception.Frame frames[] = new RecordType.Exception.Frame[trace.length];
			for(int i = 0; i < trace.length; i++){
				RecordType.Exception.Frame frame = frames[i] = RecordType.Exception.Frame.Factory.newInstance();
				StackTraceElement element = trace[i];
				
				text = element.getFileName();
				if(text != null){
					frame.setFile(text);
				}

				frame.setClass1(element.getClassName());
				frame.setMethod(element.getMethodName());
				
				if(element.getLineNumber() >= 0){
					frame.setLine(element.getLineNumber());
				}
			}
			
		}
		
		StringWriter writer = new StringWriter();
		try{
			rec.save(writer);
		}catch(IOException e){
			//This line cannot be reached.
			e.printStackTrace();
		}

		return writer.getBuffer().toString();
	}

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to