Logger.getRootLogger().getAppender("CONSOLE") returning null in a class but it can log to console. getAllAppenders() also returning NullEnumeration in this class.
In another class(MyClass) Logging level:DEBUG | Additivity:true | Parent:root | getAllAppenders():NullEnumeration
but log.debug printing nothing to console.
log4j configuration for this class is
<logger name="com.xxx.service.impl.MyClass">
<level value="TRACE"/>
</logger>
tried to debug why is debug message not getting printed in many ways but in vain.
How can I check the log4j configuration for this class in depth and what could be the problem, please help me.
At first, u should set additivity parameter as false to be sure that each class is writing its logs to one log4j file:
log4j.additivity.${APPENDARNAME}=false
And make sure that only one log4j tech is loaded with your app(I prefer only log4j for basic staff)
Be sure that , no log4j.xml or log4j.properties files are under any of your classpath folders but the one that u want to be loaded. if that is a possiibility, force application to use yours. For spring:
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" >
<property name="staticMethod">
<value>org.springframework.util.Log4jConfigurer.initLogging</value>
</property>
<property name="arguments">
<list>
<value>file:${log4jFile}</value>
</list>
</property>
</bean>
Adding below listener in web.xml solved my problem.It configured my desired log4j.xml and log4j is logging fine.
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<!-- applies log4j configuration -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Related
I am migrating an application from log4j to log4j2.
Below code snippet is log4j related and there is a filter DenyAllFilter added.
<appender name="TEST_FILE" class="org.apache.log4j.RollingFileAppender">
...
...
<filter class="com.test.it.ModeFilter">
<param name="Mode" value="Test" />
<param name="Deny" value="false" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
Should DenyAllFilter be defined in log4j2 as well ? If so, then What is the equivalent of DenyAllFilter inlog4j2 so that it can be added in RollingFile under log4j2.xml as mentioned below.
<RollingFile="TEST_FILE" other sttributes ...>
...
...
<ModeFilter name="ModeFilter" other attributes ... />
??? what is the equivalent of DenyAllFilter???
Please help. Thanks.
According to the docs, DenyAllFilter can be used to switch from the default "accept all unless instructed otherwise" filtering behaviour to a "deny all unless instructed otherwise" behaviour.
There is no direct equivalent to this in Log4j 2. Since you will need to rewrite your custom ModeFilter as a Log4j 2 plugin anyway, you may want to change the accept/reject conditions for this filter instead.
I am using spring boot for my application and I am using default spring boot logging.
In my application.properties, I have added file path for logging.file,
logging.file= ${logger_path}
and my pom.xml contains
<logger_path>/tmp/app.log</logger_path>
When I start the application, it prints the logging messages to the file at /tmp/app.log, but the problem is it also prints the log messages on the console. I really don't understand why it is printing on console (though it is printing them to the specified file) when I have specified a log file.
Is there any configuration to prevent spring boot from printing the log messages to console?
Spring boot comes with build-in logback logger, which is configured to print to the console by default.
You need to overwrite the logback configuration (providing your own logback.xml on the classpath).
This is described here - - section 66.1
How to disable logback logging read here -
as you can see you have to provide the value OFF...something like:
<configuration>
<include resource="base.xml" />
.
.
<property name="root.level.console" value="OFF" />
</configuration>
Note: Keep in mind that the general idea here is that the logback configuration coming from spring-boot is minimal. The intention is that you provide your own logback configuration and completely overwrite the existing one - e.g. providing your own logback configuration with only log file-appender configured - this should be your general approach.
Include file-appender.xml and not console-appender with following configuration in logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
You also need to add logging.file to your application.properties
This is in compliance to what is mentioned in spring boot documentation - http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
I tried removing console configuration from logback.xml. But, It was still logging in console. So What I did is, I just removed the appender being added in the logging configuration by springboot. Thereby, we can stop springboot logging in console and separate log file. Add the below lines at the end of your application specific appenders are added. Your custom appender should not match any of these appender names. It worked for me.
// get instance of your log4j instance
Logger logger = LogManager.getRootLogger();
logger.removeAppender("CONSOLE"); // stops console logging
logger.removeAppender("LOGFILE"); // stops file logging
A similar discussion can be found here.
My logback.xml looks like that:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Just in case you still experience an issue: I have read many discussions about that topic and it was not working at all for me. Unfortunately I have made a really stupid mistake when I have created the file logback.xml for the very first time: A space was added at the beginning of the filename. Therefore the file was never used. So just have another look on the filename, add the file in "src/main/resources" and remove any "logging.config" entries from your property files.
Tested with latest 1.3.1 release, with newer releases base.xml has been renamed to defaults.xml
Answer here
I'm using Tomcat 8.0.3 and log4j 2.0-rc1 to deploy a GWT application (servlet 2.5).
I believe I fixed the usual log configuration errors, but still nothing is logged to the console or file.
Head of my web.xml:
<listener>
<listener-class>org.apache.logging.log4j.core.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.core.web.Log4jServletFilter</filter-class>
</filter>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///C:/Users/name/Desktop/log4j2.xml</param-value>
</context-param>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
This is the log4j2.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%p] %c{1}: %m%n"/>
</Console>
<File name="infoFile" fileName="${sys:catalina.home}/logs/info.log" >
<PatternLayout pattern="[%p] %c{1}: %m%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="infoFile"/>
</Root>
</Loggers>
</Configuration>
I added both the log4j2 api and core to my Maven dependencies and I can see them exploded in WEB-INF\classes\. I've also removed them from the skipJar list in catalina.properties.
When the war is deployed, no errors or warnings are thrown in the localhost or catalina log. This is the localhost log:
INFO ContextListener: contextInitialized()
INFO SessionListener: contextInitialized()
INFO Log4jServletContextListener ensuring that Log4j starts up properly.
INFO Log4jServletFilter initialized.
The strange thing is that the info.log file is created, but never written to.
I'm also using Hibernate whose logs do show up in the console, could this be the problem?
Finally, this is how I'm using the logger:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
private static final Logger LOG = LogManager.getLogger(MyClass.class.getName());
public void myMethod() {
LOG.info("Logging some message");
}
(The log4j2 config file is temporarily on my desktop because it seems that I can't use spaces in the path)
As far as I can see your configuration is correct and all is set up as described in the log4j2 manual page for web apps.
One thing you could try is putting the log4j2.xml config file in the classpath (under WEB-INF/classes) and removing the log4jConfiguration context-param setting. But since you are seeing an info.log file being created, it is likely that the log4j2.xml file is already discovered correctly, so putting it in the classpath may not help. (Worth a try though.)
Two other reasons I can think of why nothing appears in the log file: log output is buffered and the buffer is not flushed on every event. However, the default for FileAppender is immediateFlush=true, so that would mean a new bug was introduced in rc1. This is possible, but not likely (current trunk source looks correct).
Finally, would it be possible that your method that does the logging (myMethod in your example above) is not called? Is there a way to verify that this method was called?
In my case, I has configured log4j2.xml file correctly, but while actually logging, I mistakenly used the #Slf4j annotation in Lombok. Changing it to #Log4j2 resolved the issue.
I'm writing a java Application for Tomcat 7.
I have a bean configuration for a class that creates a log file and appends information to it.
now the question is how can I know the tomcat log directory path in bean configuration.
for now I have the following bean:
<bean id="foo_logger" class="com.bar.LoggerBean">
<!-- <property name="logPath" value="/path/DWHEventGenerator.log"/> -->
<property name="logPath" value="/var/lib/tomcat7/logs/mylog.log"/>
<property name="logLevel" value="ALL"/> <!-- ALL, FINE/R/ST, INFO, SEVERE, WARNING, OFF -->
</bean>
what i'd like to do is instead of specify /var/log/tomcat7/log, is to specify some variable that will indicate the actual path of the logs directory of tomcat. is that possible ?
thank you.
The simplest approach would be to use catalina.base from the system properties in the logPath property value
<property name="logPath" value="${catalina.base}/logs/mylog.log"/>
This property will be set by Tomcat's launch script (catalina.sh/catalina.bat) so will be available for use when Spring loads the application context file.
This is quite similar question to one older but the solution did not work for me.
I have a WAR package.
In web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
In application-context.xml
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:social.properties</value>
</property>
</bean>
But getting this:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/social.properties]
I checked the WAR package - .xml and .properties files are both in /WEB-INF/classes
.properties file is in src/main/resources and .xml in src/main/java (in default package both) and maven transports them (I think) correctly in the default package of WEB-INF/classes
Does anyone know why i could get this exception? Thank you.
EDIT: I just want to add that JUnit tests goes correctly (i mean they load what they should from social.properties) but when running the app it ignores my classpath: prefix
Do not use classpath. This may cause problems with different ClassLoaders (container vs. application). WEB-INF is always the better choice.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
and
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/social.properties</value>
</property>
</bean>
Put the things like /src/main/resources/foo/bar.properties and then reference them as classpath:/foo/bar.properties.
Try to use classpath*: prefix instead.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-classpath-wildcards
Also please try to deploy exploded war, to ensure that all files are there.
I think currently the application-context.xml file is into src/main/resources AND the social.properties file is into src/main/java... so when you package (mvn package) or when you run tomcat (mvn tomcat:run) your social.properties disappeared (I know you said when you checked into the .war the files are here... but your exception says the opposite).
The solution is simply to put all your configuration files (application-context.xml and social.properties) into src/main/resources to follow the maven standard structure.
Are you having Tomcat unpack the WAR file? It seems that the files cannot be found on the classpath when a WAR file is loaded and it is not being unpacked.
try with this code...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>/social.properties</value>
</list>
</property>
</bean>
Mark sure propertie file is in "/WEB-INF/classes" try to use
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/social.properties</value>
</property>
</bean>
I had the same error.
My filename was jpaContext.xml and it was placed in src/main/resources. I specified param value="classpath:/jpaContext.xml".
Finally I renamed the file to applicationContext.xml and moved it to the WEB-INF directory and changed param value to /WEB-INF/applicationContext.xml, then it worked!