Spring jdbcTemplate debug messages not getting printed using log4j - java

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.

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 separate records in log file by blank line? Log4j

I try to make my log file more readable so I want to separate records in my log file by a blank line after every application run. I use log4j.
I understand that the easiest way is to append %m%n to a single record in code. But I'm interested in doing it via configuration file.
So the question is what I have to add to configuration file log4j.xml?
Here is the context of my log4j.xml configuration file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="maxFileSize" value="10MB"/>
<param name="maxBackupIndex" value="10"/>
<param name="file" value="logs/logs.log"/>
<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="DEBUG"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
You could use another Logger defined in your configuration that uses the same settings except its ConversionPattern which will contain only %n. Here is an example:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true">
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="maxFileSize" value="10MB"/>
<param name="maxBackupIndex" value="10"/>
<param name="file" value="logs/logs.log"/>
<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="file2" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="maxFileSize" value="10MB"/>
<param name="maxBackupIndex" value="10"/>
<param name="file" value="logs/logs.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n"/>
</layout>
</appender>
<logger name="com.company.MyClass">
<level value="DEBUG"/>
<appender-ref ref="file2"/>
</logger>
<root>
<level value="DEBUG"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
Then, when your app starts:
Logger logger = Logger.getLogger(com.company.MyClass.class);
logger.info(" ");
I'm not sure to understand what you want to achieve and why, but if you want an empty line between each log entry, you just have to add an extra %n at the end of your ConversionPattern:
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n%n"/>
(%n means "new line")

Log4j how to log by level and the log file should contains only its level's log [duplicate]

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.

how to stop quartz warn loggers

Console is getting filled with Quartz WARN loggers all the time and it's really annoying to developers who work in the project to find other logger messages in console.
[2013-09-14 11:18:35,142] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'system-id'
[2013-09-14 11:18:35,142] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'end-at'
[2013-09-14 11:18:35,143] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'cron-expression'
[2013-09-14 11:18:35,144] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'start-at'
[2013-09-14 11:18:35,144] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'scheduled-job-id'
[2013-09-14 11:18:35,144] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'size'
[2013-09-14 11:18:40,086] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'GNS'
[2013-09-14 11:18:40,087] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'limit'
[2013-09-14 11:18:40,087] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'tenantId'
I Google and find a way to do but it didn't give the solution.
<logger name="org.quartz">
<level value="info" />
</logger>
We are using log4j.xml instead of log4j.properties.
Here is the snapshot of log4j.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--
Read http://logging.apache.org/log4j/docs/api/org/apache/log4j/DailyRollingFileAppender.html
for more information on DaliyRollingFileAppender configuration options.
-->
<appender name="error" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/elg/logs/scandium-error.log"/>
<param name="Threshold" value="error"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p - [%t] [%x] %m%n"/>
</layout>
</appender>
<appender name="debug" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/elg/logs/scandium-debug.log"/>
<param name="Threshold" value="trace"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p [%t] - %c{1} [%x] - %m%n"/>
</layout>
</appender>
<appender name="info" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/elg/logs/scandium-info.log"/>
<param name="Threshold" value="info"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - [%t] [%x] %m%n"/>
</layout>
</appender>
<appender name="trace" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/elg/logs/scandium-trace.log"/>
<param name="Threshold" value="info"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - [%t] [%x] %m%n"/>
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="info"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1} - [%t] [%x] %m%n"/>
</layout>
</appender>
<category name="com.opensymphony.xwork2.ognl.OgnlValueStack">
<priority value="error"/>
<appender-ref ref="error"/>
<appender-ref ref="console"/>
</category>
<root>
<priority value="trace"/>
<appender-ref ref="info"/>
<appender-ref ref="trace"/>
<appender-ref ref="debug"/>
<appender-ref ref="error"/>
<appender-ref ref="console"/>
</root>
<logger name="org.quartz">
<level value="info" />
</logger>
</log4j:configuration>
Please help to sort out the issue. Thanks in advance.
WARN is a more severe log level than INFO, and so setting your log level to INFO will still get warnings. The best solution is to figure out why you're getting all those warnings; they're pointing to something Quartz thinks is a real problem (you apparently have a bunch of properties set in a configuration somewhere that the target job type doesn't understand). If you absolutely can't fix the real problem, then you can set the log level to ERROR.

Why is log4j logging to the console?

This is a standalone java application.
I am using the configuration file below and having two problems.
1) I'm getting logs to stdout and I don't know why.
2) I'm getting ALL log messages in my error log even though I have tried to direct only error and higher to the error log.
I am using the BasicConfigurator without specifying an explicit path to the log4j.xml file. The xml file is in the same jar as my classes. It is creating and writing to the appropriate logs in addition to these problems so the configuration is being applied.
3) In addition, I have had no luck having the log4j.xml file outside of the jar so I can change it at runtime. How do I do that?
<!--appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p [%F:%L] - %m%n"/>
</layout>
</appender-->
<!-- working dir is $CATALINA_TMPDIR. send logs to log dir. -->
<appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/log/company/application.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="9"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<appender name="ERRORLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/log/rocketvox/company/error.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="9"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<category name="com.company">
<priority value="ALL"/>
<appender-ref ref="ROLL"/>
</category>
<category name="com.mattx">
<priority value="ALL"/>
<appender-ref ref="ROLL"/>
</category>
<root>
<priority value="error"/>
<appender-ref ref="ERRORLOG"/>
</root>
Add -Dlog4j.debug to the application's JVM args to see exactly what log4j is doing and which configuration file it uses.
Your problem is using BasicConfigurer - for configuring using a file named log4j.xml, you don't need to use any explicit log4j Configurer in your application code, as the default log4j initialization logic will kick in to pick log4j.xml (or log4j.properties, if no xml is found) from the classpath.
See Default Initialization Procedure from the log4j manual.

Categories

Resources