本帖最后由 ilovecomputer66 于 2024-2-18 17:20 编辑
目的是想实现,为每场战斗,打印输出到以battleId(我下面代码中,battleId用UUID代替生成)为文件名的log文件中,从而方便测试。我按照网上方法,这么写的。结果无效,不会生成到自己指定的log文件中
Main.java
[Java] 纯文本查看 复制代码
import java.util.UUID;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.filter.LevelFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.util.StatusPrinter;
public class Main {
public static void main(String[] args) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger logger = (Logger) LoggerFactory.getLogger(Main.class);
// 创建一个FileAppender
FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
fileAppender.setContext(loggerContext);
// 设置encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %logger{36} - %msg%n");
encoder.start();
// 设置FileAppender的文件名
fileAppender.setFile("/log/specificLog/" + UUID.randomUUID().toString());
fileAppender.setEncoder(encoder);
fileAppender.start();
// 设置FileAppender的日志级别
fileAppender.addFilter(createLevelFilter(Level.INFO));
// 添加FileAppender到Logger中
logger.addAppender(fileAppender);
// 打印logback的内部状态
StatusPrinter.print(loggerContext);
logger.info("info");
logger.error("error");
// 移除FileAppender
logger.detachAppender(fileAppender);
// 停止encoder和FileAppender
encoder.stop();
fileAppender.stop();
// 打印logback的内部状态
StatusPrinter.print(loggerContext);
}
// 创建LevelFilter
private static LevelFilter createLevelFilter(Level level) {
LevelFilter levelFilter = new LevelFilter();
levelFilter.setLevel(level);
levelFilter.setOnMatch(FilterReply.ACCEPT);
levelFilter.setOnMismatch(FilterReply.DENY);
levelFilter.start();
return levelFilter;
}
}
logback的配置文件:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="UTF-8"?>
<!-- 勿对此文件进行格式整理,eclipse自动整理会换行<encoder><pattern>中的格式,导致log也被换行 -->
<configuration>
<appender name="console"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>log/MainLog.log</File>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>log/MainLog-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>365</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>1000MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
<charset class="java.nio.charset.Charset">UTF-8</charset>
</encoder>
</appender>
<root>
<level value="error" />
<level value="warn" />
<level value="info" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>
</configuration>
通过看打印出来的logback内部状态。最后有一个FileAppender[null]
11:15:57,649 |-INFO in ch.qos.logback.core.FileAppender[null] - File property is set to [/log/specificLog/29f1e240-07a1-401b-9226-2d6547623721]
|