Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How do you change a LogRecord?
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

Report this thread to moderator Post Follow-up to this message
Old Post
Christian Williamson
04-03-08 12:14 AM


Re: How do you change a LogRecord?
Christian 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();
}
}


Report this thread to moderator Post Follow-up to this message
Old Post
Christian Williamson
04-03-08 09:14 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 01:07 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.