<?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>Log4j &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/log4j/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:23:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Log4j &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>IntelliJ: Overriding Log4J Configuration Globally for JUnit</title>
		<link>https://myshittycode.com/2014/09/05/intellij-overriding-log4j-configuration-globally-for-junit/</link>
					<comments>https://myshittycode.com/2014/09/05/intellij-overriding-log4j-configuration-globally-for-junit/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Sat, 06 Sep 2014 02:55:39 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Development Tools]]></category>
		<category><![CDATA[IntelliJ IDEA]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Log4j]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=579</guid>

					<description><![CDATA[<p>PROBLEM Most of the time, we may have several Log4J configurations depending on the environments, for example:- Since log4j.xml and log4j2.xml are the default configuration files for Log4J and Log4J2, these configurations will always be used unless we override the configuration file path. In another word, if we don&#8217;t override the configuration file path and [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/09/05/intellij-overriding-log4j-configuration-globally-for-junit/">IntelliJ: Overriding Log4J Configuration Globally for JUnit</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Most of the time, we may have several Log4J configurations depending on the environments, for example:-</p>



<ul class="wp-block-list">
<li><b>log4j.xml</b> (Log4J) or <b>log4j2.xml</b> (Log4J2) &#8211; Production configuration using socket appender.</li>



<li><b>log4j-dev.xml</b> (Log4J) or <b>log4j2-dev.xml</b> (Log4J2) &#8211; Development configuration using console appender.</li>
</ul>



<p>Since <b>log4j.xml</b> and <b>log4j2.xml</b> are the default configuration files for Log4J and Log4J2, these configurations will always be used unless we override the configuration file path.</p>



<p>In another word, if we don&#8217;t override the configuration file path and we run our JUnit test cases offline from IntelliJ, it may take a very long time to execute them due to the broken socket connection. Further, it is not a good idea to clutter our production log files with non-production logs.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>To fix this, we need to configure IntelliJ to always use our development Log4J configuration.</p>



<p>First, on the menu bar, select <b>Run -&gt; Edit Configurations&#8230;</b></p>



<figure class="wp-block-image aligncenter"><img fetchpriority="high" decoding="async" width="385" height="181" src="https://myshittycode.com/wp-content/uploads/2014/09/screen-shot-2014-09-05-at-9-35-14-pm-1.png?x45560" alt="" class="wp-image-580" srcset="https://myshittycode.com/wp-content/uploads/2014/09/screen-shot-2014-09-05-at-9-35-14-pm-1.png 385w, https://myshittycode.com/wp-content/uploads/2014/09/screen-shot-2014-09-05-at-9-35-14-pm-1-300x141.png 300w" sizes="(max-width: 385px) 100vw, 385px" /></figure>



<p>Then, delete all the existing JUnit configuration files.</p>



<figure class="wp-block-image aligncenter"><img decoding="async" width="992" height="636" src="https://myshittycode.com/wp-content/uploads/2014/09/screen_shot_2014-09-05_at_9_13_53_pm-1.png?x45560" alt="" class="wp-image-581" srcset="https://myshittycode.com/wp-content/uploads/2014/09/screen_shot_2014-09-05_at_9_13_53_pm-1.png 992w, https://myshittycode.com/wp-content/uploads/2014/09/screen_shot_2014-09-05_at_9_13_53_pm-1-300x192.png 300w, https://myshittycode.com/wp-content/uploads/2014/09/screen_shot_2014-09-05_at_9_13_53_pm-1-768x492.png 768w" sizes="(max-width: 992px) 100vw, 992px" /></figure>



<p>Finally, expand <b>Defaults -&gt; JUnit</b>.</p>



<p>Under <strong>VM options</strong>, specify the following system property:-</p>



<ul class="wp-block-list">
<li>For Log4J, specify <b>-Dlog4j.configuration=log4j-dev.xml</b></li>



<li>For Log4J2, specify <b>-Dlog4j.configurationFile=log4j2-dev.xml</b></li>
</ul>



<p>Please note the slight change on the system property name depending on the Log4J version.</p>



<figure class="wp-block-image aligncenter"><img decoding="async" width="988" height="635" src="https://myshittycode.com/wp-content/uploads/2014/09/screen_shot_2014-09-05_at_9_16_03_pm-1.png?x45560" alt="" class="wp-image-582" srcset="https://myshittycode.com/wp-content/uploads/2014/09/screen_shot_2014-09-05_at_9_16_03_pm-1.png 988w, https://myshittycode.com/wp-content/uploads/2014/09/screen_shot_2014-09-05_at_9_16_03_pm-1-300x193.png 300w, https://myshittycode.com/wp-content/uploads/2014/09/screen_shot_2014-09-05_at_9_16_03_pm-1-768x494.png 768w" sizes="(max-width: 988px) 100vw, 988px" /></figure>



<p>Now, when we run our JUnit test cases from IntelliJ, it will always pick up the correct custom Log4J configuration file.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/09/05/intellij-overriding-log4j-configuration-globally-for-junit/">IntelliJ: Overriding Log4J Configuration Globally for JUnit</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/09/05/intellij-overriding-log4j-configuration-globally-for-junit/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">579</post-id>	</item>
		<item>
		<title>Managing Log4j Configuration for Both Development and Production Environments</title>
		<link>https://myshittycode.com/2013/09/04/managing-log4j-configuration-for-both-development-and-production-environments/</link>
					<comments>https://myshittycode.com/2013/09/04/managing-log4j-configuration-for-both-development-and-production-environments/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 04 Sep 2013 13:25:19 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Jetty]]></category>
		<category><![CDATA[Log4j]]></category>
		<category><![CDATA[Maven]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=25</guid>

					<description><![CDATA[<p>PROBLEM Most of the time, we set the Log4j&#8217;s log levels to something lower (debug or info) during our local development. Once it is ready for production, we normally set the Log4j&#8217;s log levels to something higher (warn or even error) to prevent meaningless information from flooding the server log. One way to do this [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/09/04/managing-log4j-configuration-for-both-development-and-production-environments/">Managing Log4j Configuration for Both Development and Production Environments</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Most of the time, we set the Log4j&#8217;s log levels to something lower (<b>debug</b> or <b>info</b>) during our local development. Once it is ready for production, we normally set the Log4j&#8217;s log levels to something higher (<b>warn</b> or even <b>error</b>) to prevent meaningless information from flooding the server log.</p>



<p>One way to do this is to manually adjust the log level(s) in <b>log4.xml</b> during our local development, for example:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?--&gt;

&lt;log4j:configuration&gt;
    &lt;!-- This appender logs to the console --&gt;
    &lt;appender name=&quot;consoleAppender&quot; class=&quot;org.apache.log4j.ConsoleAppender&quot;&gt;
        &lt;param name=&quot;Target&quot; value=&quot;System.out&quot;&gt;
        &lt;layout class=&quot;org.apache.log4j.PatternLayout&quot;&gt;
            &lt;param name=&quot;ConversionPattern&quot; value=&quot;&#x5B;%-5p] &#x5B;%c{1}] &#x5B;%M:%L] - %m%n&quot;&gt;
        &lt;/layout&gt;
    &lt;/appender&gt;

	&lt;!-- Set &quot;debug&quot; log level for project code  --&gt;
    &lt;logger name=&quot;com.choonchernlim.myproject&quot;&gt;
        &lt;level value=&quot;debug&quot;&gt;
    &lt;/level&gt;&lt;/logger&gt;

	&lt;!-- Set &quot;info&quot; log level for Spring framework  --&gt;
    &lt;logger name=&quot;org.springframework&quot;&gt;
        &lt;level value=&quot;info&quot;&gt;
    &lt;/level&gt;&lt;/logger&gt;

	&lt;!-- Set &quot;debug&quot; log level for Hibernate framework  --&gt;
    &lt;logger name=&quot;org.hibernate&quot;&gt;
        &lt;level value=&quot;debug&quot;&gt;
    &lt;/level&gt;&lt;/logger&gt;

    &lt;root&gt;
    	&lt;!-- The default log level is &quot;warn&quot; --&gt;
        &lt;priority value=&quot;warn&quot;&gt;
        &lt;appender-ref ref=&quot;consoleAppender&quot;&gt;
    &lt;/appender-ref&gt;&lt;/priority&gt;&lt;/root&gt;
&lt;/log4j:configuration&gt;
</pre></div>


<p>Once we are ready for production, we will manually change the package specific log levels back to <b>warn</b> or comment them out, for example:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?--&gt;

&lt;log4j:configuration&gt;
    &lt;!-- this appender logs to the console --&gt;
    &lt;appender name=&quot;consoleAppender&quot; class=&quot;org.apache.log4j.ConsoleAppender&quot;&gt;
        &lt;param name=&quot;Target&quot; value=&quot;System.out&quot;&gt;
        &lt;layout class=&quot;org.apache.log4j.PatternLayout&quot;&gt;
            &lt;param name=&quot;ConversionPattern&quot; value=&quot;&#x5B;%-5p] &#x5B;%c{1}] &#x5B;%M:%L] - %m%n&quot;&gt;
        &lt;/layout&gt;
    &lt;/appender&gt;

	&lt;!-- Comment out package specific log levels
    &lt;logger name=&quot;com.choonchernlim.myproject&quot;&gt;
        &lt;level value=&quot;debug&quot;/&gt;
    &lt;/logger&gt;

    &lt;logger name=&quot;org.springframework&quot;&gt;
        &lt;level value=&quot;info&quot;/&gt;
    &lt;/logger&gt;

    &lt;logger name=&quot;org.hibernate&quot;&gt;
        &lt;level value=&quot;debug&quot;/&gt;
    &lt;/logger&gt;
	--&gt;

    &lt;root&gt;
		&lt;!-- The default log level is &quot;warn&quot; --&gt;
        &lt;priority value=&quot;warn&quot;&gt;
        &lt;appender-ref ref=&quot;consoleAppender&quot;&gt;
    &lt;/appender-ref&gt;&lt;/priority&gt;&lt;/root&gt;
&lt;/log4j:configuration&gt;
</pre></div>


<p>The problem with this approach is I <strong>always forget</strong> to make necessary changes in <b>log4j.xml</b> when I&#8217;m ready to package my project to be deployed in production.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>The solution that I come up with is rather simple. We will have two Log4j XML files under <b>src/main/resources</b>:-</p>



<ul class="wp-block-list">
<li><b>log4j.xml</b></li>



<li><b>log4j-dev.xml</b></li>
</ul>



<p>By default, assuming there&#8217;s no further Log4j configuration, <b>log4j.xml</b> will always get picked up by Log4j due to the default filename and its location. <b>log4j-dev.xml</b> will always be ignored by Log4j.</p>



<p>To ensure our local development picks up the configuration from <b>log4j-dev.xml</b>, we will need to make a minor tweak to the Jetty configuration in <b>pom.xml</b>:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [16,17,18,19]; title: ; notranslate">
&lt;project ...=&quot;&quot;&gt;
	...
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupid&gt;org.mortbay.jetty&lt;/groupid&gt;
                &lt;artifactid&gt;jetty-maven-plugin&lt;/artifactid&gt;
                &lt;version&gt;8.1.8.v20121106&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;systemproperties&gt;
                        &lt;!--
                        When Jetty runs, &quot;log4j-dev.xml&quot; will be used instead of
						&quot;log4j.xml&quot; because the latter is reserved for production
						usage.
                        --&gt;
                        &lt;systemproperty&gt;
                            &lt;name&gt;log4j.configuration&lt;/name&gt;
                            &lt;value&gt;log4j-dev.xml&lt;/value&gt;
                        &lt;/systemproperty&gt;
                    &lt;/systemproperties&gt;
					...
                &lt;/configuration&gt;
                &lt;dependencies&gt;
					...
                &lt;/dependencies&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;
</pre></div>


<p>This way, when we run our local development on Jetty, <b>log4j-dev.xml</b> will be used. When we deploy the project in production, <b>log4j.xml</b> will be used instead.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/09/04/managing-log4j-configuration-for-both-development-and-production-environments/">Managing Log4j Configuration for Both Development and Production Environments</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/09/04/managing-log4j-configuration-for-both-development-and-production-environments/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">25</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-22 04:18:34 by W3 Total Cache
-->