Spring Core

Spring Logging with Log4J Example

<p>Spring <i><b>Logging <&sol;b><&sol;i>is very important for any application its give us insider information about application about its background and what happens in the application at debugging and run time&period; It helps us understand what is happening inside the engine by recording parameters silently and provides vital information&period; It helps in providing system related information like health status&comma; debugging and provides critical support during production troubleshooting and unexplained failure&period;<&sol;p>&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<h2>Spring Logging<&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the Spring logging options as below&colon;<&sol;p>&NewLine;<h3><b>Understanding for Log4J Logger Tool&colon;- <&sol;b><&sol;h3>&NewLine;<p>Log4j logger contains three main components namely logger&comma; appender and layout&period; Logger takes care of the logging mechanism and deals with the level of logging&period; Log4j provides five standard levels of logging&period; There are two more special levels given by log4j&period; Above all these&comma; log4j allows you to create custom levels&period;<&sol;p>&NewLine;<h3><b>Five standard log4j levels&colon;-<&sol;b><&sol;h3>&NewLine;<p><b>1&period; DEBUG Level &&num;8211&semi;<&sol;b> This log4j level helps the developer to debug the application&period; Level of message logged will be focused on providing support to an application developer&period;<&sol;p>&NewLine;<p><b>2&period; INFO Level &&num;8211&semi;<&sol;b> This log4j level gives the progress and chosen state information&period; This level will be generally useful for the end user&period; This level is one level higher than DEBUG&period;<&sol;p>&NewLine;<p><b>3&period; WARN Level &&num;8211&semi;<&sol;b> This log4j level gives a warning about an unexpected event to the user&period; The messages coming out of this level may not halt the progress of the system&period;<&sol;p>&NewLine;<p><b>4&period; ERROR Level &&num;8211&semi; <&sol;b>This log4j level gives information about a serious error which needs to be addressed and may result in an unstable state&period; This level is one level higher than WARN&period;<&sol;p>&NewLine;<p><b>5&period; FATAL Level &&num;8211&semi;<&sol;b> This log4j level is straightforward and you don’t get it quite often&period; Once you get this level and it indicates application death&period;<&sol;p>&NewLine;<h3><b>Two special log4j levels&colon;-<&sol;b><&sol;h3>&NewLine;<p><b>ALL Level-<&sol;b><br &sol;>&NewLine;This log4j level is used to turn on all levels of logging&period; Once this is configured and the levels are not considered&period;<&sol;p>&NewLine;<p><b>OFF Level-<&sol;b><br &sol;>&NewLine;This log4j level is opposite to ALL level&period; It turns off all the logging&period;<&sol;p>&NewLine;<p>This is very easy to use <i><b>Log4J <&sol;b><&sol;i>functionality inside Spring MVC applications&period; The following example will take you through simple steps to explain the simple integration between Log4J and Spring or Spring MVC&period;<&sol;p>&NewLine;<p><i><b>Step 1&colon; Log4j library&lpar;<&sol;b><&sol;i><i><b><b>log4j-x&period;y&period;z&period;jar<&sol;b>&rpar;&colon;- <&sol;b><&sol;i> Download the <i><b>Log4j library<&sol;b><&sol;i> from the <a href&equals;"http&colon;&sol;&sol;logging&period;apache&period;org&sol;log4j&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer">official website<&sol;a>&comma;<br &sol;>&NewLine;<i><b><br &sol;>&NewLine;<&sol;b><&sol;i> <i><b>Step 2&colon;<&sol;b><&sol;i> <i><b>log4j&period;properties&colon;-<&sol;b><&sol;i> Create a <i><b>Log4j properties<&sol;b><&sol;i> file &lpar;<b><i>log4j&period;properties<&sol;i><&sol;b>&rpar;&comma; put it into the project classpath&period;<&sol;p>&NewLine;<p>Define how log4j handling the logged message&comma; in this example&comma; it will redirect all the logged messages into a text file named <b>&&num;8220&semi;C&colon; logging&period;out<&sol;b>&&num;8220&semi;&period;<&sol;p>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">&num; Direct log messages to a log file&NewLine;log4j&period;appender&period;file&equals;org&period;apache&period;log4j&period;RollingFileAppender&NewLine;log4j&period;appender&period;file&period;File&equals;C&colon;logging&period;out&NewLine;log4j&period;appender&period;file&period;MaxFileSize&equals;1MB&NewLine;log4j&period;appender&period;file&period;MaxBackupIndex&equals;1&NewLine;log4j&period;appender&period;file&period;layout&equals;org&period;apache&period;log4j&period;PatternLayout&NewLine;log4j&period;appender&period;file&period;layout&period;ConversionPattern&equals;&percnt;d&lbrace;ABSOLUTE&rcub; &percnt;5p &percnt;c&lbrace;1&rcub;&colon;&percnt;L - &percnt;m&percnt;n&NewLine; &NewLine;&num; Direct log messages to stdout&NewLine;log4j&period;appender&period;stdout&equals;org&period;apache&period;log4j&period;ConsoleAppender&NewLine;log4j&period;appender&period;stdout&period;Target&equals;System&period;out&NewLine;log4j&period;appender&period;stdout&period;layout&equals;org&period;apache&period;log4j&period;PatternLayout&NewLine;log4j&period;appender&period;stdout&period;layout&period;ConversionPattern&equals;&percnt;d&lbrace;ABSOLUTE&rcub; &percnt;5p &percnt;c&lbrace;1&rcub;&colon;&percnt;L - &percnt;m&percnt;n&NewLine; &NewLine;&num; Root logger option&NewLine;log4j&period;rootLogger&equals;debug&comma; file&comma; stdout&NewLine;<&sol;pre>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img title&equals;"Spring Logging with Log4J" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2012&sol;12&sol;looging&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p><i><b><br &sol;>&NewLine;<&sol;b><&sol;i> <i><b>Step 3&colon;-<&sol;b><&sol;i>In Java class&comma; call <b>Logger&period;getLogger<&sol;b> to return the Log4j handler to handle the logging task&period;<br &sol;>&NewLine;<i><b>HelloLoggingController&period;java<&sol;b><&sol;i><br &sol;>&NewLine;<i><b><br &sol;>&NewLine;<&sol;b><&sol;i><&sol;p>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;controller&semi;&NewLine;&NewLine;import java&period;util&period;HashMap&semi;&NewLine;import java&period;util&period;Map&semi;&NewLine;&NewLine;import org&period;apache&period;log4j&period;Logger&semi;&NewLine;import org&period;springframework&period;stereotype&period;Controller&semi;&NewLine;import org&period;springframework&period;web&period;bind&period;annotation&period;RequestMapping&semi;&NewLine;import org&period;springframework&period;web&period;servlet&period;ModelAndView&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;&commat;Controller&NewLine;public class HelloLoggingController &lbrace;&NewLine; &sol;&sol;get log4j handler&NewLine; private static final Logger logger &equals; Logger&period;getLogger&lpar;HelloLoggingController&period;class&rpar;&semi;&NewLine; &NewLine; &commat;RequestMapping&lpar;"&sol;hello"&rpar;&NewLine; public ModelAndView sayLogiingHello&lpar;&rpar;&lbrace;&NewLine; &sol;&sol;log it via log4j&NewLine; if&lpar;logger&period;isDebugEnabled&lpar;&rpar;&rpar;&lbrace;&NewLine; logger&period;debug&lpar;"Start debug"&rpar;&semi;&NewLine; &rcub;&NewLine; logger&period;info&lpar;"Going to run HelloLoggingController class"&rpar;&semi;&NewLine; Map model &equals; new HashMap&lpar;&rpar;&semi;&NewLine; model&period;put&lpar;"message"&comma; "Hello world Example with Logging"&rpar;&semi;&NewLine; model&period;put&lpar;"author"&comma; "Dinesh on Java"&rpar;&semi;&NewLine; logger&period;info&lpar;"Exiting the program"&rpar;&semi;&NewLine; return new ModelAndView&lpar;"helloWorld"&comma; model&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p><i><b>Step 4&colon;<&sol;b><&sol;i> You can generate debug and error message similar way as we have generated info messages&period; Now let us see the content of the <i><b>sdnext-servlet&period;xml <&sol;b><&sol;i>file&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;beans xmlns&colon;context&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context" xmlns&colon;mvc&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc" xmlns&colon;p&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;p" xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" xsi&colon;schemalocation&equals;"&NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&sol;spring-beans-3&period;0&period;xsd&NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context&NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context&sol;spring-context-3&period;0&period;xsd&NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc&NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc&sol;spring-mvc-3&period;0&period;xsd"&gt&semi;&NewLine;&NewLine; &lt&semi;mvc&colon;annotation-driven&gt&semi;&lt&semi;&sol;mvc&colon;annotation-driven&gt&semi;&NewLine; &NewLine; &lt&semi;context&colon;component-scan base-package&equals;"com&period;dineshonjava&period;controller"&gt&semi;&NewLine;&lt&semi;&sol;context&colon;component-scan&gt&semi;&NewLine;&NewLine;&lt&semi;bean class&equals;"org&period;springframework&period;web&period;servlet&period;view&period;InternalResourceViewResolver" id&equals;"jspViewResolver"&gt&semi;&NewLine; &lt&semi;property name&equals;"viewClass" value&equals;"org&period;springframework&period;web&period;servlet&period;view&period;JstlView"&gt&semi;&lt&semi;&sol;property&gt&semi;&NewLine; &lt&semi;property name&equals;"prefix" value&equals;"&sol;WEB-INF&sol;views&sol;"&gt&semi;&lt&semi;&sol;property&gt&semi;&NewLine; &lt&semi;property name&equals;"suffix" value&equals;"&period;jsp"&gt&semi;&lt&semi;&sol;property&gt&semi;&NewLine;&lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;beans&gt&semi;&NewLine;<&sol;pre>&NewLine;<p><i><b><br &sol;>&NewLine;<&sol;b><&sol;i> <i><b>web&period;xml<&sol;b><&sol;i><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;web-app version&equals;"2&period;5" xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" xmlns&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee" xsi&colon;schemalocation&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee &NewLine; http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;2&lowbar;5&period;xsd"&gt&semi;&NewLine;&NewLine;&lt&semi;servlet&gt&semi;&NewLine; &lt&semi;servlet-name&gt&semi;sdnext&lt&semi;&sol;servlet-name&gt&semi;&NewLine; &lt&semi;servlet-class&gt&semi;org&period;springframework&period;web&period;servlet&period;DispatcherServlet&lt&semi;&sol;servlet-class&gt&semi;&NewLine; &lt&semi;init-param&gt&semi;&NewLine; &lt&semi;param-name&gt&semi;contextConfigLocation&lt&semi;&sol;param-name&gt&semi;&lt&semi;param-value&gt&semi;&sol;WEB-INF&sol;config&sol;sdnext-servlet&period;xml&lt&semi;&sol;param-value&gt&semi;&lt&semi;&sol;init-param&gt&semi;&NewLine; &lt&semi;load-on-startup&gt&semi;1&lt&semi;&sol;load-on-startup&gt&semi;&NewLine; &lt&semi;&sol;servlet&gt&semi;&NewLine;&NewLine; &lt&semi;servlet-mapping&gt&semi;&NewLine; &lt&semi;servlet-name&gt&semi;sdnext&lt&semi;&sol;servlet-name&gt&semi;&NewLine; &lt&semi;url-pattern&gt&semi;&ast;&period;html&lt&semi;&sol;url-pattern&gt&semi;&NewLine; &lt&semi;&sol;servlet-mapping&gt&semi;&NewLine;&NewLine; &lt&semi;welcome-file-list&gt&semi;&NewLine; &lt&semi;welcome-file&gt&semi;index&period;html&lt&semi;&sol;welcome-file&gt&semi;&NewLine; &lt&semi;&sol;welcome-file-list&gt&semi;&NewLine;&lt&semi;&sol;web-app&gt&semi;&NewLine;<&sol;pre>&NewLine;<p><i><b><br &sol;>&NewLine;<&sol;b><&sol;i> <i style&equals;"color&colon; &num;0c343d&semi;"><b>HelloWorld&period;jsp<&sol;b><&sol;i><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; taglib prefix&equals;"c" uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jstl&sol;core" &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"x" uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jstl&sol;xml" &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"fmt" uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jstl&sol;fmt" &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"sql" uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jstl&sol;sql" &percnt;&gt&semi;&NewLine;&NewLine;&NewLine;Welcome message &colon; &dollar;&lbrace;message&rcub;&NewLine;<&sol;pre>&NewLine;<p>Once you are done with creating source and bean configuration files&comma; let us run the application&period; If everything is fine with your application&comma; this we will get the following message in the browser&colon;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img class&equals;"aligncenter" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2012&sol;12&sol;logging&period;jpg" width&equals;"640" height&equals;"148" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Same time<&sol;b> if you will check your<b> C&colon; drive <&sol;b>then you should find your log file <b>logging&period;out<&sol;b> with various log messages&comma; something as follows&colon;<&sol;p>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">------&NewLine;Going to run HelloLoggingController class&NewLine;Exiting the program&NewLine;Invoking afterPropertiesSet&lpar;&rpar; on bean with name 'helloWorld'&NewLine;Rendering view &lbrack;org&period;springframework&period;web&period;servlet&period;view&period;JstlView&colon; name 'helloWorld'&semi; URL &lbrack;&sol;WEB-INF&sol;views&sol;helloWorld&period;jsp&rsqb;&rsqb; in DispatcherServlet with name 'sdnext'&NewLine;Added model object 'message' of type &lbrack;java&period;lang&period;String&rsqb; to request in view with name 'helloWorld'&NewLine;Added model object 'author' of type &lbrack;java&period;lang&period;String&rsqb; to request in view with name 'helloWorld'&NewLine;Forwarding to resource &lbrack;&sol;WEB-INF&sol;views&sol;helloWorld&period;jsp&rsqb; in InternalResourceView 'helloWorld'&NewLine;Successfully completed request&NewLine;----&NewLine;<&sol;pre>&NewLine;<h2><b>Spring logging with Jakarta Commons Logging &lpar;JCL&rpar; API<&sol;b><&sol;h2>&NewLine;<p>Alternatively&comma; you can use <b>Jakarta Commons Logging &lpar;JCL&rpar;<&sol;b> API to generate a log in your Spring logging application&period; JCL can be downloaded from the <a href&equals;"http&colon;&sol;&sol;jakarta&period;apache&period;org&sol;commons&sol;logging&sol;" target&equals;"blank" rel&equals;"nofollow noopener noreferrer">http&colon;&sol;&sol;jakarta&period;apache&period;org&sol;commons&sol;logging&sol;<&sol;a>&period; The only file we technically need out of this package is the <i>commons-logging-x&period;y&period;z&period;jar<&sol;i> file&comma; which needs to be placed in your class-path similar way as you had put <i>log4j-x&period;y&period;z&period;jar<&sol;i> in the above example&period;<&sol;p>&NewLine;<p>To use the logging functionality you need an <b><i>org&period;apache&period;commons&period;logging&period;Log<&sol;i> <&sol;b>object and then you can call one of the following methods as per your requirement&colon;<&sol;p>&NewLine;<ul class&equals;"list">&NewLine;<li>fatal&lpar;Object message&rpar;<&sol;li>&NewLine;<li>error&lpar;Object message&rpar;<&sol;li>&NewLine;<li>warn&lpar;Object message&rpar;<&sol;li>&NewLine;<li>info&lpar;Object message&rpar;<&sol;li>&NewLine;<li>debug&lpar;Object message&rpar;<&sol;li>&NewLine;<li>trace&lpar;Object message&rpar;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Below is the replacement of <i><b>HelloLoggingController&period;java <&sol;b><&sol;i>which makes use of JCL API&colon;<&sol;p>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;controller&semi;&NewLine;&NewLine;import java&period;util&period;HashMap&semi;&NewLine;import java&period;util&period;Map&semi;&NewLine;&NewLine;import org&period;apache&period;commons&period;logging&period;Log&semi;&NewLine;import org&period;apache&period;commons&period;logging&period;LogFactory&semi;&NewLine;import org&period;springframework&period;stereotype&period;Controller&semi;&NewLine;import org&period;springframework&period;web&period;bind&period;annotation&period;RequestMapping&semi;&NewLine;import org&period;springframework&period;web&period;servlet&period;ModelAndView&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;&commat;Controller&NewLine;public class HelloLoggingController &lbrace;&NewLine;&NewLine; static Log log &equals; LogFactory&period;getLog&lpar;HelloLoggingController&period;class&period;getName&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; &commat;RequestMapping&lpar;"&sol;hello"&rpar;&NewLine; public ModelAndView sayLogiingHello&lpar;&rpar;&lbrace;&NewLine; &sol;&sol;log it via log4j&NewLine; if&lpar;logger&period;isDebugEnabled&lpar;&rpar;&rpar;&lbrace;&NewLine; logger&period;debug&lpar;"Start debug"&rpar;&semi;&NewLine; &rcub;&NewLine; logger&period;info&lpar;"Going to run HelloLoggingController class"&rpar;&semi;&NewLine; Map model &equals; new HashMap&lpar;&rpar;&semi;&NewLine; model&period;put&lpar;"message"&comma; "Hello world Example with Logging"&rpar;&semi;&NewLine; model&period;put&lpar;"author"&comma; "Dinesh on Java"&rpar;&semi;&NewLine; logger&period;info&lpar;"Exiting the program"&rpar;&semi;&NewLine; return new ModelAndView&lpar;"helloWorld"&comma; model&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>You have to make sure that you included <i><b>commons-logging-x&period;y&period;z&period;jar<&sol;b><&sol;i> file in your project before deploying the program&period;<&sol;p>&NewLine;<p>Now keeping rest of the configuration and content unchanged in the above example&comma; if you deploy your application you will get the similar result what you got using <i><b>Log4J<&sol;b><&sol;i> API&period;<&sol;p>&NewLine;<h2><b>Download SourceCode&plus;Libs<&sol;b><br &sol;>&NewLine;<a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;site&sol;dinesh9582486434&sol;my-forms&sol;Spring3LoggingApp&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer"><b>SpringLoggingApp&period;zip<&sol;b><&sol;a><&sol;h2>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Spring Related Topics you may like<&sol;b><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;interview-questions-on-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring Interview Questions and Answers<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP Interview Questions and Answers<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-mvc-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Interview Questions<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Interview Questions and Answers<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-rest-web-services-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring REST Interview Questions and Answers<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-boot-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring Boot Interview Questions and Answers<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-boot-microservices-interview-questions-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring Boot Microservices Interview Questions and Answers<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;dependency-injection-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Dependency Injection &lpar;DI&rpar; in Spring <&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-ioc-container&sol;"><span style&equals;"color&colon; red&semi;">Spring IoC Container<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;what-is-bean-factory-in-spring&sol;"><span style&equals;"color&colon; red&semi;">What is Bean Factory in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;application-context-in-spring&sol;"><span style&equals;"color&colon; red&semi;">ApplicationContext in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;bean-autowiring-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Bean Autowiring in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;understanding-bean-scopes&sol;"><span style&equals;"color&colon; red&semi;">Spring Bean Scopes<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;create-custom-bean-scope-in-spring-example&sol;"><span style&equals;"color&colon; red&semi;">Create Custom Bean Scope in Spring Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-applicationcontextaware-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Using ApplicationContextAware in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;bean-lifecycle-and-callbacks&sol;"><span style&equals;"color&colon; red&semi;">Spring Bean Life Cycle and Callbacks<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;writing-beanpostprocessor-in-spring&sol;"><span style&equals;"color&colon; red&semi;">BeanPostProcessor in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;writing-beanfactorypostprocessor-in&sol;"><span style&equals;"color&colon; red&semi;">BeanFactoryPostProcessor in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;annotations-in-spring-and-based&sol;"><span style&equals;"color&colon; red&semi;"> Annotations in Spring and Based Configuration<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-jsr-250-annotations&sol;"><span style&equals;"color&colon; red&semi;">Spring JSR-250 Annotations<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsr-330-annotations-in-spring&sol;"><span style&equals;"color&colon; red&semi;">JSR 330 Annotations in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;stereotype-annotations-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring &commat;Component&comma; &commat;Repository&comma; &commat;Service and &commat;Controller Stereotype Annotations<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;method-injection-with-spring-using-lookup-method-property&sol;"><span style&equals;"color&colon; red&semi;">Method injection with Spring using Lookup method property<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-aop-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP-Introduction to Aspect Oriented Programming<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aspect-annotation&sol;"><span style&equals;"color&colon; red&semi;">&commat;Aspect Annotation in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-before-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;Before Annotation Advice Example<br &sol;>&NewLine;<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-before-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP Before Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-after-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;After Annotation Advice Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-after-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP After Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-after-returning-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;AfterReturning Annotation Advice Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-after-returning-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP After-Returning Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-after-throwing-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;AfterThrowing Annotation Advice Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-after-throwing-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP After Throwing Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-around-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;Around Annotation Advice Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-around-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP Around Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;understanding-aop-proxies-chapter-31&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP Proxies in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-transaction-management-in&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP Transaction Management in Hibernate<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;transaction-management-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring Transaction Management<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;declarative-transaction-management&sol;"><span style&equals;"color&colon; red&semi;">Spring Declarative Transaction Management Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-ordering-of-aspects-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP-Ordering of Aspects with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-java-based-configuration-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Java Based Configuration with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-xml-namespace-configuration-example&sol;"><span style&equals;"color&colon; red&semi;">Spring Security XML Namespace Configuration Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<div style&equals;"text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<&sol;b><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;">Spring Web MVC Framework<&sol;a><&sol;b><b> &vert;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;">index<&sol;a>&vert; <&sol;b><b style&equals;"background-color&colon; white&semi; color&colon; &num;4c1130&semi; font-family&colon; Arial&comma;Tahoma&comma;Helvetica&comma;FreeSans&comma;sans-serif&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 18px&semi; orphans&colon; 2&semi; text-align&colon; left&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; white-space&colon; normal&semi; widows&colon; 2&semi; word-spacing&colon; 0px&semi;"><b style&equals;"color&colon; &num;4c1130&semi;"><a style&equals;"color&colon; &num;33aaff&semi; text-decoration&colon; underline&semi;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;">Spring Batch Process with Example<&sol;a><&sol;b><&sol;b><b>&gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-30-mvc-hello-world-example&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '623'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago