Results 1 to 2 of 2
Thread: Log 4j Multithreading
- 01-20-2010, 05:29 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 1
- Rep Power
- 0
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.
- 01-31-2010, 03:48 AM #2
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
Not very helpful for realtime or immediate thread-specific log outputs.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
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
-
Servlet Multithreading
By Saurabh321 in forum Threads and SynchronizationReplies: 4Last Post: 10-18-2009, 05:13 AM -
Applet and multithreading
By pricelessjunk in forum Threads and SynchronizationReplies: 1Last Post: 08-03-2009, 09:47 PM -
multithreading
By shilpa.krishna in forum New To JavaReplies: 2Last Post: 06-27-2008, 04:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks