当我刚开始学习log4j时,这个项目只是一个用于打印日志信息的log4j-demo。并希望使用不同的信息打印控制台日志和文件日志。这应该不难,但我不知道它失败的原因。
希望你能帮助我解决这个问题。感谢。
这是log4j.properties:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, console, file
# Define the console appender
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold = DEBUG
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layput.conversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p -%C- - %m%n
## Define the file appender
log4j.appender.file = org.apache.log4j.FileAppender
log4j.appender.file.Threshold = INFO
log4j.appender.file.File = ./log.out
log4j.appender.file.Append = true
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.conversionPattern = %m%n
这是log4j主类:
import org.apache.log4j.Logger;
public class Log4jExample {
static Logger log = Logger.getLogger(Log4jExample.class.getName());
public static void main(String[] args){
log.trace("Trace Message!");
log.debug("Debug Message!");
log.info("Info Message!");
log.warn("Warn Message!");
log.error("Error Message!");
log.fatal("Fatal Message!");
}
}
这是pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yizhang</groupId>
<artifactId>log4j-demo</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Log4jExample</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
这是输出图像: screencut
答案 0 :(得分:0)
修正拼写错误layput
。
# Define the console appender
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold = DEBUG
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p -%C- - %m%n