Solved

different log4j file for multiple threads

Posted on 2008-09-30
2
738 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 56
oracle 11g 23 107
Android development question 2 55
Java 8 to Java 6 8 22
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 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