How to stop console logging for log4j in spring batch? - maven

This is my log4j.properties file.
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.integration=FATAL
log4j.category.org.springframework.batch=FATAL
log4j.category.org.springframework.jdbc=FATAL
log4j.category.org.springframework.transaction=FATAL
I have tried changing the logging levels to INFO, ERROR etc. but nothing changes. I have this file in webapp/resources folder and i have added
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/main/webapp/resources/log4j.properties</param-value>
</context-param>
to web.xml as well. But making changes to log4j.properties doesn't do anything. I can still see many DEBUG logs in console like this
17:06:06.405 [qtp2018468405-39] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
How can I control logging level?

Logging level can be controled by using log4j.rootLogger property in log4j.properties file.
In your properties file I see you have not set log4j.rootLogger to INFO. Hence by default, the root logger is assigned to Level.DEBUG.

Related

Spring Boot : LOGBack custom fields in every log statement

Thought of sharing the below work as I struggled to find this info:
Requirement is to put the business correlation id(AccCode and Mac) in every log statement. This can be done by using Mapped Diagnostic Context(MDC)..
No need to understand how LOGBack appender->Encoder->Layout work.
Spring boot log4j.properties has below pattern:
LOG_LEVEL_PATTERN=%5p
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - ${PID} ${LOG_LEVEL_PATTERN} [%t] --- %c{1}: %m%n
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN}
Override LOG_LEVEL_PATTERN from application.properties with custom fields:
logging.pattern.level=AccCode:%X{AccCode} Mac:%X{Mac} %5p
Please note, now we have two extra fields defined into the layout. AccCode and Mac. Value for these new fields need to be provided using the MDC:
MDC.put("AccCode", "46014" );
MDC.put("Mac", "2025b9bcf");
Then all the log statements would have AccCode and Mac as below:
2016-09-12 09:11:37.574 **AccCode:46014** **Mac:2025b9bcf** DEBUG 9724 --- [main] c.c.p.sph.http.client.SphApAdminClient : Loging out from Sph

Log4j configured but no sql got printed out

I have latest spring+mybatis
Spring 4.2.3 + MyBatis 3.4+ MyBatis-Spring 1.3
I want to sql got printed out,so configured log4j like this:
# Console output...
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.Threshold = ALL
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %5p [%t] - %m%n
#log4j.logger.com.idpweb = TRACE
log4j.rootLogger = ERROR,stdout
log4j.logger.org.springframework = DEBUG, stdout
# SqlMap logging configuration...
log4j.logger.com.company.dept.mapper.map.SeniorMapper= TRACE
log4j.logger.com.mybatis = TRACE
log4j.logger.java.sql = TRACE
log4j.logger.java.sql.Connection = TRACE
log4j.logger.java.sql.PreparedStatement = TRACE
log4j.logger.java.sql.Statement = TRACE
log4j.logger.java.sql.ResultSet = TRACE
and project struct like this
Project
src
war
WEB-INF
applicationContext.xml
logj.properties
But nothing got printed out when app running,so what is the problem?
Another problem is that after appending "fgsdfgsd" to the end of log4j.properties,then restart web app,no exception got thrown out,so I doubt that spring doesn't take
project/war/WEB-INF/log4j.properties
as it's logging configuration.So I moved log4j.properties to
project/log4j.properties,
nothing changed.
1、if you use maven project,then put the log4j.properties in the resources folder like this:
project/src/main/resources/log4j.properties
this is the default location where log4j try to locate its property file.
2、you can also specify you log4j config file in web.xml like this:
(1)add Log4jConfigListener
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
(2)add context param name 'log4jConfigLocation' to tell Log4jConfigListener where to locate the log4j.proterties
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/conf/log4j.properties</param-value>
</context-param>

Hadoop log4j not working as No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory)

I am working on developing mapreduce using eclipse , and trying to test it using hadoop 2.6.0 windows standalone mode.
But getting the below error for log4j,
How to fix the below appender problem,
No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory)
same problem as hadoop log4j not working but no answer yet
Thanks ,
1- Create the file log4j.properties and put it in the location of
src/main/resources
the content of the log4j.properties
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
it happens because of the hadoop logging framework
Create a log4j.properties file with something like the following:
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

log4j configurations for non web application

I've create a simple maven application and using log4j logger. My application is not a WEB application. I've tried to copy log4j.properties everywhere in java folder and everywhere across a project but still getting following error. Could you suggest how can I fix it?
log4j:WARN No appenders could be found for logger (org.springframework.web.client.RestTemplate).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j.properties
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
The log4j.properties file should be in a dirctory or JAR file that is on the classpath at runtime.
If you have a Maven project with the standard Maven project directory layout, then put the file in the directory src\main\resources - that way, Maven should put it in the right place for you when you compile your project.

Why does this only log for hibernate?

Within my log4j.properties, log4j.rootLogger=DEBUG, stdout only shows debug data for hibernate. Can anyone explain this? I'm trying to debug other packages but none of them will work.
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c:%L - %m%n
I realized that you do NOT have log4j in your pom, you need these 3 dependencies to log with log4j and slf4j:
slf4j-api-x.x.x.jar
slf4j-log4j12-x.x.x.jar
log4j-x.x.x.jar
log4j and slf4j-log4j12 should have the same version.
Please also note the commet from #Stefan Lindenberg

Resources