Thursday, January 31, 2013

how to test log4j /SLF4j tutorial

Log4j is one very popular logging framework which is widely used by a lot java applications such as hadoop, tomcat. here is quick tutorial for programmers to setup the log4j and write some very basic logging applications.

   first, create a java application and download reference the log4j jar dependency, if you use maven, just add log4j to the dependency list in your pom.xml.

I have the maven eclipse plugin, so I can create one simple maven project.
image
give it a groupid and artifact id

image

right click pom.xml, and chose the maven menu, then add dependency, search log4j. and click to add one latest version to the dependency.

image

then the pom file loos like this

image

now the project has the log4j jar in the maven’s dependencies folder.
image

Now let’s create one simple class with one main method to do the logging.
basically, we just create one logger by calling the logmanaager,getlogger. then can log out some message by using different level settings.
image

now we can tell from the error message , there is no logger for com.androidyou. so we need to put some instruction into the log4j.properties , basically tell the runtime which logger I should use and for a giving logger, what’s the logging level. for each given logger, what appender we should use, then the properties for the appenders.
put the log4j.debug=true, you will see more verbal messages. it tells us that no resource file found (by default it’s the log4j.properties.)  we can override the resource file to whaterve file we have by passing the locatio to the log4j.configuration property.
image

Now we just create a very basic log4j.properties file under any classpath folder, I just put it under src/main/resources folder.

 
log4j.logger.com.androidyou=DEBUG
log4j.rootLogger=ERROR, console

# console is set to be a ConsoleAppender.
log4j.appender.console=org.apache.log4j.ConsoleAppender
 
# setup console properties

log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


run again, you can see the loading process, our resource file log4j.properties get picked up, and our logger also identified and level debug is assigned
image

for the logging configuration, there are different levels. here is the list, I checked the code declaration

image

Also there are a lot built-in appenders.
image

here is one example that we change the consoleappender to dailyrollingfileappender, which rotate the file by time setting.

log4j.logger.com.androidyou=DEBUG
log4j.rootLogger=ERROR, df

# console is set to be a DailyRollingFileAppender.
log4j.appender.df=org.apache.log4j.DailyRollingFileAppender
 
# setup DailyRollingFileAppender properties

log4j.appender.df.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.df.File=mmm.txt

log4j.appender.df.layout=org.apache.log4j.PatternLayout
log4j.appender.df.layout.ConversionPattern=ROOTJJJ-%-4r [%t] %-5p %c %x - %m%n

Besides the log4j. slf4j is another popular logging framwrok, it’s a fa├žade layer which works as a separate stable interface, slf4j-log4j is a implementation.  so code is almos the same, you only need change the pacakge namer from log4j to slf4j.

image

for the pom, API is the core library, log4j is the implementation. we can simply copy another impementation like no-op to the classpath, then no log will be gerated.

image

image

Also we can send the messages to the ActiveMQ Topics.
Change the log4.properties to add JMSappender.

log4j.rootLogger=ERROR, jms

log4j.appender.jms=org.apache.log4j.net.JMSAppender
log4j.appender.jms.InitialContextFactoryName=org.apache.activemq.jndi.ActiveMQInitialContextFactory
log4j.appender.jms.ProviderURL=tcp://localhost:61616
log4j.appender.jms.TopicBindingName=logTopic
log4j.appender.jms.TopicConnectionFactoryBindingName=ConnectionFactory


then define one jndi.properties file and put it under any classpath folder.
image

then reference all the jars under activemq lib for safe.

then you can see the messages in the topic we created.
image

under activemq examples, run ant consumer to consumer the topics,
image

log4j dependency error,

Just created one simple Maven project to test the log4j, simply add one dependency called log4j.  the run mvn dependency:resolve, get an error that might brings you here.

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project log4jexample: Could not resolve dependencies for project com.androidyou.test:log4jexample:jar:0.0.1-SNAPSHOT: The following
artifacts could not be resolved: javax.jms:jms:jar:1.1, com.sun.jdmk:jmxtools:jar:1.2.1, com.sun.jmx:jmxri:jar:1.2.1: Could not transfer artifact javax.jms:jms:

jar:1.1 from/to java.net (https://maven-repository.dev.java.net/nonav/repository): No connector available to access repository java.net (https://maven-repositor
y.dev.java.net/nonav/repository) of type legacy using the available factories WagonRepositoryConnectorFactory -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

Pom.xml

image

And the fix is easy, change the version 1.2.15->1.2.16 or 17
image

 
Locations of visitors to this page