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
%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 ?
Ich möchte, dass log4j2 eine E -Mail [b] nur [/b] sendet, wenn eine Konfiguration protokolliert ist. Alle anderen Ausnahmen sollten vom E -Mail -Appender ignoriert werden.[code]log4j2.xml[/code]
[code]
%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);
}
[/code]
Was passiert jetzt:
Die E -Mail wird für [b] beide [/b] Ausnahmen gesendet. Eine E -Mail nur dann, wenn die protokollierte Ausnahme eine Instanz von ConfigException ?