I've configured an SMTPAppender in my JBoss server.
<appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Threshold" value="ERROR"/>
<param name="To" value="steve.bucciacchio#muhc.mcgill.ca"/>
<param name="From" value="jboss.wnetvmap47#muhc.mcgill.ca"/>
<param name="Subject" value="PQDSN Sever Error on WNETVMAP47"/>
<param name="SMTPHost" value="smtpout.muhc.mcgill.ca"/>
<param name="BufferSize" value="512"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>
</layout>
</appender>
It works, I get emails every time an error is logged in JBoss's server.log file. The problem is that I get an email for every single error that is logged, even if the error was a JBoss error, has nothing to do with my application. Is there a way to select or filter what errors are emailed? Also can I email only errors that are logged to my application's log file?
Related
i have configured log4j appender to create log file every hour. The logged file should not be created if there is nothing to log.
Everything works correctly, but when I start jBoss the log file is created even if there is nothing to log.
Appender settings.
<appender name="ordersLogFile" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="Append" value="true"/>
<param name="Threshold" value="INFO"/>
<rollingPolicy name="ordersLogFile" class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="${jboss.server.log.dir}/Portal_%d{yyyy_MM_dd-HH}-00-00.log" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="'%d{yyyyMMddHHmmss,SSS}';%m%n"/>
</layout>
</appender>
Is it possible to turn off creation of log file during jboss start?
I have overridden log4j smtp appender for some purpose. The problem is if i keep <param name="Threshold" value="INFO"/> Then it would send mails properly. But if i keep <param name="Threshold" value="ERROR"/> Then no mails are sent. What is the problem? Please find my code.
public class MyAppender extends SMTPAppender{
#Override
protected void sendBuffer() {
//Some code to format email body
}
}
log4j.xml
<!-- Appenders -->
<appender name="mail" class="com.service.MyAppender">
<param name="BufferSize" value="2"/>
<param name="BufferedIO" value="true"/>
<param name="Threshold" value="ERROR"/>
<param name="SMTPHost" value="myhost"/>
<param name="From" value="abc#co.com"/>
<param name="To" value="def#ld.com"/>
<param name="Subject" value="Testing Testing"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{2}:%L - %m%n"/>
</layout>
</appender>
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="500"/>
<appender-ref ref="mail"/>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="ASYNC" />
</root>
I looked into SMTPAppender source code and i believe below lines are the problem.
if(evaluator.isTriggeringEvent(event)) {
sendBuffer();
}
According to log4j documentation, all log events with value lower than Treshold will be filtered out (i.e. not sent).
It's possible that changing Treshold to more restrictive filters out all messages that are logged in your case.
Make sure that you actually have events with log level ERROR (or higher - FATAL).
On my SpringMVC project I enabled email logging starting from INFO massage (default behaviour is starting from ERROR message).
This is my appender configuration in log4j.xml:
<appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
<param name="SMTPDebug" value="true"/>
<param name="SMTPProtocol" value="smtps"/>
<param name="SMTPUsername" value="*"/>
<param name="SMTPPassword" value="*"/>
<param name="SMTPHost" value="*"/>
<param name="SMTPPort" value="*"/>
<param name="Subject" value="*"/>
<param name="To" value="*"/>
<param name="From" value="*"/>
<param name="BufferSize" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MM/dd/yyyy HH:mm:ss} [%M] %-5p %C - %m%n" />
</layout>
<triggeringPolicy class="org.apache.log4j.rolling.FilterBasedTriggeringPolicy">
<filter class="org.apache.log4j.filter.LevelRangeFilter">
<param name="levelMin" value="INFO" />
</filter>
</triggeringPolicy>
</appender>
It works on runtime but I get the same xml parser error and warning launching the project:
log4j:WARN The content of element type "appender" must match "(errorHandler?,param,rollingPolicy?,triggeringPolicy?,connectionSource?,layout?,filter*,appender-ref*)".*
What's wrong?
I'm using log4j-1.2.16.
I believe the reason to be that the order of the children in the XML <appender> element must follow the order declared in the DTD that the warning message shows. Quoting W3Schools about DTD elements:
When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document.
Put your <layout> element after the <triggeringPolicy> element and the warning will likely disappear.
I'm using Apache Commons logging with a Java EE based application.
Is it possible that whenever there is an exception being logged, an alert is send via an email?
We're doing that whenever an error is logged.
Here's the relevant part of our log4j configuration:
<appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Threshold" value="ERROR"/>
<param name="To" value="email#our.domain"/>
<param name="From" value="server#our.domain"/>
<param name="Subject" value="Error"/>
<param name="SMTPHost" value="our.host"/>
<param name="BufferSize" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE},%c] %m%n"/>
</layout>
</appender>
The logging itself is done using apache commons logging as a wrapper around log4j.
Note that you still might miss some exceptions if they are logged as warnings, but that should be intentional in that case and you'd normally not want to get an email every time an "expected" exception occurs.
I've configured an SMTPAppender into my Java app.
<appender name="AdministratorEmail" class="org.apache.log4j.net.SMTPAppender">
<param name="Threshold" value="info" />
<param name="BufferSize" value="512" />
<param name="SMTPHost" value="smtp.sss.intranet" />
<param name="From" value="adminEbookMaker#sss.intranet" />
<param name="To" value="user#sss.it" />
<param name="Subject" value="errors" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}]%n%n%-5p%n%n%c%n%n%m%n%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="info" />
<param name="LevelMax" value="fatal" />
</filter>
</appender>
I receive only the ERROR log type and not the INFO type! I changed the LevelMin but nothing, I inserted the Thresold, but nothing!
Does someone have any kind of suggestion?
The SMTPAppender by design only logs ERROR and above messages. This level cannot be affected by properties. The documentation for the appended states:
By default, an email message will be sent when an ERROR or higher severity message is appended. The triggering criteria can be modified by setting the evaluatorClass property with the name of a class implementing TriggeringEventEvaluator, setting the evaluator property with an instance of TriggeringEventEvaluator or nesting a triggeringPolicy element where the specified class implements TriggeringEventEvaluator
See: Class SMTPAppender
The fact that you are seeing INFO messages only after the first ERROR is due to the bufferSize property which shows the 'n' most recent log lines before the error to give context to the error.
Further research on this shows that there is an implementation of the required interface for TriggerEventEvaluator in the 'extras companion'
This can be downloaded from: Apache Download Mirrors
If you include this in your project you can then add the following to your SMTPAppender definition in log4j.xml (note the properties format is not supported!)
<appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
...
<triggeringPolicy class="org.apache.log4j.rolling.FilterBasedTriggeringPolicy">
<filter class="org.apache.log4j.filter.LevelRangeFilter">
<param name="levelMin" value="INFO" />
</filter>
</triggeringPolicy>
...
</appender>