[logback-user] Creating empty directory for log automatically

Daniel Rossi spam at electroteque.org
Thu Oct 25 14:16:11 CEST 2007


Hi there, im trying to setup loggers programatically and noticed that  
logback / log4j wont create the empty directory for you using  
rollingfileappender. Here is what ive had to do to get the parent  
directory recursively created because i wont be sure what the parent  
directory of the logger will be until its created. Is there a better  
way ?

			LoggerContext lc = (LoggerContext) logStats.getLoggerContext();
			W3CAccessLayout layout = new W3CAccessLayout();
			layout.setContext(lc);
			layout.start();
			
			RollingFileAppender<LoggingEvent> appender = new  
RollingFileAppender<LoggingEvent>();		
			appender.setContext(lc);
			appender.setImmediateFlush(true);
			appender.setName(appenderName);
     		appender.setLayout(layout);
     		
     		TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy();
			policy.setContext(lc);
			policy.setFileNamePattern(path + logFilePattern);
			policy.setParent(appender);
			policy.setCurrentTime(System.currentTimeMillis());
			policy.start();
			
			File file = new File(policy.getNewActiveFileName());
			File dir = new File(file.getParent());
			dir.mkdirs();
			appender.setRollingPolicy(policy);
			appender.start();
		
				
     		logStats.addAppender(appender);

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://qos.ch/pipermail/logback-user/attachments/20071025/7f323cd0/attachment.htm 


More information about the Logback-user mailing list