我正在构建一个Java项目来测试log4j和logback的性能。这两个实用程序的测试代码是相同的,通过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.");
}
}
当我将log4j.properties和相应的jar文件('log4j:log4j:1.2.17'和'org.slf4j:slf4j-log4j12:1.7.7')添加到类路径中时,该程序将生成log4j日志。 log4j.properties文件:
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
如果将logback.xml和相应的jar('ch.qos.logback:logback-classic:1.1.2','ch.qos.logback:logback-core:1.1.2')添加到类路径中,它会产生回溯日志。 logback.xml文件:
<?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>
我尝试使用Gradle构建这两个配置,因此我可以分别使用以下命令运行它们:
gradle run log4j
gradle run logback
我的build.gradle文件是:
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'
}
但是当我运行gradle classes
时,在build / classes / log4j或build / classes / logback中没有创建类文件。如何配置,我可以分别运行log4j和logback?感谢。
答案 0 :(得分:1)
classess
任务只构建默认的来源&#34; main&#34;由Java
插件添加的源集。但是,当您添加新的源集时,Java
插件还会为每个已定义的源集添加<sourceSet>Classes
任务。尝试:
$ gradle log4jClasses
您可以在Gradle docs。
中找到有关其工作原理的更多信息