我的项目有一个使用log4j实现的日志模块。在项目的春季启动版本为1.5.8之前,我最近将它升级到spring boot2.0打包项目后出错,根据在线解决方案,我更换了log4j log4j2,然后删除了spring boot默认日志记录模块。成功,程序可以正常运行。但是,日志模块功能有问题。它可以正确输出控制台上的日志,但不能将日志导出到文件中,但不能将所有日志信息输出到文件中。将日志输出到启动类中的文件是可以的。这是我的日志模块的主要代码
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.builder.api.*;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
public class LoggerUtil { static Logger logger = null;
public static void fatal(String message) {
if (logger != null) {
logger.fatal(message);
}
}
public static void debug(String msg) {
if (logger != null) {
logger.debug(msg);
}
}
public static void info(String message) {
if (logger != null) {
logger.info(message);
}
}
public static void warn(String message) {
if (logger != null) {
logger.warn(message);
}
}
public static void error(String message) {
if (logger != null) {
logger.error(message);
}
}
public static void initLogger(String filePrefix, String level) {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.ERROR);
builder.setConfigurationName("RollingBuilder");
// create a console appender
AppenderComponentBuilder appenderBuilder =
builder.newAppender("Stdout", "CONSOLE")
.addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT)
.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.DENY)
.addAttribute("level", Level.ERROR));
appenderBuilder.add(builder.newLayout("PatternLayout")
.addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
builder.add(appenderBuilder);
// create a rolling file appender
LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")
.addAttribute("pattern", "%d [%t] %-5level: %msg%n");
ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
.addComponent(builder.newComponent("CronTriggeringPolicy").addAttribute("schedule", "0 0 0 * * ?"));
appenderBuilder = builder.newAppender("rolling", "RollingFile")
.addAttribute("filePattern", filePrefix + "-%d{yyyyMMdd}.log")
.add(layoutBuilder)
.addComponent(triggeringPolicy);
builder.add(appenderBuilder);
// create the new logger
builder.add(builder.newLogger("Logger", Level.DEBUG)
.add(builder.newAppenderRef("rolling"))
.addAttribute("additivity", false));
builder.add(builder.newRootLogger(getLogLevel(level))
.add(builder.newAppenderRef("Stdout"))
.add(builder.newAppenderRef("rolling")));
LoggerContext ctx = Configurator.initialize(builder.build());
logger = ctx.getLogger(KnowledgelibapiApplication.class.getName());
}
private static Level getLogLevel(String level) {
switch (level.toUpperCase()) {
case "FATAL":
return Level.FATAL;
case "DEBUG":
return Level.DEBUG;
case "INFO":
return Level.INFO;
case "WARN":
return Level.WARN;
case "ERROR":
return Level.ERROR;
default:
return Level.WARN;
}
}
}
答案 0 :(得分:1)
将日志输出到文件的解决方案是在src / main / resources&amp;下创建log4j2.xml文件。使用类中的LogManager创建Logger,如下所示:
private static Logger logger = LogManager.getLogger(MyClass.class);
然后使用logger.info或logger.debug或logger.error将日志输出到文件。
示例log4j2.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<!-- Console Appender -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<!-- File Appender -->
<File name="File" fileName="d:/app.log">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<!-- Log everything in custom package -->
<Logger name="your base package" level="debug" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Logger>
</Loggers>
</Configuration>
希望这会对你有帮助......