在maven项目中使用log4j

时间:2012-11-09 04:47:19

标签: java maven

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

我在我的pom.xml中添加了

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

然后我将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

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

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

但是在运行时我遇到了以下错误并且没有创建日志文件!!!

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

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

1 个答案:

答案 0 :(得分:3)

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

        <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>