Log tomcat logs to rsyslog - java

I have a java web application that runs with tomcat7 and logs with log4j. I want to send my logs to rsyslog.
I have use the appeneder in my log4j.xml file.
<appender name="syslog" class="org.apache.log4j.net.SyslogAppender">
<param name="Facility" value="USER"/>
<param name="SyslogHost" value="localhost"/>
<param name="Threshold" value="WARN"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} MYAPP: %-5p %m%n"/>
</layout>
</appender>
But I still don't see the logs that I've implemented in my java application in rsyslog. I can see them in catalina.out but not on rsyslog.
Do I have to define new rules for rsyslog configuration?
Thanks in advance.
Updated log4j.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="System.out" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.out" />
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="SYSLOG" class="org.apache.log4j.net.SyslogAppender">
<param name="SyslogHost" value="localhost"/>
<param name="Facility" value="LOCAL1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<!-- for package specific -->
<!-- <category name ="my.package" /> -->
<logger name ="Logger">
<level value ="debug" />
</logger>
<!-- root logger -->
<root>
<level value="info"/>
<appender-ref ref="SYSLOG"/>
</root>

Related

Spring Boot Admin: Error while reading logfile: -

When I'm tapping on the log button corresponding to my application of spring boot admin dashboard I'm getting Error while reading logfile: - error. Does somebody faced this already?
I'm using log4j with such configuration:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE}] [%-5p] [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="10KB" />
<param name="maxBackupIndex" value="5" />
<param name="file" value="service-rest.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE}] [%-5p] [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="file" />
</root>
</log4j:configuration>
My application.yml file has such logging section:
logging:
file: "service-rest.log"
level:
org:
springframework:
boot:
autoconfigure:
logging: DEBUG
Also I could get that logfile from app directly http://url/actuator/logfile
The problem was the slightly wrong format of logs that has been pushed to service-rest.log file. The right one appender for file is
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="5" />
<param name="file" value="tvpp-rest.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
Without that brackets around time and log level.

Not getting expected output from log4j when the logs are printed in file

Problem:
We got 2 logs printed in the file like this:
Analysis
1)These 2 logs are printed one after the other in the code and here too but the time difference is 7 minutes which is huge.
2)This has been observed when there is only one War file as well as multiple wars in the Tomcat.
Below is the log4j configuration file to analyse the issue clearly:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
</layout>
</appender>
<appender name="callback" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/subs_engine.log" />
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern"
value="${catalina.base}/logs/%d{yyyy-MM-dd_HH}_subs_engine.gz" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="callback" />
</root>
</log4j:configuration>
Can anyone guide me how to solve or debug this problem:
Is this the problem of log4j?
We are using log4j-1.2.17.jar
#Edit:
Logger.sysLog(LogValues.APP_DEBUG, Utility.class.getName(),"Inside loadHttpReqToBean.");
Logger class Calling syslog method

Stop Hibernate SQL logs coming on catalina.out

I was making a log4j.xml for one of my projects in netbeans .
Here is its contents :
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/MyLogs.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m #]%n" />
</layout>
</appender>
<appender name="sqlLogs" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/MySqlLogs.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Threshold" value="TRACE" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value ="INFO" />
<appender-ref ref="FILE" />
</root>
<logger name="org.hibernate.SQL" additivity="false" >
<level value="DEBUG" />
<appender-ref ref="sqlLogs" />
</logger>
</log4j:configuration>
Now i want sql logs to go only to MySqlLogs.log but they are appearing on both MySqlLogs.log and catalina.out .
The format of logs in catalina.out is
Hibernate : select ....
Hibernate : insert into ....
Can anyone help me out on this please .
You can stop it, on your env shared properties.
hibernate.show_sql=true
you have to add the following line in your hibernate_cfg.xml file
show_sql to false
and in your file loging.properties :
log4j.logger.org.hibernate=debug
log4j.logger.org.hibernate.type=trace
log4j.logger.org.apache=debug

log different log levels to different files with log4j

I am using log4j for logging in my applicaion. I am trying to different level logs in different files but something going wrong. Any help regrading this issue will be highly appreciated.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<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="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="TCS_patch_9/log/retailer_debug.log" />
<param name="Threshold" value="DEBUG" />
<param name="MaxFileSize" value="2MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="TCS_patch_9/log/retailer_info.log" />
<param name="Threshold" value="INFO" />
<param name="MaxFileSize" value="2MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="TCS_patch_9/log/retailer_error.log" />
<param name="Threshold" value="ERROR" />
<param name="MaxFileSize" value="2MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="FATAL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="TCS_patch_9/log/retailer_repeat_delay.log" />
<param name="Threshold" value="FATAL" />
<param name="MaxFileSize" value="2MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
</layout>
</appender>
<root>
<priority value ="error" />
<appender-ref ref="console"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="FATAL"/>
</root>
</log4j:configuration>
The standard behaviour for log4j appenders is that they log all messages at their threshold level or higher, i.e. an appender with threshold INFO will log INFO, WARN, ERROR and FATAL messages but not DEBUG. If you want to log only INFO messages but not WARN and above then you need to use a LevelMatchFilter.
You will also need to set your root logger priority to DEBUG, otherwise it will only send ERROR and FATAL messages to its appenders and your DEBUG and INFO files will be empty.

log4j console warnings

I am getting two console warnings:
log4j:WARN No appenders could be found for logger (org.roussev.selenium4j.MainTestCase).
log4j:WARN Please initialize the log4j system properly.
Following is my log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appender" class="org.apache.log4j.FileAppender">
<param name="File" value="selenium4j.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
</layout>
</appender>
<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 [%t] %p - %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG"/>
<appender-ref ref="appender"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
I am not sure why am I getting this error. The xml seems to be fine to me.
Most common reason for this undescriptive message is ::
The log4j.xml is not properly added in your classpath.
See more here ::: https://stackoverflow.com/a/6616858/1085285

Categories