{"id":1717,"date":"2020-02-07T15:36:41","date_gmt":"2020-02-07T15:36:41","guid":{"rendered":"https:\/\/gja.rfl.mybluehost.me\/website_fd74af1b\/introduction-to-log4j\/"},"modified":"2026-03-04T18:38:15","modified_gmt":"2026-03-04T18:38:15","slug":"introduction-to-log4j","status":"publish","type":"post","link":"https:\/\/onwardpath.com\/intelligence\/introduction-to-log4j\/","title":{"rendered":"Introduction to Log4j"},"content":{"rendered":"<h3>What is logging?<\/h3>\n<ul>\n<li>Tracing program execution during development<\/li>\n<li>Debugging<\/li>\n<li>Providing an audit trail for a program<\/li>\n<li>Recording soft-errors (lost connections, etc.) for monitoring performance and troubleshooting<\/li>\n<li>A logging framework lets you use different logging levels<\/li>\n<\/ul>\n<h3>Adding logging code<\/h3>\n<ul>\n<li>Replaces <em>System.out.println()<\/em> calls throughout<\/li>\n<li>Utility of logging code is improved if it\u2019s consistent\u2014logs can be searched.<\/li>\n<li>Five recognized message priorities: DEBUG, INFO, WARN, ERROR &amp; FATAL<\/li>\n<\/ul>\n<h3>Installing Log4J<\/h3>\n<ul>\n<li>Download a zip or compressed tar file from Apache.org website<\/li>\n<li>Uncompress to a local directory<\/li>\n<li>In your Eclipse project\u2019s properties, Locate Java Build Path &#8211; On Libraries page, click on Add External Jars<\/li>\n<li>Browse for your Log4J directory, and locate the log4J jar file in the \/dist\/lib directory<\/li>\n<li>Click Open, followed by OK<\/li>\n<\/ul>\n<h3>Creating a Log4J configuration file<\/h3>\n<p>The easiest way to configure Log4J is to add a <em>log4j.properties<\/em> file to your source directory. There are many options available which we won\u2019t cover here, but essentially you need to define:<\/p>\n<ul>\n<li>A logger<\/li>\n<li>An appender<\/li>\n<li>A pattern layout<\/li>\n<\/ul>\n<h4><\/h4>\n<h4>Log4j Set-Up Process<\/h4>\n<p>The following example uses the default root logger, appends to the console and prints the date, time, message priority, thread and message<\/p>\n<p><b>Step 1 &#8211; Download the Log4j library<\/b><\/p>\n<p>If you are using Maven 2 for your project, you do not need to download the binary. You can simply declare it as a dependency in your Maven 2 <em>pom.xml<\/em> and Maven will grab the library for you. Declare it as follows<\/p>\n<p><tt>&lt;dependency&gt;<br \/>\n&lt;groupId&gt;log4j&lt;\/groupId&gt;<br \/>\n&lt;artifactId&gt;log4j&lt;\/artifactId&gt;<br \/>\n&lt;version&gt;1.2.17&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<br \/>\n<\/tt><\/p>\n<p><b>Step 2 &#8211; Import the jar file into your web project<\/b><\/p>\n<p>If your using Maven 2, you can skip this step; after declaring the dependency, Maven will put the jar file in the proper place automatically when you build.<\/p>\n<p>Import the <em>log4j<\/em> jar file into your <em>JavaBuild<\/em> Path<\/p>\n<p><b>Step 3 &#8211; Create Log4j Properties file<\/b><\/p>\n<p>In Eclipse, right click on the <em>src<\/em> folder<\/p>\n<p>Select New &gt; Others &gt; File<\/p>\n<p>Create file with name <em>log4j.properties<\/em> and add below property settings<\/p>\n<p><tt># Root logger option<br \/>\nlog4j.rootLogger=INFO, file<br \/>\n# Direct log messages to a log file<br \/>\nlog4j.appender.file=org.apache.log4j.RollingFileAppender<br \/>\n#Redirect to Tomcat logs folder<br \/>\n#log4j.appender.file.File=${catalina.home}\/logs\/logging.log<br \/>\nlog4j.appender.file.File=C:\\log\\applog.log<br \/>\n#log4j.appender.file.File=C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\geologs\\applog.log<br \/>\nlog4j.appender.file.MaxFileSize=10MB<br \/>\nlog4j.appender.file.MaxBackupIndex=10<br \/>\nlog4j.appender.file.layout=org.apache.log4j.PatternLayout<br \/>\nlog4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} UTC %-5p %c{1}:%L - %m%n<br \/>\n<\/tt><\/p>\n<p><b>Step 5 &#8211; Put logging code in your classes<\/b><\/p>\n<p>The last thing you need to do is drop some logging code in a class and test this whole setup.<\/p>\n<p>Add the following to the imports section of your java code:<\/p>\n<p><tt>import org.apache.log4j.Logger;<br \/>\nstatic Logger log = Logger.getLogger(MyClassName.class);<br \/>\n<\/tt><\/p>\n<p>Throw some logging statements in your code where you know they&#8217;ll be fired right away when you run your app. For example:<\/p>\n<p><tt>log.trace(\"Hello\");<br \/>\nlog.debug(\"Hello\");<br \/>\nlog.info(\"Hello\");<br \/>\nlog.error(\"Hello\");<br \/>\nlog.warn(\"Hello\");<br \/>\nlog.fatal(\"Hello\");<br \/>\n<\/tt><\/p>\n<p><b>Step 6 &#8211; Run your app and make sure it works<\/b><\/p>\n<p>Finally, run your app and make sure it works. You should see log lines in your file where you setup your appender.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is logging? Tracing program execution during development Debugging Providing an audit trail for a program Recording soft-errors (lost connections, etc.) for monitoring performance and troubleshooting A logging framework lets you use different logging levels Adding logging code Replaces System.out.println() calls throughout Utility of logging code is improved if it\u2019s consistent\u2014logs can be searched. Five [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":4170,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[37],"tags":[120,121,122],"class_list":["post-1717","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology-blog","tag-eclise-log4j-setup","tag-log4j","tag-logging"],"jetpack_featured_media_url":"https:\/\/onwardpath.com\/intelligence\/wp-content\/uploads\/2020\/09\/6-log.jpg","_links":{"self":[{"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/posts\/1717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/comments?post=1717"}],"version-history":[{"count":1,"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/posts\/1717\/revisions"}],"predecessor-version":[{"id":6896,"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/posts\/1717\/revisions\/6896"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/media\/4170"}],"wp:attachment":[{"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/media?parent=1717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/categories?post=1717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onwardpath.com\/intelligence\/wp-json\/wp\/v2\/tags?post=1717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}