Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

How do you change a LogRecord?

Thread view: 
Christian Williamson - 02 Apr 2008 21:12 GMT
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
Christian Williamson - 03 Apr 2008 10:08 GMT
> 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();
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.