We use Log4j (and Commons Logging) to log our error messages. Now we want to set up an additional log appender that outputs fatal errors to syslog, but without the exception
Edit after reading some more of the source:
You still need to subclass PatternLayout, but the method you want to override is ignoresThrowable(): it should return false, which will prevent the appender from writing the Throwable (it assumes that the layout has done so already).
No way to specify this in the configuration: PatternLayout has a hardcoded "return true".
Here's the actual code I use:
import org.apache.log4j.PatternLayout;
public class NoStackTracePatterLayout extends PatternLayout {
@Override
public boolean ignoresThrowable(){
return false;
}
}
In 1.2.16 you can use EnhancedPatternLayout
You may need to write a custom layout to do it (which isn't that bad to do; you could subclass PatternLayout).
If you use log4j > 1.2.16, you can use the EnhancedPatternLayout layout.
Example (with a log4j.properties file), define it as the layout of your appender, and then add %throwable{0}
in the conversion pattern:
log4j.appender.XXX.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.XXX.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n%throwable{0}
The "nopex" or "nopexception" conversion word in logback-classic (log4j's successor) disables printing stack traces. The "nopex" conversion word is documented along with the rest of conversion words. You need to scroll down a little.
If you need further information on this topic, please contact the logback-user mailing list.