<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>entroducing.com &#187; quartz</title>
	<atom:link href="http://www.entroducing.com/view/tag/quartz/feed" rel="self" type="application/rss+xml" />
	<link>http://www.entroducing.com</link>
	<description>to prove that i have too much time</description>
	<lastBuildDate>Sun, 15 May 2011 06:00:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Create new log file with log4j FileAppender when using Quartz Scheduler</title>
		<link>http://www.entroducing.com/view/create-new-log-file-with-log4j-fileappender-when-using-quartz-scheduler</link>
		<comments>http://www.entroducing.com/view/create-new-log-file-with-log4j-fileappender-when-using-quartz-scheduler#comments</comments>
		<pubDate>Tue, 02 Mar 2010 14:49:18 +0000</pubDate>
		<dc:creator>Benny</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[quartz]]></category>

		<guid isPermaLink="false">http://www.entroducing.com/?p=177</guid>
		<description><![CDATA[Recently came across a problem whereby the quartz scheduler could not recreate a new log4j FileAppender log when the job is call again. Googled a bit and found out a couple of solutions such as using a custom log4j classes, which create a seperate thread when being called in quartz scheduler. I find that it [...]]]></description>
			<content:encoded><![CDATA[<p>Recently came across a problem whereby the quartz scheduler could not recreate a new log4j FileAppender log when the job is call again.</p>
<p>Googled a bit and found out a couple of solutions such as using a custom log4j classes, which create a seperate thread when being called in quartz scheduler.</p>
<p>I find that it is too complex and had try several ways to recreate the log file inside Quartz.</p>
<p>And finally came up with this one line of code.</p>
<p>Before I begin, let&#8217;s take a look at my log4j config.<br />
This log4j config will create a simple console system out logger and also a FileAppender logger.<br />
For my case, I want to re-create a new log file whenever this job is run.</p>
<pre class="brush: php">
log4j.rootLogger=INFO, stdout
log4j.additivity.stdout=false
log4j.additivity.joblog=false
log4j.appender.ROOT.layout.ConversionPattern=[%d] %t %c %-5p - %m%n

#console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p - %m%n

#per job execution log
log4j.logger.com.yourcompany=INFO, joblog
log4j.appender.joblog=org.apache.log4j.FileAppender
log4j.appender.joblog.layout=org.apache.log4j.PatternLayout
log4j.appender.joblog.File=../jobtemp.log
log4j.appender.joblog.Append=false
log4j.appender.joblog.layout.ConversionPattern=%d %p - %m%n
</pre>
<p>and here&#8217;s the schedule job</p>
<pre class="brush: java">
public class ScheduleJob implements StatefulJob {

	private static Logger logger = Logger.getLogger(ScheduleJob.class);

	public void startJob() throws SchedulerException{
		Scheduler scheduler =
			StdSchedulerFactory.getDefaultScheduler();
		scheduler.start();
	}

	public void execute(JobExecutionContext context)
		throws JobExecutionException {

		//reset log4j config for FileAppender
		PropertyConfigurator.configure(&amp;quot;../conf/log4j.properties&amp;quot;);

		RunJobImmediately newJob  = new RunJobImmediately();
		newJob.performJob();

	}

	public static void main(String args[]){
    	try{
    		ScheduleJob scheduleJob = new ScheduleJob();
    		scheduleJob.startJob();

		}catch(Exception e){
			logger.error(e);
		}
	}
}
</pre>
<p>and finally the one line of code to recreate the log file.<br />
Apparently, this line will somehow &#8216;reload&#8217; the log4j  utitlies, which thus recreate the FileAppender logger to re-create the log file.</p>
<pre class="brush: java">
PropertyConfigurator.configure(&amp;quot;../conf/log4j.properties&amp;quot;);
</pre>
<p>Feel free to let me know if there is any performance issues or it is not working at your side.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.entroducing.com%2Fview%2Fcreate-new-log-file-with-log4j-fileappender-when-using-quartz-scheduler&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.entroducing.com/view/create-new-log-file-with-log4j-fileappender-when-using-quartz-scheduler/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

