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 / May 2006

Tip: Looking for answers? Try searching our database.

log4j, threading and appenders

Thread view: 
simonboland@gmail.com - 25 May 2006 13:19 GMT
I have a multithreaded application that executes several tasks in
parallel.  Each task involves  telnet and FTP connections and each task
is executed using a new thread.  The overall application is controlled
using RMI.

The application must be able to retrieve the log entries for a given
task.  This means retrieving the log entries for a given thread.
Furthermore this has to be performed synchronously by polling at
regular intervals to return the newest log entries.

I'm considering using the Log4j library by extending an appender and
using a pattern layout for formatting.   What I need is a sort of a
memory appender which saves the log entries for each thread in a
buffer.  It would then need to return the newest entries upon each
call.

A few questions:
1) Are there better alternatives to log4J?
2) Should I use one Log4j instance with multiple memory appenders for
each thread? This means I need to manage these with a list of
appenders.
3) Is there a better way by using multiple instances of Log4j, i.e. one
for each thread?

Thanks.
Domagoj Klepac - 25 May 2006 15:31 GMT
>A few questions:
>1) Are there better alternatives to log4J?

No.

>2) Should I use one Log4j instance with multiple memory appenders for
>each thread? This means I need to manage these with a list of
>appenders.
>3) Is there a better way by using multiple instances of Log4j, i.e. one
>for each thread?

You don't instantiate Log4j, you just instantiate logger for every
class which logs something:

public class MyClass {
   private static Logger log = Logger.getLogger(MyClass.class);

   public static void main(String[] args) {
       log.info("Log something...");
   }
}

There are several ways to separate log entries for each thread. If
your threads don't use the same classes, you could use:

public class MyClass {
   private static Logger log = Logger.getLogger("Thread1");

   public void myMethod() {
       log.info("Log something for Thread1");
   }
}

public class MyOtherClass {
   private static Logger log = Logger.getLogger("Thread2");

   public void myMethod() {
       log.info("Log something for Thread2");
   }
}

...and thus have separate loggers for each thread.

Or you might want to use NDC/MDC - check log4j docs for that one.

               Domchi

Signature

Ouroboros ltd. - http://www.ouroboros.hr 
Antispam: to reply, remove extra monkey from reply-to address.

dimitar - 25 May 2006 15:53 GMT
CharArrayWriter sink = new CharArrayWriter();
Layout layout = ....;
Logger logger = Logger.getLogger("ftp-session." + sessionId);
logger.setAppender(new WriterAppender(layout, sink))
Oliver Wong - 25 May 2006 16:25 GMT
> 3) Is there a better way by using multiple instances of Log4j, i.e. one
> for each thread?

   You may want to look at the ThreadLocal class:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ThreadLocal.html

   - Oliver


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.