<?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>Jimmy&#039;s Blog &#187; logger</title>
	<atom:link href="http://jimmod.com/blog/tag/logger/feed/" rel="self" type="application/rss+xml" />
	<link>http://jimmod.com/blog</link>
	<description>My name is Jimmy, and this is my story... ;)</description>
	<lastBuildDate>Fri, 14 Oct 2011 17:26:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple Log4J implementation</title>
		<link>http://jimmod.com/blog/2007/11/simple-log4j-implementation/</link>
		<comments>http://jimmod.com/blog/2007/11/simple-log4j-implementation/#comments</comments>
		<pubDate>Thu, 15 Nov 2007 11:27:23 +0000</pubDate>
		<dc:creator>jimmy</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://jimlife.wordpress.com/2007/11/15/simple-log4j-implementation/</guid>
		<description><![CDATA[ 
				
			 
				
			 
				Tweet 
			
		Why do we need a library for logger? doesn&#8217;t System.out.println help us enough?
When you try log4j you wouldn&#8217;t use System.out.println ever again&#8230; (Ok.. you will still used it.. occasionally :p)
What do you need? of course you need to download log4j first   , get it at log4j download page
And open [...]


Related posts:<ol><li><a href='http://jimmod.com/blog/2007/12/netbeans-6-bug-configurationexception-class-in-apache-common-configuration-is-not-throwable/' rel='bookmark' title='Permanent Link: NetBeans 6 Bug &#8211; ConfigurationException Class in Apache Common Configuration is not Throwable?'>NetBeans 6 Bug &#8211; ConfigurationException Class in Apache Common Configuration is not Throwable?</a></li><li><a href='http://jimmod.com/blog/2010/03/java-me-j2me-json-implementation-tutorialsample/' rel='bookmark' title='Permanent Link: Java ME (J2ME) JSON Implementation Tutorial/Sample'>Java ME (J2ME) JSON Implementation Tutorial/Sample</a></li><li><a href='http://jimmod.com/blog/2007/11/change-junit-run-file-main-class-working-directory-at-netbeans-55-web-project/' rel='bookmark' title='Permanent Link: Change junit &amp; run file (main class) working directory at Netbeans 5.5 Web Project'>Change junit &amp; run file (main class) working directory at Netbeans 5.5 Web Project</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="height:33px; padding-top:2px; padding-bottom:2px; clear:both;" class="really_simple_share"><div style="float:left; width:100px; " class="really_simple_share_facebook_like"> 
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fjimmod.com%2Fblog%2F2007%2F11%2Fsimple-log4j-implementation%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;height=27" 
					scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe>
			</div><div style="float:left; width:90px; padding-left:10px;" class="really_simple_share_google1"> 
				<g:plusone size="medium" href="http://jimmod.com/blog/2007/11/simple-log4j-implementation/" ></g:plusone>
			</div><div style="float:left; width:110px; padding-left:10px;" class="really_simple_share_twitter"> 
				<a href="http://twitter.com/share" class="twitter-share-button" data-count="none" 
					data-text="Simple Log4J implementation" data-url="http://jimmod.com/blog/2007/11/simple-log4j-implementation/">Tweet</a> 
			</div></div>
		<div style="clear:both;"></div><p>Why do we need a library for logger? doesn&#8217;t System.out.println help us enough?<br />
When you try log4j you wouldn&#8217;t use System.out.println ever again&#8230; (Ok.. you will still used it.. occasionally :p)</p>
<p>What do you need? of course you need to download log4j first <img src='http://jimmod.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  , get it at <a href="http://logging.apache.org/log4j/1.2/download.html">log4j download page</a><br />
And open your favorite Java IDE.</p>
<blockquote><p>import org.apache.log4j.Logger;<br />
import org.apache.log4j.PropertyConfigurator;<br />
/**<br />
*<br />
* @author jimmy<br />
*/<br />
public class SampleLog4j {</p>
<p>private final static Logger LOGGER = Logger.getLogger(SampleLog4j.class);</p>
<p>public static void main(String[] args) {<br />
PropertyConfigurator.configure(&#8221;log4j.properties&#8221;);<br />
LOGGER.trace(&#8221;Level trace&#8221;);<br />
LOGGER.debug(&#8221;Level debug&#8221;);<br />
LOGGER.info(&#8221;Level info&#8221;);<br />
LOGGER.warn(&#8221;Level warn&#8221;);<br />
LOGGER.fatal(&#8221;Level fatal&#8221;);<br />
}</p>
<p>}</p></blockquote>
<p>This program will read file log4j.properties in the folder for log4j configuration.<br />
Example of log4j.properties:</p>
<blockquote><p> ### file appender<br />
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender<br />
log4j.appender.file.File=sample.log<br />
log4j.appender.file.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} &#8212; %p %C(%M):%L &#8212; %m%n<br />
### console appender<br />
log4j.appender.console=org.apache.log4j.ConsoleAppender<br />
log4j.appender.console.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} &#8212; %p %C(%M):%L &#8212; %m%nlog4j.rootLogger=debug, console<br />
# uncomment the line below if you want log output to file<br />
# log4j.rootLogger=debug, file</p></blockquote>
<p>And it&#8217;ll produce output:</p>
<blockquote><p>2007-11-15 18:19:09,812 &#8212; DEBUG testing.SampleLog4j(main):26 &#8212; Level debug<br />
 2007-11-15 18:19:09,828 &#8212; INFO testing.SampleLog4j(main):27 &#8212; Level info<br />
 2007-11-15 18:19:09,828 &#8212; WARN testing.SampleLog4j(main):28 &#8212; Level warn<br />
 2007-11-15 18:19:09,828 &#8212; FATAL testing.SampleLog4j(main):29 &#8212; Level fatal
</p></blockquote>
<p>Why the log &#8220;Level trace&#8221; doesn&#8217;t logged, because we set the log level only to debug.<br />
You should learn the configuration. There are 2 appender, 1 will log to console and other will log to a file.</p>
<p>Now why shouldn&#8217;t I used standard System.out.println?</p>
<ul>
<li>You can&#8217;t set the level of log with println only</li>
<li>You will end up delete/comment the log using println</li>
<li>It won&#8217;t be that easy to create log to file with different filename daily (daily-rolling)</li>
<li>I wonder how your println can print class name, line number easily <img src='http://jimmod.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>&#8230; you can add more after you try it <img src='http://jimmod.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
</ul>


<p>Related posts:<ol><li><a href='http://jimmod.com/blog/2007/12/netbeans-6-bug-configurationexception-class-in-apache-common-configuration-is-not-throwable/' rel='bookmark' title='Permanent Link: NetBeans 6 Bug &#8211; ConfigurationException Class in Apache Common Configuration is not Throwable?'>NetBeans 6 Bug &#8211; ConfigurationException Class in Apache Common Configuration is not Throwable?</a></li><li><a href='http://jimmod.com/blog/2010/03/java-me-j2me-json-implementation-tutorialsample/' rel='bookmark' title='Permanent Link: Java ME (J2ME) JSON Implementation Tutorial/Sample'>Java ME (J2ME) JSON Implementation Tutorial/Sample</a></li><li><a href='http://jimmod.com/blog/2007/11/change-junit-run-file-main-class-working-directory-at-netbeans-55-web-project/' rel='bookmark' title='Permanent Link: Change junit &amp; run file (main class) working directory at Netbeans 5.5 Web Project'>Change junit &amp; run file (main class) working directory at Netbeans 5.5 Web Project</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://jimmod.com/blog/2007/11/simple-log4j-implementation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

