Solved

different log4j file for multiple threads

Posted on 2008-09-30
2
736 Views
Last Modified: 2012-05-05
I need to create a different log4j file for each thread in a process. I want to read the configuration from one file and than change just the file name for each thread.

E.g. Process A  reads loads log4j properties file, the logfile name is processA.log

Process A now spawns 3 new threads, I want each thread to keep the same log4j confiiguation  (i.e. log level, file size etc.) and just change the names to processA_thread1.log, processA_thread2.log et.c

Thanks
0
Comment
Question by:sshah_tea
2 Comments
 
LVL 5

Accepted Solution

by:
muktajindal earned 500 total points
ID: 22612927
You may have your threads as per attached code snippet.

class MyThread extends Thread {

	public MyThread(String name) {

		this.setName(name);

	}

	public synchronized void run() {

		

		Properties props = new Properties();

		props.setProperty("log4j.logger.com.myapp", "INFO, myapp");

		props.setProperty("log4j.appender.myapp", "org.apache.log4j.RollingFileAppender");

		props.setProperty("log4j.appender.myapp.File", "C:/Process_"+getName()+".log");

		props.setProperty("log4j.appender.myapp.layout", "org.apache.log4j.PatternLayout");

		props.setProperty("log4j.appender.myapp.MaxFileSize", "500KB");

		props.setProperty("log4j.appender.myapp.layout.ConversionPattern", "%d{MM-dd@HH:mm:ss} %-5p (%13F:%L) %3x - %m%n");

		props.setProperty("log4j.appender.myapp.Threshold", "INFO");
 

		PropertyConfigurator.configure(props);

		

		Logger logger = Logger.getLogger(getClass());

		System.out.println("Logging for " + getName());

		logger.info("Testing " + getName());		

	}

}

Open in new window

0
 

Author Closing Comment

by:sshah_tea
ID: 31501703
perfect!! it worked!!

thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDFBox - convert image to PDF, PDF resolution 3 117
eclipse package explorer vs project explorer view 2 80
custom annotations 9 31
expectj telnet failing 5 26
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now