Send email only for a specific exception (ConfigException)

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Send email only for a specific exception (ConfigException)

by Anonymous » 19 Aug 2025, 09:53

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 ?

Top