[英]Not able to create a log file using log4j2
I am relatively new to log4j2. 我对log4j2比较陌生。 I am trying to create a log file using log4j2 but it is not getting created. 我正在尝试使用log4j2创建一个日志文件,但是没有被创建。 Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance. 提前致谢。
log4j.xml Configuration File(below): log4j.xml配置文件(如下):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
<Properties>
<Property name="basePath">../resources/logs</Property>
</Properties>
<Appenders>
<!-- File Appender -->
<File name="FILE" fileName="${basePath}/logfile.log" append="true">
<PatternLayout
pattern="[%-5p][%d{dd-MMM-yyyy HH:mm:ss a}] %C{0}.%M:(%L) - %m%n" />
</File>
<!-- Console Appender -->
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5p][%d{dd-MMM-yyyy HH:mm:ss a}] %C{0}.%M:(%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="log" level="all" />
<Root level="all">
<AppenderRef ref="FILE" level="all" />
<AppenderRef ref="STDOUT" level="all" />
</Root>
</Loggers>
</Configuration>
Project Folder Structure(below): 项目文件夹结构(如下):
My Java Code(below): 我的Java代码(如下):
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Demo {
public static Logger log;
public static void main(String[] args) {
System.err.println("Start");
System.setProperty("log4j.configurationFile", "./resources/config/logger/log4j2.xml");
log = LogManager.getLogger(Demo.class.getName());
System.err.println("Initialisied Logger");
log.trace("TRACE");
log.debug("DEBUG");
log.info("INFO");
log.warn("WARN");
log.error("ERROR");
log.fatal("FATAL");
System.err.println("END");
}
}
Maven Dependency(below): Maven依赖关系(如下):
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
Console Outout(below): 控制台输出(如下):
Start
Initialisied Logger
[TRACE][28-Feb-2018 14:45:11 PM] Demo.main:(17) - TRACE
[DEBUG][28-Feb-2018 14:45:11 PM] Demo.main:(18) - DEBUG
[INFO ][28-Feb-2018 14:45:11 PM] Demo.main:(19) - INFO
[WARN ][28-Feb-2018 14:45:11 PM] Demo.main:(20) - WARN
[ERROR][28-Feb-2018 14:45:11 PM] Demo.main:(21) - ERROR
[FATAL][28-Feb-2018 14:45:11 PM] Demo.main:(22) - FATAL
END
Had similar issues. 有类似的问题。 It would be easier to just show you a working code, I'll give you mine below ;) 仅向您显示有效代码会更容易,我在下面为您提供;)
pom.xml 的pom.xml
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>log4j2.xml</include>
</includes>
</resource>
</resources>
Demo.java Demo.java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Demo {
public static Logger log;
public static void main(String[] args) {
System.err.println("Start");
private static final Logger log = LogManager.getLogger(Demo.class);
System.err.println("Initialisied Logger");
log.trace("TRACE");
log.debug("DEBUG");
log.info("INFO");
log.warn("WARN");
log.error("ERROR");
log.fatal("FATAL");
System.err.println("END");
}
}
It is better if you would copy your xml log file inside src/main/resources
. 最好将xml日志文件复制到src/main/resources
。 log4j 2 - configuration issue log4j 2-配置问题
(i just took a random question about log4j2, because there are thousands, this would have helped you solved but i believe the code i posted here would help you too). (我只是问了一个关于log4j2的随机问题,因为有成千上万个,这本可以帮助您解决的,但是我相信我在这里发布的代码也会对您有所帮助)。
UPDATE: I forgot to paste a working log4j2.xml
file. 更新:我忘了粘贴一个有效的log4j2.xml
文件。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<RollingFile name="RollingFile" filename="/var/log/mylog.log"
filepattern="${logPath}/%d{YYYYMMddHHmmss}-fargo.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
UPDATE 2: And, since everybody love screenshots, you'd have this folder structure. 更新2:而且,由于每个人都喜欢屏幕截图,因此您将拥有此文件夹结构。 Netbeans, Eclipse ... Names could change, but src/main/resources
and pom.xml
are the same across all IDE. Netbeans,Eclipse ...名称可能会更改,但是src/main/resources
和pom.xml
在所有IDE中都相同。
Solved it by replacing the two dots to one dot ie ../resources
to ./resources
I know it is a silly mistake. 通过将两个点替换为一个点来解决它,即../resources
到./resources
我知道这是一个愚蠢的错误。 :-) :-)
<Properties>
<Property name="basePath">../resources/logs</Property>
</Properties>
Credit to @vikingsteve for pointing it out in the comments. 感谢@vikingsteve在评论中指出它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.