我正在使用Gradle创建一个简单的Web应用程序。我使用slf4j和log4j作为记录器,我使用gradle.build文件添加3个罐子来使用slf4j。我经历了一些gradle和slf4j教程,但仍然无法找到并回答将log4j.properties放在gradle项目中的位置。任何人都能帮助我吗?
答案 0 :(得分:9)
log4j.properties
通常是从网络应用的类路径中读取的,因此您可以将其放入src/main/resources
。
答案 1 :(得分:6)
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.apache.log4j.Logger;
public class Log4jTest {
//Slf4J
//final static Logger logger = LoggerFactory.getLogger(Log4jTest.class);
//Log4J
final static Logger logger = Logger.getLogger(Log4jTest.class);
public static void main(String[] args) {
logger.info("Logging with Log 4J starts");
Log4jTest obj = new Log4jTest();
try{
obj.divide();
}catch(ArithmeticException ex){
logger.error("Sorry, something wrong!", ex);
}
}
private void divide(){
int i = 10 /0;
}
}
** Log4J属性文件**
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Redirect 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
# Rirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=E:\\Logging\\log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
** build.gradle **
的依赖关系dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'log4j:log4j:1.2.17'
compile 'org.slf4j:slf4j-log4j12:1.7.18'
}