我有2个批处理,当我单独运行时,我需要将hibernate和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 logger以便在文件中接收它们的日志。问题是,当我这样做时,即使我不运行BatchB,创建的BatchB日志文件将从BatchA接收hibernate / spring日志。 如何在运行关联的批处理时避免这种情况并填写批处理日志文件?
非常感谢
答案 0 :(得分:0)
通过以编程方式添加hibernate和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);
}
}
}