I am building a Java project to test performance of log4j and logback. The test codes for these 2 utility are the same via slf4j:
package com.boco.logb;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggerOneThread {
public static final long TOTAL_TIMES = 4000 * 1000;
private static final Logger logger = LoggerFactory
.getLogger(LoggerOneThread.class);
public static void main(String[] args) {
long start = System.nanoTime();
for (int i = 1; i < TOTAL_TIMES; i++) {
logger.debug("This is a test message. This is another test message.");
}
long estimatedTime = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()
- start);
System.out.println("Write " + TOTAL_TIMES + " logs takes time "
+ estimatedTime + " s.");
}
}
When I add log4j.properties and corresponding jar files ('log4j:log4j:1.2.17' and 'org.slf4j:slf4j-log4j12:1.7.7') into classpath, this program will produces log4j logs. The log4j.properties file:
log4j.rootLogger = DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=log4j.log
log4j.appender.FILE.Append=true
log4j.appender.FILE.MaxFileSize=50MB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{MM-dd HH:mm:ss.SSS} [%t] %p %c - %m%n
While if the logback.xml and corresponding jars ('ch.qos.logback:logback-classic:1.1.2', 'ch.qos.logback:logback-core:1.1.2') are added into classpath, it will produce logback logs. The logback.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logback.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logback.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>11</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date{MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
I tried to build these 2 configuration with Gradle, so I can run them respectively with:
gradle run log4j
gradle run logback
my build.gradle file is:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
sourceSets {
log4j {
java {
srcDir 'src'
}
resources {
srcDir 'res/log4j'
}
output.dir 'build/classes/log4j'
}
logback {
java {
srcDir 'src'
}
resources {
srcDir 'res/logback'
}
output.dir 'build/classes/logback'
}
}
mainClassName = "com.boco.logb.LoggerOneThread"
sourceCompatibility = 1.6
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
log4jCompile 'log4j:log4j:1.2.17', 'org.slf4j:slf4j-log4j12:1.7.7'
log4jRuntime 'log4j:log4j:1.2.17', 'org.slf4j:slf4j-log4j12:1.7.7'
logbackCompile 'ch.qos.logback:logback-classic:1.1.2', 'ch.qos.logback:logback-core:1.1.2'
logbackRuntime 'ch.qos.logback:logback-classic:1.1.2', 'ch.qos.logback:logback-core:1.1.2'
}
But when I ran gradle classes
, there were no class files created in build/classes/log4j or build/classes/logback. How to config so I can run log4j and logback respectively? Thanks.