Logback: Logging revisited
I have written about this before here. This is not really mandatory to read in order to make sense of the current article, but it might help to give it a cursory look, to set the context for this article.
File: MavenCommands.bat
1 2 3 4 | call mvn archetype:create ^ -DarchetypeGroupId=org.apache.maven.archetypes ^ -DgroupId=org.academy ^ -DartifactId=logger |
This unfortunately is preloaded with JUnit 3. I set up JUnit 4 and also add Contiperf, so that I could run the tests multiple times – something that would come in handy if I were to check performance.
File: /logger/pom.xml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | [...] UTF-8 4.10 2.2.0 [...] [...] junit junit ${junit.version} test org.databene contiperf ${contiperf.version} test |
Also, I like to explicitly control the java version that is being used to compile and execute my code.
File: /logger/pom.xml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | [...] 2.0.2 1.7 [...] org.apache.maven.plugins maven-compiler-plugin ${maven-compiler-plugin.version} ${java.version} ${java.version} |
Last of configurations – for the time being. Slap on surefire to run unit tests.
File: /logger/pom.xml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | [...] 2.12 [...] org.apache.maven.plugins maven-surefire-plugin ${maven-surefire-plugin.version} org.apache.maven.surefire surefire-junit47 ${maven-surefire-plugin.version} -XX:-UseSplitVerifier |
Please note, I have taken the pains of adding all these dependencies to this article with their versions, just to ensure that should you try this yourself, you know exactly what was the software configuration of my test.
Now, let us finally add the unit tests.
File: /logger/src/test/java/org/academy/AppTest.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | public class AppTest { private final static Logger logger = LoggerFactory .getLogger(AppTest. class ); @Rule public ContiPerfRule i = new ContiPerfRule(); @Test @PerfTest (invocations = 10 , threads = 1 ) @Required (max = 1200 , average = 1000 ) public void test() { for ( int i = 0 ; i< 10000 ; i++){ logger.debug( "Hello {}" , "world." ); } } } |
So, we have used the logger in my unit test but have not added an implementation of logger. What I intend to do is to add log4j (with slf4j) and logback (with inherent support of slf4j) one by one and run this simple test multiple times to compare performance.
To add log4j I used this setting.
File: /logger/pom.xml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | org.slf4j slf4j-api ${slf4j.version} org.slf4j jcl-over-slf4j ${slf4j.version} runtime org.slf4j slf4j-log4j12 ${slf4j.version} runtime |
and for logback I used this setting.
File: /logger/pom.xml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | ch.qos.logback logback-classic ${logback.version} |
with the following versions.
File: /logger/pom.xml
1 2 3 4 5 6 | 1.6.1 1.0.6 |
For either of these logger framework to actually log anything you will have to add a file telling loggers what to log and where.
File: src/main/resources/log4j.properties
1 2 3 4 5 6 7 | # Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=DEBUG, A1 # configure A1 to spit out data in console log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n |
Framework | 1st run | 2nd run | 3rd run |
---|---|---|---|
Logback | 0.375 seconds | 0.375 seconds | 0.406 seconds |
Log4j | 0.454 seconds | 0.453 seconds | 0.454 seconds |
Reference: Logging revisited from our JCG partner Partho at the Tech for Enterprise blog.