Send email only for a specific exception (ConfigException)Java

Java-Forum
Anonymous
 Send email only for a specific exception (ConfigException)

Post by Anonymous »

Ich möchte, dass log4j2 eine E -Mail nur sendet, wenn eine Konfiguration protokolliert ist. Alle anderen Ausnahmen sollten vom E -Mail -Appender ignoriert werden.

Code: Select all

log4j2.xml

Code: Select all








%d{yyyy-MM-dd HH:mm:ss} %-5level %logger - %msg%n%throwable

















< /code>
Java-Code: < /p>
public class ConfigException extends Exception {
public ConfigException(String message) {
super(message);
}
public ConfigException(String message, Throwable cause) {
super(message, cause);
}
}
< /code>
private final org.apache.logging.log4j.Logger log4jLogger =
org.apache.logging.log4j.LogManager.getLogger(getClass());

public void severe(String message, Throwable throwable) {
log4jLogger.error(message, throwable);
}
< /code>
try {
throw new ConfigException("Config failed");
} catch (ConfigException cEx) {
severe("Hihi; " + cEx.getMessage(), cEx);
}

try {
throw new RuntimeException("Some other error");
} catch (RuntimeException ex) {
severe("Oops; " + ex.getMessage(), ex);
}
Was passiert jetzt:

Die E -Mail wird für beide Ausnahmen gesendet. Eine E -Mail nur dann, wenn die protokollierte Ausnahme eine Instanz von ConfigException ?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post