Results 1 to 2 of 2
  1. #1
    joe2010 is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default Log 4j Multithreading

    Hi.
    I'm trying to create log file based on my thread. Suppose I have two thread T1 and T2. I want to create logfile like T1_out.log and T2_out.log dynamically.
    After coding it I can create two log file which is thread specific but the all message is going to T2_out.log while T1_out.log remains blanks.

    Below is my code.

    class MyThread implements Runnable{

    Thread t;
    MyThread (String threadName) {
    t = new Thread(this,threadName);

    t.start();
    }

    MyThread()
    {

    }
    public void run() {
    try{
    String name=t.getName();
    EPSLogger logger =
    EPSLoggerFactory.getInstance().getLogger(MyThread. class);

    String outFileName= name +"_out.log";

    FileAppender fileAppenderOut= new FileAppender();

    fileAppenderOut.setFile(outFileName);
    fileAppenderOut.activateOptions();

    System.setProperty("file.out.name",outFileName);
    URL url = MyThread.class.getClassLoader().getResource(Proper tyConfig.getInstance().getProperty("LOG_4J"));
    DOMConfigurator.configure(url);

    logger.info("Name", name);
    logger.debug("Name", name);

    }catch(Exception e)
    {
    e.printStackTrace();
    }
    }


    }
    class MultiThreadLog {

    public static void main (String args[]) {

    new MyThread("T1");
    new MyThread("2");

    }

    }

    My Log4j.xml as below

    <appender name="CommonAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${file.out.name}"/> <param name="Append" value="true"/>
    <param name="MaxFileSize" value="50MB"/>
    <param name="MaxBackupIndex" value="10"/>
    <param name="Threshold" value="DEBUG"/>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{DATE} %-5p %c - %m%n"/>
    </layout>
    </appender>


    The reason I'm getting all logs in T2_out.log is because I 'm using System.setproperties();

    Is there any alternate way to do this.? So that I can create thread specific log.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    wow, thread specific log would be neat.

    I sometimes used to add the "%t" output format option (see PatternLayout (Apache Log4j 1.2.15 API) )
    [quote
    Used to output the name of the thread that generated the logging event.[/quote]

    And then, when each log line is tagged with the [thread-1] [main-thread], etc. thread name, I would have to manually post-process and sort the log file to only show, or exclude lines that were of specific threads. Such command line tools as
    Java Code:
      cat log_output.txt | grep "thread-1" > thread1_log.txt
      cat log_output.txt | grep -v "main-thread" > all_except_main_thread_log.txt
    Not very helpful for realtime or immediate thread-specific log outputs.

    other than the manual log setup stuff you are doing inside the thread like you are doing, I think its possible they don't readily do a thread specific log file facility, is the threads can vary , and can be created and destroyed through out the lifetime of the application, and possibly when a thread is created it could eventually have the same name as before and this would clobber or wrongfully append to an existing thread log file.

Similar Threads

  1. Servlet Multithreading
    By Saurabh321 in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-18-2009, 06:13 AM
  2. Applet and multithreading
    By pricelessjunk in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-03-2009, 10:47 PM
  3. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •