[logback-dev] Using Thread to monitored TimeBasedRollingPolicy...!

boss134 balasundaram.06 at gmail.com
Fri Apr 3 10:46:48 CEST 2009


Dear sir,

          below part is my complete configuration... includes xml
configuration and java code part
          i logging every level in separate log file.....

<configuration>

  	<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  		<ImmediateFlush>true</ImmediateFlush> 
    	<layout class="ch.qos.logback.classic.PatternLayout">
      		<Pattern>%-4relative [%thread] %-5level %logger{35} - %msg
%n</Pattern> 
    	</layout>
  	</appender>

	<appender name="FILEDEBUG"
class="ch.qos.logback.core.rolling.RollingFileAppender"> 
		<file>Debug/myDebug.log</file>
		
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
			<FileNamePattern>myDebug.%d.zip</FileNamePattern>  
			<!-- keep 60 days worth of history -->  
			<MaxHistory>60</MaxHistory>  
		</rollingPolicy>
		 
	 	<filter class="ch.qos.logback.classic.filter.LevelFilter">
      		<level>DEBUG</level>
      		<onMatch>ACCEPT</onMatch>
      		<onMismatch>DENY</onMismatch>
    	</filter> 
       	
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>%date %level [%thread] %logger{10} [%file:%line]
%msg%n</Pattern>
		</layout>
	</appender>
	
	<appender name="FILEINFO"
class="ch.qos.logback.core.rolling.RollingFileAppender"> 
		<file>Information/myInfo.log</file>
		
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
			<FileNamePattern>myInfo.%d.log</FileNamePattern>  
			<!-- keep 60 days worth of history -->  
			<MaxHistory>60</MaxHistory>  
		</rollingPolicy>
		 
	 	<filter class="ch.qos.logback.classic.filter.LevelFilter">
      		<level>INFO</level>
      		<onMatch>ACCEPT</onMatch>
      		<onMismatch>DENY</onMismatch>
    	</filter> 
       	
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>%date %level [%thread] %logger{10} [%file:%line]
%msg%n</Pattern>
		</layout>
	</appender>
	
	<appender name="FILEWARN"
class="ch.qos.logback.core.rolling.RollingFileAppender"> 
		<file>Warning/myWarn.log</file>
		
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
			<FileNamePattern>myWarn.%d.log</FileNamePattern>  
			<!-- keep 60 days worth of history -->  
			<MaxHistory>60</MaxHistory>  
		</rollingPolicy>
		 
	 	<filter class="ch.qos.logback.classic.filter.LevelFilter">
      		<level>WARN</level>
      		<onMatch>ACCEPT</onMatch>
      		<onMismatch>DENY</onMismatch>
    	</filter> 
       	
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>%date %level [%thread] %logger{10} [%file:%line]
%msg%n</Pattern>
		</layout>
	</appender>
	
	<appender name="FILERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender"> 
		<file>Error/myError.log</file>
		
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
			<FileNamePattern>myError.%d.zip</FileNamePattern>  
			<!-- keep 60 days worth of history -->  
			<MaxHistory>60</MaxHistory>  
		</rollingPolicy>
		 
	 	<filter class="ch.qos.logback.classic.filter.LevelFilter">
      		<level>ERROR</level>
      		<onMatch>ACCEPT</onMatch>
      		<onMismatch>DENY</onMismatch>
    	</filter> 
       	
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>%date %level [%thread] %logger{10} [%file:%line]
%msg%n</Pattern>
		</layout>
	</appender>
 
  	<root level="debug">
    	<appender-ref ref="CONSOLE" />
   		<appender-ref ref="FILEDEBUG" /> 
   		<appender-ref ref="FILEINFO" />
   		<appender-ref ref="FILEWARN" />
   		<appender-ref ref="FILERROR" />  
  	</root>
  	
</configuration>


==================================================================================
            COMPLETE JAVA CODE 
==================================================================================

import java.util.Random;
import java.util.Scanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.util.StatusPrinter;

public class LogBackTester implements Runnable
{
	/** Monitor Thread Status */
	private volatile boolean threadStatus = false;
	
	/** Logger */
	final static Logger logger = LoggerFactory.getLogger(LogBackTester.class);
	
	public LogBackTester()
	{
		try {
			// LogBack Configuration
			LoggerContext loggerContext = (LoggerContext)
LoggerFactory.getILoggerFactory();
			JoranConfigurator config = new JoranConfigurator();
			config.setContext(loggerContext);
			loggerContext.reset();
			config.doConfigure(".\\src\\logback.xml");
			StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run()
	{
		int minValue = 500;
		int maxValue = 1000;
		Random random = new Random();

		while(!threadStatus)
		{
			int randomValue = random.nextInt(maxValue - minValue + 1) + minValue;
			// Write All logs here
			System.err.println("Random Value : " + randomValue);
			
			logger.trace("Trace {} " + randomValue);
			logger.debug("Debug {} " + randomValue);
			logger.info("Information {} " + randomValue);
			logger.warn("Warning {} " + randomValue);
			logger.error("Error {} " + randomValue);
			
			try	{
				Thread.sleep(randomValue);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void logBackThreadKiller()
	{
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		while(scan.hasNext()) {
			str = scan.next();
			if(true == str.equalsIgnoreCase("Y")) {
				threadStatus = true;
				break;
			}
		}
		scan.close();
	}

	public static void main(String[] args)
	{
		LogBackTester tester = new LogBackTester();
		Thread thread = new Thread(tester, "LogBackTester");
		thread.start();

		// Thread Killer
		tester.logBackThreadKiller();
	}
}


Thank you,
boss134
-- 
View this message in context: http://www.nabble.com/Using-Thread-to-monitored-TimeBasedRollingPolicy...%21-tp22864185p22864390.html
Sent from the Logback Dev mailing list archive at Nabble.com.



More information about the logback-dev mailing list