Code Comments
Programming Forum and web based access to our favorite programming groups.I'm trying to get the Logger class to insert the thread name as well as date and time and so forth. Does anyone know of any code to do this? Thanks buckets in advance, Chris
Post Follow-up to this messageChristian Williamson wrote:
> I'm trying to get the Logger class to insert the thread name as well as
> date and time and so forth. Does anyone know of any code to do this?
>
> Thanks buckets in advance,
> Chris
I figured it out. Here's a class, compiled using JDK 6, that works.
import java.util.logging.*;
import java.util.Date;
import java.io.*;
import java.lang.*;
class IndependentLog implements Runnable {
Logger myLogger;
IndependentLog(Logger myLogger) {
this.myLogger = myLogger;
}
private void logOutSideRunMethod() {
myLogger.finest("It doesn't get any more detailed than this.");
}
public void run () {
myLogger.severe("Hello from a different thread!");
logOutSideRunMethod();
}
}
/**
* Example of manipulating a logger.
*
* Send the log messages to a file, but not
* to the console. Format the log messages
* to several lines, using class LogRecord's
* "get" methods to indicate all the good
* things you can log, including which thread
* you're in.
*
* Chris Williamson, April 3, 2008
**/
public class LogExample {
Logger myLogger;
String logFileName = "c:\\LogExample.log";
public void sillyMethod() {
myLogger.warning("Don't mess with silly methods!");
}
public static void main(String [] args) {
LogExample le = new LogExample();
le.myLogger = Logger.getLogger("myLogger");
FileHandler myHandler = null;
System.out.println("Look in file " +
le.logFileName +
" to see log records.");
le.myLogger.setLevel(Level.ALL);
/* turn off logging to the console. */
le.myLogger.setUseParentHandlers(false);
/* Send the logging messages to a
* file. Will overwrite the file
* every time it's run. */
try {
myHandler = new FileHandler(le.logFileName);
}
catch(IOException e) {
System.out.println("Can't open file handler file. exiting.");
System.exit(1);
}
Formatter myFormatter = new MyFormatter();
le.myLogger.addHandler(myHandler);
myHandler.setFormatter(myFormatter);
le.sillyMethod();
le.myLogger.severe("Things are terrible here!");
le.myLogger.info("Just letting you know.");
/* Set up a separate thread and send
* a log message from there. */
IndependentLog il = new IndependentLog(le.myLogger);
Thread nt = new Thread(il, "New Thread!");
nt.start();
}
}
/**
* From http://tinyurl.com/26pybz with changes.
*
*/
class MyFormatter extends Formatter {
public MyFormatter() {
super();
}
public String format(LogRecord record) {
// Create a StringBuffer to contain the formatted record
// start with the date.
StringBuffer sb = new StringBuffer();
sb.append("\n\n**NEW LOG RECORD**\n");
sb.append("Sequence Number: " +
record.getSequenceNumber() +
"\n");
Date date = new Date(record.getMillis());
sb.append("Date: " +
date.toString());
sb.append("\n");
sb.append("Thread Name: " +
Thread.currentThread().getName() +
"\n");
sb.append("Thread ID: " +
record.getThreadID() +
"\n");
sb.append("Class name: " +
record.getSourceClassName() +
"\n");
sb.append("Method Name: " +
record.getSourceMethodName() +
"\n");
// Get the level name and add it to the buffer
sb.append("Level: " +
record.getLevel().getName() +
"\n");
// Get the formatted message (includes localization and
// substitution of parameters) and add it to the buffer
sb.append("Raw Message: " +
formatMessage(record) +
"\n");
return sb.toString();
}
}
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.