提前感谢,我正在尝试使用 log4j 将日志写入文件,我已经尝试了所有 log4j.properties 但没有任何效果。下面是代码。
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class Main {
final static Logger logger = LogManager.getLogger(Main.class);
public static void main(String[] args) throws IOException {
logger.info("Hello and welcome!");
// Press Shift+F10 or click the green arrow button in the gutter to run the code.
for (int i = 1; i <= 50; i++) {
logger.warn("This is warn : " + i);
logger.error("This is error : " + i);
logger.fatal("This is fatal : " + i);
}
}
}
这里是放在 src/main/resources/log4j.properties 下的 log4j.properties 文件
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender log4j.appender.file.File=C:\logs\mylog.log log4j.appender.file.ImmediateFlush=true log4j.appender.file.threshold=信息 log4j.appender.file.Append=true log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=20 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy log4j.appender.file.rollingPolicy.FileNamePattern=C:\logs\mylog%d.log 虚拟机配置: -Dlog4j2.debug=true-Dlog4j2.configurationFile=C:\Users**** 资源\log4j.properties
pom.xml => log4j-api 和 log4j-core 作为依赖项。
使用这个并在其中设置您的日志路径,它将起作用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%p] %c{1} %m%n"/>
</layout>
</appender>
<appender name="info" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName" value="YOUR_LOG_FOLDER_PATH/log4j-current.log" />
<param name="FileNamePattern" value="YOUR_LOG_FOLDER_PATH/log4j-%d{HH-mm}.%i.log.gz" />
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="1000" />
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
</layout>
</appender>
<!--CHANGE name TO ROOT PACKAGE NAME OF YOUR PROJECT THAT YOU WANT TO LOG.-->
<logger name="org.example" additivity="false">
<level value="debug"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="info"/>
</logger>
<!-- <logger name="org.springframework" additivity="false">-->
<!-- <level value="info"/>-->
<!-- <appender-ref ref="consoleAppender"/>-->
<!-- <appender-ref ref="info"/>-->
<!-- </logger>-->
<!-- <logger name="org.hibernate" additivity="false">-->
<!-- <level value="info"/>-->
<!-- <appender-ref ref="consoleAppender"/>-->
<!-- <appender-ref ref="info"/>-->
<!-- </logger>-->
<root>
<priority value="INFO"></priority>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="info"/>
</root>
</log4j:configuration>