Sunday, May 17, 2015

Log4j configuration in Java applications

Hello Guys,
This time I come with a very simple, useful and very common requirement that all of our Java developers have.

I used System.out.println extensively when I was new to Java world but after few years(1 or 2 may be) :) I realized that it is not really a good idea to use it in production ready applications. Then slowly moved to Log4j for all logging requirements and made my life simple.

So here I am giving you the configuration file that I am currently using for all my applications. I don't change this file unless my application demands it. I know it is very simple and everyone knows about what I am writing here but I still feel that it would be handy when you guys really need it. I configure this based on my regular requirements and feel free to change this on need basis

Small notes:
  • You can see log4j internal debug statements by enabling  log4j.debug property
  • It basically writes all (INFO, DEBUG, ERROR) level logs into separate files
  • They role the files on daily basis
  • You can change the file paths based on your folder structures
  • Change the package name to enable DEBUG level to your application logging

#log4j.debug=true
# Root logger option
log4j.rootLogger=INFO, infoappend, debugappend, errorappend
log4j.logger.<Give your package name>=DEBUG

# Only DEBUG
log4j.appender.debugappend=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debugappend.File=/opt/apache-tomcat-7.0.40/logs/app_DEBUG.log
log4j.appender.debugappend.DatePattern='.'yyyy-MM-dd
log4j.appender.debugappend.layout=org.apache.log4j.PatternLayout
log4j.appender.debugappend.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.debugappend.filter.A=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.debugappend.filter.A.LevelMin=DEBUG   
log4j.appender.debugappend.filter.A.LevelMax=DEBUG
log4j.appender.debugappend.filter.A.AcceptOnMatch=true

# Only INFO
log4j.appender.infoappend=org.apache.log4j.DailyRollingFileAppender
log4j.appender.infoappend.File=/opt/apache-tomcat-7.0.40/logs/app_INFO.log
log4j.appender.infoappend.DatePattern='.'yyyy-MM-dd
log4j.appender.infoappend.layout=org.apache.log4j.PatternLayout
log4j.appender.infoappend.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.infoappend.filter.A=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.infoappend.filter.A.LevelMin=INFO   
log4j.appender.infoappend.filter.A.LevelMax=INFO
log4j.appender.infoappend.filter.A.AcceptOnMatch=true

# Only ERROR
log4j.appender.errorappend=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorappend.DatePattern='.'yyyy-MM-dd
log4j.appender.errorappend.File=/opt/apache-tomcat-7.0.40/logs/app_ERROR.log
log4j.appender.errorappend.layout=org.apache.log4j.PatternLayout
log4j.appender.errorappend.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.errorappend.filter.A=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.errorappend.filter.A.LevelMin=ERROR   
log4j.appender.errorappend.filter.A.LevelMax=ERROR
log4j.appender.errorappend.filter.A.AcceptOnMatch=true

#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log

# Direct log messages to a log file and role it based on it's size
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C://app.log
#log4j.appender.file.MaxFileSize=10MB
#log4j.appender.file.MaxBackupIndex=10

# Direct log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


Sunday, February 8, 2015

HUDSON + SonarQube

Hello,
In my last post i mentioned about HUDSON and now I am going to extend this to get best out of it

  • I configured HUDSON to poll my SVN repository for every 5 minutes to check any recent changes are there and to prepare a build with those changes and deploy it to Tomcat container (It is pretty straight forward. i certainly used HUDSON's plugins)
So building and deploying changes continuously task is given to HUDSON and it is happily doing without any rest. Now it is time to automate QA checks to make sure that builds are having valid and quality code. 

SonarQube is an open platform to manage your code quality. It is a one stop solution for all your quality related tasks( static checks, dynamic checks, quality reports, code duplicates, comments, coding rules, complexity..etc) It covers more than 20 programming languages like Java, C, C++...etc. You can basically analyze code by it's own Sonar analyzer or you can even use Maven analyzer(can configure to analyze with many plugins like Findbugs, PMD...etc ) and then your results will be stored into your favorite database automatically. You will be provided a nice dash board where you can see all the reports at once and take actions accordingly. Actually you can do a lot better with this great combination.

I will write another blog on how to setup SonarQube. Please do visit again.

Thursday, January 29, 2015

Configure HUDSON CI tool in Tomcat

I thought of automating the build and deployment process to reduce  the efforts that my team spends and minimize the errors that we see later while integrating and deploying to production by adopting one Continuous Integration (CI) tool into my project. I chose HUDSON as it is very simple to configure, opensource, widely adopted and I used it in one of my previous projects (5 years back). Here are the steps to configure Hudson in Tomcat

·         Download Hudson.war file from http://eclipse.org/hudson/download.php
·         Go through the best practices http://wiki.eclipse.org/Hudson-ci/Hudson_Best_Practices
·         Deploy the war file into your favorite web container(Tomcat, JBoss..etc)
·         You may need to tweak your JVM memory options like set "JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx1024m -XX:MaxPermSize=256m -server" https://docs.oracle.com/cd/E40518_01/integrator.311/integrator_install/src/cli_ldi_server_config.html
·         Start your web container and open it in browser (http://localhost:8080/hudson-3.2.1/)
·         Select or unselect required plugins and enter your proxy details if you are under proxy

·         It will download those plugins and will be ready to start play around it