Using logback, I would like to start a new log every time an asynchronous job starts, so I need to trigger a rollover manually. But when I try to get the appender, I get null instead. Below is my configuration:
<configuration scan="true"> <timestamp key="time" datePattern="yyyy-MM-dd_HH_mm"/> <logger name="com.my.com.pany" level="DEBUG"> <appender name="TEST" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>logs/log_TEST_${time}.log</file> <triggeringPolicy class="com.my.com.pany.myapp.logging.ManualRollingPolicy"> </triggeringPolicy> <append>true</append> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> </logger> </configuration>
I trigger the rollover as follows:
ch.qos.logback.classic.Logger logF = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.my.com.pany"); RollingFileAppender<ILoggingEvent> appender = (RollingFileAppender<ILoggingEvent>) logF.getAppender("test"); appender.rollover();
I extended TimeBasedRollingPolicy<E> so that my log starts when I start async:
@NoAutoStart public class ManualRollingPolicy<E> extends TimeBasedRollingPolicy<E> { }
Can someone help me on this? EDIT: After some further research, I see that LogF has a size 1 appenderList that my custom RollingPolicy configured RollingPolicy . However, the name property of this appender is null, and I think that is why I cannot get it by name.
source share