How to configure SNMP Appender in Log4j2.xml? - java

We have configured the SNMP appender as below in log4j.xml
<appender name="TRAP_LOG_APPENDER" class="org.apache.log4j.ext.SNMPTrapAppender">
<param name="ImplementationClassName" value="org.apache.log4j.ext.JoeSNMPTrapSender"/>
<param name="EnterpriseOID" value="1.3.6.1.4.1.2854.1"/>
<param name="ApplicationTrapOID" value="1.3.6.1.4.1.24.12.10.22.64"/>
<param name="ManagementHost" value="127.0.0.1"/>
<param name="ManagementHostTrapListenPort" value="162"/>
<param name="LocalIPAddress" value="127.0.0.1"/>
<param name="LocalTrapSendPort" value="161"/>
<param name="GenericTrapType" value="6"/>
<param name="SpecificTrapType" value="12345678"/>
<param name="CommunityString" value="public"/>
<param name="ForwardStackTraceWithTrap" value="true"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d,%p,[%t],[%c],%m%n"/>
</layout>
</appender>
Since appender tag is no longer in use with the latest log4j2 version ,We need help to configure the same in log4j2.xml.Thanks!!!

Unfortunately I dont think there is an SNMP appender available for Log4j2. Anyway I personally use following open source appender.
You can find it on github.com/DushmanthaBandaranayake/log4j2-snmp-appender.

Related

How to solve Log4J No Such Property warning/error?

I have a Log4J xml config file with appenders in it.
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="File" value="/tomcat/website/webapps/app/logs/appInfo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<param nawebsite/webapps/app/logs/app.log" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
When I build my project, I get the following warnings/errors in the console which I am trying to get rid of:
log4j:WARN No such property [datePattern] in org.apache.log4j.RollingFileAppender.
log4j:WARN No such property [file] in org.apache.log4j.ConsoleAppender.
I did not make these log4j config xml files; they were already part of the project.
I've seen the DatePattern documentation online, and it seems to suggest exactly what is in the DatePattern value, so I have no idea why it's still showing me that warning. Same with file.
Did the person who made this before me just stick properties in these appenders that do not belong? I'm having a difficult time verifying this.
Every Appender has it's own configuration properties. file make sense for RollingFileAppender but doesn't make sense for ConsoleAppender.
If you switch one appender to another and do not update configuration properties, then you will get such warnings.
To fix them, you need to remove unused (or inapplicable) properties from your configuration:
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<param name="FileName" value="/tomcat/website/webapps/app/logs/app.log" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
Complete list of appenders and their parameters is available on the Apache Commons Log4j webpage:
https://logging.apache.org/log4j/2.x/manual/appenders.html

How to use syslog with LOG4J 1.x?

I want to config my LOG4J. I can't update the version to 2.x because it's a big application. The actual Documentation for the LOG4J version 1.x is offline.
Here is my actual config for the appender:
<appender name="syslog" class="org.apache.log4j.net.SyslogAppender">
<param name="SyslogHost" value="localhost:514"/>
<param name="Facility" value="LOCAL1"/>
<param name="FacilityPrinting" value="true"/>
<param name="Threshold" value="WARN"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MMM.yyyy HH:mm:ss} %-5p %m%n"/>
</layout>
</appender>
I tested it with : netstat -a -b > test.txt
But i dont't found something with the Port 514.
Can somebody help me(because he made experience with LOG4J 1.x), or has somebody a copy of the documentation from LOG4J 1.x ?
<appender name="Syslog" class="org.apache.log4j.net.SyslogAppender">
<param name="SyslogHost" value="IP:514"/>
<param name="Facility" value="USER"/>
<param name="FacilityPrinting" value="true"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.IvyLog4jLayout">
<param name="DateFormat" value="MM/dd/yyyy hh:mm:ss"/>
</layout>
</appender>
This way it worked for me :)

log4J empty file

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?

log4j smtp appender overriding gives unexpected behavior?

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).

Log4j parsing error adding triggeringPolicy on SMTPAppender

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.

Categories

Resources