java 在 Maven 项目中使用 log4j

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13302252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:12:20  来源:igfitidea点击:

Using log4j in maven project

javamaven

提问by user1790894

I have trouble creating the log file

我在创建日志文件时遇到问题

I added in my pom.xml

我在我的 pom.xml 中添加

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
</dependency>

then I placed log4j.properies under resources folder

然后我将 log4j.properies 放在资源文件夹下

# Define the root logger with appender file
log = /home/soumya/log4j
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Then in My java code I use

然后在我的java代码中我使用

public class Test
{
static Logger logger = Logger.getLogger(Test.class.getName());
 public void testLog(){
 logger.info("testing ..!!!");
}

But when running I got below error and the log file is not created !!!

但是在运行时出现以下错误并且未创建日志文件!!!

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation

My maven version is Apache Maven 2.2.1 (rdebian-6) and I am using ecclipse Indigo Appreciate any help

我的 maven 版本是 Apache Maven 2.2.1 (rdebian-6),我正在使用 ecclipse Indigo 感谢任何帮助

回答by Vito Limandibhrata

SLF4j library needs to be included manually in your pom.xml

SLF4j 库需要手动包含在你的 pom.xml 中

        <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.6.1</version>
        </dependency>
        <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.6.1</version>
        </dependency>
        <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.6.1</version>
        </dependency>