package com.glassworks.logback_classic_demo; import java.io.InputStream; import org.junit.Test; 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.joran.spi.JoranException; import ch.qos.logback.core.util.StatusPrinter; public class SmtpTest { private static final Logger log = LoggerFactory.getLogger(SmtpTest.class); private static final String INPUT_FILE = "logback-smtp.xml"; private final static boolean loop = false; public SmtpTest() throws JoranException { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(context); context.reset(); InputStream inputStream = SmtpTest.class.getClassLoader().getResourceAsStream(INPUT_FILE); if(inputStream == null) throw new IllegalStateException("File not found: " + INPUT_FILE); configurator.doConfigure(inputStream); StatusPrinter.printInCaseOfErrorsOrWarnings(context); } /** 01:03:21,649 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@70453807 - Registering current configuration as safe fallback point 01:03:21,653 |-INFO in ch.qos.logback.classic.net.SMTPAppender[EMAIL] - SMTPAppender [EMAIL] is tracking [1] buffers 01:03 [DEFAULT] INFO c.g.l.SmtpTest - hello world 01:03 [DEFAULT] WARN c.g.l.SmtpTest - hello world 01:03 [DEFAULT] ERROR c.g.l.SmtpTest - hello world 01:03 [DEFAULT] INFO c.g.l.SmtpTest - done */ @Test public void smtpTest() { log.info("hello world"); log.warn("hello world"); log.error("hello world"); log.info("done"); } /** * Output 01:02:53,354 |-INFO in ch.qos.logback.classic.net.SMTPAppender[EMAIL] - SMTPAppender [EMAIL] is tracking [1] buffers 01:02 [DEFAULT] INFO c.g.l.SmtpTest - hello world 01:02 [DEFAULT] WARN c.g.l.SmtpTest - hello world 01:02 [DEFAULT] ERROR c.g.l.SmtpTest - hello world 01:02:55,008 |-INFO in ch.qos.logback.classic.net.SMTPAppender[EMAIL] - Sent out SMTP message "c.g.l.SmtpTest - hello world" to [alexander.glass@gmail.com] 01:02 [DEFAULT] INFO c.g.l.SmtpTest - done */ @Test public void smtpWithPauseTest() throws InterruptedException { log.info("hello world"); log.warn("hello world"); log.error("hello world"); Thread.sleep(3000); log.info("done"); } }