java - How to flush a buffered log4j FileAppender without shutdown? -


this question has answer here:

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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -