将日志保存到不同批次的文件中
save log into a file for different batchs
我有 2 个批次,我需要将休眠和 spring 日志保存到单独的文件中,当我 运行 它们分开时
所以我有这样的 logback 配置
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<!-- Batch 1 -->
<appender name="BatchALogToFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logback.root.path}/batchA.log</file>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d{dd/MM/yyyy : HH:mm:ss}] %m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<minIndex>1</minIndex>
<maxIndex>30</maxIndex>
<fileNamePattern>${logback.root.path}/batchA.%i.log</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>20MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="BatchALogger" additivity="false">
<level value="DEBUG" />
<appender-ref ref="BatchALogToFile" />
<appender-ref ref="STDOUT" />
</logger>
<!-- End Batch1 -->
<!-- Batch2 -->
<appender name="BatchBLogToFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logback.root.path}/batchB.log</file>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d{dd/MM/yyyy : HH:mm:ss}] %m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<minIndex>1</minIndex>
<maxIndex>30</maxIndex>
<fileNamePattern>${logback.root.path}/batchB.%i.log</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>20MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="BatchBLogger" additivity="false">
<level value="DEBUG" />
<appender-ref ref="BatchBLogToFile" />
<appender-ref ref="STDOUT" />
</logger>
<!-- End Batch2 -->
<logger name="org.hibernate" level="warn">
<appender-ref ref="BatchALogToFile" />
<appender-ref ref="BatchBLogToFile" />
</logger>
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace">
<appender-ref ref="BatchALogToFile" />
<appender-ref ref="BatchBLogToFile" />
</logger>
<logger name="org.springframework" level="info">
<appender-ref ref="BatchALogToFile" />
<appender-ref ref="BatchBLogToFile" />
</logger>
<root level="${logback.root.level}">
<appender-ref ref="STDOUT" />
</root>
</configuration>
我需要将两个 appender 都附加到 hibernate/spring 记录器,以便在文件中接收它们的日志。问题是当我这样做时,即使我没有 运行 BatchB,创建的 BatchB 日志文件也会从 BatchA 接收 hibernate/spring 日志。
我怎样才能避免这种情况并仅在我 运行 关联批次时才填充批处理日志文件?
非常感谢
通过以编程方式添加休眠和 spring 日志解决
public static void addHibernateAndSpringLogs(String loggerName, String appenderName) {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
Appender<ILoggingEvent> saveLogToFile = null;
for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
if (logger.getName()
.equals(loggerName)) {
for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
Appender<ILoggingEvent> appender = index.next();
if (appender.getName()
.equals(appenderName)) {
saveLogToFile = appender;
}
}
}
if (logger.getName()
.startsWith(HIBERNATE_LOGGER)
|| logger.getName()
.startsWith(SPRING_LOGGER)) {
logger.addAppender(saveLogToFile);
}
}
}
我有 2 个批次,我需要将休眠和 spring 日志保存到单独的文件中,当我 运行 它们分开时
所以我有这样的 logback 配置
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<!-- Batch 1 -->
<appender name="BatchALogToFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logback.root.path}/batchA.log</file>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d{dd/MM/yyyy : HH:mm:ss}] %m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<minIndex>1</minIndex>
<maxIndex>30</maxIndex>
<fileNamePattern>${logback.root.path}/batchA.%i.log</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>20MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="BatchALogger" additivity="false">
<level value="DEBUG" />
<appender-ref ref="BatchALogToFile" />
<appender-ref ref="STDOUT" />
</logger>
<!-- End Batch1 -->
<!-- Batch2 -->
<appender name="BatchBLogToFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logback.root.path}/batchB.log</file>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d{dd/MM/yyyy : HH:mm:ss}] %m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<minIndex>1</minIndex>
<maxIndex>30</maxIndex>
<fileNamePattern>${logback.root.path}/batchB.%i.log</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>20MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="BatchBLogger" additivity="false">
<level value="DEBUG" />
<appender-ref ref="BatchBLogToFile" />
<appender-ref ref="STDOUT" />
</logger>
<!-- End Batch2 -->
<logger name="org.hibernate" level="warn">
<appender-ref ref="BatchALogToFile" />
<appender-ref ref="BatchBLogToFile" />
</logger>
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace">
<appender-ref ref="BatchALogToFile" />
<appender-ref ref="BatchBLogToFile" />
</logger>
<logger name="org.springframework" level="info">
<appender-ref ref="BatchALogToFile" />
<appender-ref ref="BatchBLogToFile" />
</logger>
<root level="${logback.root.level}">
<appender-ref ref="STDOUT" />
</root>
</configuration>
我需要将两个 appender 都附加到 hibernate/spring 记录器,以便在文件中接收它们的日志。问题是当我这样做时,即使我没有 运行 BatchB,创建的 BatchB 日志文件也会从 BatchA 接收 hibernate/spring 日志。 我怎样才能避免这种情况并仅在我 运行 关联批次时才填充批处理日志文件?
非常感谢
通过以编程方式添加休眠和 spring 日志解决
public static void addHibernateAndSpringLogs(String loggerName, String appenderName) {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
Appender<ILoggingEvent> saveLogToFile = null;
for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
if (logger.getName()
.equals(loggerName)) {
for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
Appender<ILoggingEvent> appender = index.next();
if (appender.getName()
.equals(appenderName)) {
saveLogToFile = appender;
}
}
}
if (logger.getName()
.startsWith(HIBERNATE_LOGGER)
|| logger.getName()
.startsWith(SPRING_LOGGER)) {
logger.addAppender(saveLogToFile);
}
}
}