Created
July 9, 2012 13:11
-
-
Save ripper234/3076474 to your computer and use it in GitHub Desktop.
Play Framework SMTP appender with exclusions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.apache.commons.lang.StringUtils; | |
| import java.util.regex.Pattern; | |
| public class RegexUtils { | |
| private RegexUtils(){} | |
| public static Pattern matchAny(String[] patterns) { | |
| return matchAny(patterns, 0); | |
| } | |
| public static Pattern matchAny(String[] patterns, int flags) { | |
| StringBuilder uberString = new StringBuilder(); | |
| String uber = StringUtils.join(patterns, ")|(?:"); | |
| if (patterns.length > 1) { | |
| uber = "(?:" + uber + ")"; | |
| } | |
| return Pattern.compile(uber, flags); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.Test; | |
| import java.util.regex.Pattern; | |
| public class RegexUtilsTests extends TestClass { | |
| @Test | |
| public void zero() { | |
| Pattern pattern = RegexUtils.matchAny(new String[0]); | |
| // empty regex, should match anything | |
| assertTrue(pattern.matcher("").find()); | |
| assertTrue(pattern.matcher("a").find()); | |
| } | |
| @Test | |
| public void one() { | |
| Pattern pattern = RegexUtils.matchAny(new String[]{"one"}); | |
| assertTrue(pattern.matcher("zzz one zzz").find()); | |
| assertFalse(pattern.matcher("zzz two zzz").find()); | |
| } | |
| @Test | |
| public void two() { | |
| Pattern pattern = RegexUtils.matchAny(new String[]{"one", "two"}); | |
| assertTrue(pattern.matcher("zzz one zzz").find()); | |
| assertTrue(pattern.matcher("zzz two zzz").find()); | |
| assertFalse(pattern.matcher("zzz three zzz").find()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import notifiers.Mails; | |
| import org.apache.log4j.AppenderSkeleton; | |
| import org.apache.log4j.spi.LoggingEvent; | |
| import java.util.regex.Pattern; | |
| /** | |
| * sends email in async manner. | |
| */ | |
| public class SMTPAppender extends AppenderSkeleton { | |
| private static String[] EMAIL_PATTERN_STRS = { | |
| "You wanted an exception", // Dev.throwException | |
| "java.util.concurrent.ExecutionException: java.net.ConnectException: null to", // Play will report ERROR logs on failed HTTP gets. See https://play.lighthouseapp.com/projects/57987-play-framework/tickets/1555-wsurlfetchget-write-an-error-log-on-failure | |
| "Action not found.*No method public static", | |
| "Action not found.*Error raised is.*not found", // example: manager.status action not found Action not found Action manager.status could not be found. Error raised is Controller controllers.manager not found | |
| }; | |
| private static Pattern DO_NOT_EMAIL_PATTERN = RegexUtils.matchAny(EMAIL_PATTERN_STRS, Pattern.DOTALL | Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); | |
| @Override | |
| protected void append(final LoggingEvent loggingEvent) { | |
| if (doNotEmail(loggingEvent)) { | |
| return; | |
| } | |
| Mails.notifyOnError(loggingEvent); | |
| } | |
| private boolean doNotEmail(LoggingEvent loggingEvent) { | |
| return loggingEvent.getMessage() != null && | |
| DO_NOT_EMAIL_PATTERN.matcher(loggingEvent.getRenderedMessage()).find(); | |
| } | |
| @Override | |
| public void close() { | |
| } | |
| @Override | |
| public boolean requiresLayout() { | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment