How to use syslog with LOG4J 1.x? - java

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

Related

Write specific logs to a log file spring-boot/slf4j

I am able to write console logs to rotating log files using this method here:
https://www.codejava.net/frameworks/spring-boot/logback-rolling-files-example
But my requirement is to write only specific logs to the log files instead of whole console log. For example, i need to write only the json messages(i can print them on console using log.info(message) which is supported by slf4j) being received by Kafka listener to the log files and nothing more. How can i achieve this in my spring-boot app? Is this not possible with slf4j logback?
Specify the appender-ref in your logger element.
So something like this in your logger XML configuration file:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="bdfile" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false"/>
<param name="maxFileSize" value="1GB"/>
<param name="maxBackupIndex" value="2"/>
<param name="file" value="/tmp/bd.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
Then in the same config file, add a appender reference to the log file that you are interested in
<logger name="com.example.ClassWithJsonMessage" additivity="false">
<level value="debug"/>
<appender-ref ref="bdfile"/>
</logger>

How to configure SNMP Appender in Log4j2.xml?

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.

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

Spring jdbcTemplate debug messages not getting printed using log4j

I have the below log4j.xml in placed in the src directory of a web application. Even after follwing many post examples, i cannot get the below configuration print jdbcTemplate internals to log file.
Config:
<appender name="INTERNALSLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/springDB.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10mb"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} - %m%n"/>
</layout>
</appender>
<logger name="org.springframework.jdbc.core">
<level value="DEBUG" />
<appender-ref ref="INTERNALSLOG"/>
</logger>
Whats wrong with the configuration. Just want to get the sql's from jdbcTemplate printed.

log4j writing to rolled over file

We have set log4j rollover as 500mb upon 500 mb it is creating a file new file log.1 but still it is sometimes writing to log.1 and to log and sometimes only to log and some times to log.1.
I am using apache tomcat.
It is so inconsistent.
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p: %c - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="500MB" />
<param name="maxBackupIndex" value="5" />
<param name="Append" value="true"/>
<param name="File" value="c:/logs/web.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n" />
</layout>
</appender>
Could you please let me know any reason for this?
Regards,
Adam

Categories

Resources