[logback-user] How should I unit test my programmatic setup of appenders?

Noremac cam at byu.edu
Mon Jul 30 17:39:59 CEST 2012


ceki <ceki at ...> writes:

> 
> On 28.07.2012 00:37, Noremac wrote:
> > Thank you for the reply. The problem I've been having is that I'm not
> > finding accessible some of the specific pieces of the configuration. How
> > can I test that the markerEvaluator is set up correctly? There's no method
> > to get the evaluator, only to set the evaluator.
> 
> Could you post your code and config file so we can have a look at it? 
> You can change logger names etc to protect the innocent.


So the config file is basic, as I will be programmatically adding appenders:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <!-- Conversion rules; a custom pattern token to use in the pattern layout 
class -->
  <conversionRule conversionWord="threadHash" 
converterClass="stuff.util.logging.ThreadHashConverter" />
  
  <!-- Write logs out to the console -->
  <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
  	<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>[\(%-6threadHash\) app=iv  %d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level 
%-25logger{25} - %msg%n</pattern>
    </encoder>
  </appender>

    
  <!-- Specify what loggers are to be used -->
  <root level="INFO">
    <appender-ref ref="stdout"/>
  </root>
</configuration>

The appender is added like so:
	public void createAdminNotifyAppender(LoggerContext lc, 
AppConfigPropertiesReader propReader)
	{
		logger.info("Setting up logback admin_notify logging");
		
	    //Set up the admin notify appender programatically (needs to use the 
properties from the propReader above)
	    OnMarkerEvaluator markerEvaluator = new OnMarkerEvaluator();
	    markerEvaluator.setContext(lc);
	    markerEvaluator.addMarker("ADMIN_NOTIFY");
	    markerEvaluator.setName("ADMIN_NOTIFY");
	    markerEvaluator.start();

	    PatternLayout patternLayout = new PatternLayout();
	    patternLayout.setContext(lc);
	    patternLayout.setPattern("[\\(%-6threadHash\\) app=iv  %d{yyyy-MM-dd 
HH:mm:ss.SSS}] %-5level %-25logger{25} - %msg%n");
	    patternLayout.start();

	    String adminEmails = propReader.getProperty("adminEmail");

	    SMTPAppender adminNotifyAppender = new SMTPAppender();
	    adminNotifyAppender.setContext(lc);
	    adminNotifyAppender.setEvaluator(markerEvaluator);
	    
adminNotifyAppender.setSMTPHost(propReader.getProperty("mail.smtp.host"));
	    if (adminEmails == null)
	    {
	    	logger.error("No Admin Emails are set! No one will be notified 
for system alerts");
	    }
	    else
	    {
	    	for (String adminEmail : adminEmails.split(",")) { 
adminNotifyAppender.addTo(adminEmail); };
	    }
	    
adminNotifyAppender.setFrom(propReader.getProperty("sysEmailAddressNoReply"));
	    adminNotifyAppender.setSubject("System Alert! [" + 
propReader.getProperty("environment") + "]");
	    adminNotifyAppender.setLayout(patternLayout);
	    adminNotifyAppender.setName("ADMIN_NOTIFY");
	    adminNotifyAppender.start();

	    Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
	    rootLogger.addAppender(adminNotifyAppender);
	}

and my test case is as follows:
	private void testSimpleSMTPAppenderSetup(LoggingMarker marker, String 
email, Logger rootLogger, AppConfigPropertiesReader propReader)
	{
		SMTPAppender smtpAppender = 
(SMTPAppender)rootLogger.getAppender(LoggingMarker.ADMIN_NOTIFY.getName());
		
		//check that the necessary fields were set
		assertEquals("smtpHost set correctly", 
smtpAppender.getSMTPHost(), propReader.getProperty("mail.smtp.host"));
		assertEquals("from address set correctly", 
smtpAppender.getFrom(), propReader.getProperty("sysEmailAddressNoReply"));
		assertTrue("environment set correctly", 
smtpAppender.getSubject().contains(propReader.getProperty("environment")));
		
		List<String> appenderEmailsWithSuffix = 
smtpAppender.getToAsListOfString();
		List<String> appenderEmails = new ArrayList<String>();
		String[] propEmails = 
propReader.getProperty("adminEmail").split(",");
		
		for (String emailAddress : appenderEmailsWithSuffix)
		{
			appenderEmails.add(emailAddress.replaceFirst("%nopex$", 
""));
		}
		assertTrue("to-emails are set correctly", 
appenderEmails.containsAll(Arrays.asList(propEmails)));
		
		//TODO find a way to test that the logs are getting filtered by 
the markerEvaluator (or that it is there set on the logger)
	}



More information about the Logback-user mailing list