java - How to flush a buffered log4j FileAppender without shutdown? -
this question has answer here:
- how flush buffered log4j fileappender? 6 answers
i need flush log disk , turn normal buffer mode.
the requirement need generate hourly log. example, user_info.2017-09-08-18.log. every hourly generate one. use scheduledexecutorservice write simple string log @ beginning of each hour. found if buffer not full, log still in buffer. want write 1 log @ beginning of each hour, make sure every hour has log. logger.shutdown() not proper method in situation.
this properties definition:
log4j.appender.user_push_info=org.apache.log4j.dailyrollingfileappender log4j.appender.user_info.file=/data/log/user_info log4j.appender.user_info.datepattern='.'yyyy-mm-dd-hh'.log' log4j.appender.user_info.layout=org.apache.log4j.patternlayout log4j.appender.user_info.layout.conversionpattern=%m%n log4j.appender.user_info.bufferedio=true log4j.appender.user_info.buffersize=8192
for log4j 1.2, can set immediate flush come normal behavior with:
logger logger = logger.getrootlogger(); appender appender = logger.getappender("name"); if ( appender instanceof writerappender ) { ( (writerappender) appender ).setimmediateflush(true); logger.info("flushing log"); ( (writerappender) appender ).setimmediateflush(false); }
Comments
Post a Comment