Issue
I have a war file (web-services) deployed on weblogic 12c that reads the log4j.properties file but doesn't use it. Below is the debug of log4j on application startup showing the parsing of the file and the setting but the logs are not being written to the log file specified and the ones in stdout do not match the pattern specified and the levels configured.
log4j: Reading configuration from URL file:////weblogic/apps/config/ws/log4j.properties
log4j: Parsing for [root] with value=[WARN, stdout, wslogfile].
log4j: Level token is [WARN].
log4j: Category root set to WARN
log4j: Parsing appender named "stdout".
log4j: Parsing layout options for "stdout".
log4j: Setting property [conversionPattern] to [%d %6.6p [%30.30c{1}][%10.10t] %m%n].
log4j: End of parsing for "stdout".
log4j: Parsed "stdout" options.
log4j: Parsing appender named "wslogfile".
log4j: Parsing layout options for "wslogfile".
log4j: Setting property [conversionPattern] to [%d %6.6p [%30.30c{1}][%10.10t] %m%n].
log4j: End of parsing for "wslogfile".
log4j: Setting property [file] to [/weblogic/apps/logs/ws.log].
log4j: Setting property [datePattern] to ['.'yyyy-MM-dd].
log4j: setFile called: /weblogic/apps/logs/ws.log, true
log4j: setFile ended
log4j: Appender [wslogfile] to be rolled at midnight.
log4j: Parsed "wslogfile" options.
log4j: Parsing for [com.app] with value=[DEBUG, stdout, wslogfile].
log4j: Level token is [DEBUG].
log4j: Category com.app set to DEBUG
log4j: Parsing appender named "stdout".
log4j: Appender "stdout" was already parsed.
log4j: Parsing appender named "wslogfile".
log4j: Appender "wslogfile" was already parsed.
log4j: Handling log4j.additivity.com.app=[false]
log4j: Setting additivity for "com.app" to false
log4j: Parsing for [com.comp] with value=[DEBUG, stdout, wslogfile].
log4j: Level token is [DEBUG].
log4j: Category com.comp set to DEBUG
log4j: Parsing appender named "stdout".
log4j: Appender "stdout" was already parsed.
log4j: Parsing appender named "wslogfile".
log4j: Appender "wslogfile" was already parsed.
log4j: Handling log4j.additivity.com.comp=[false]
log4j: Setting additivity for "com.comp" to false
log4j: Parsing for [org.springframework.ws.server.MessageTracing.received] with value=[TRACE, stdout, wslogfile].
log4j: Level token is [TRACE].
log4j: Category org.springframework.ws.server.MessageTracing.received set to TRACE
log4j: Parsing appender named "stdout".
log4j: Appender "stdout" was already parsed.
log4j: Parsing appender named "wslogfile".
log4j: Appender "wslogfile" was already parsed.
log4j: Handling log4j.additivity.org.springframework.ws.server.MessageTracing.received=[false]
log4j: Setting additivity for "org.springframework.ws.server.MessageTracing.received" to false
log4j: Finished configuring.
My log4j.properties is as below log4j.rootLogger=WARN, stdout, wslogfile
log4j.logger.com.app=DEBUG, stdout, wslogfile
log4j.additivity.com.app=false
log4j.logger.com.comp=DEBUG, stdout, wslogfile
log4j.additivity.com.comp=false
log4j.logger.org.springframework.ws.server.MessageTracing.received=TRACE, stdout, wslogfile
log4j.additivity.org.springframework.ws.server.MessageTracing.received=false
log4j.appender.wslogfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.wslogfile.File=/weblogic/apps/logs/ws.log
log4j.appender.wslogfile.DatePattern='.'yyyy-MM-dd
log4j.appender.wslogfile.layout=org.apache.log4j.PatternLayout
log4j.appender.wslogfile.layout.ConversionPattern=%d %6.6p [%30.30c{1}][%10.10t] %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %6.6p [%30.30c{1}][%10.10t] %m%n
Sample output of the logs (only showing in stdout)
Sep 28, 2022 11:42:17 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'spring-ws': initialization started
Sep 28, 2022 11:42:17 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'spring-ws-servlet': startup date [Wed Sep 28 11:42:17 AST 2022]; root of context hierarchy
Sep 28, 2022 11:42:17 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-ws-servlet.xml]
Sep 28, 2022 11:42:17 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/service-context.xml]
Sep 28, 2022 11:42:18 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/stmt-client-context.xml]
My pom.xml is as below
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
Solution
So the issue got solved by updating the weblogic.xml file under the war file to specify the "prefer-application-packages" of log4j.properties as below
<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.3/weblogic-web-app.xsd">
<session-descriptor/>
<jsp-descriptor/>
<container-descriptor>
<prefer-application-packages>
<package-name>org.apache.commons.*</package-name>
<package-name>org.slf4j.*</package-name>
</prefer-application-packages>
</container-descriptor>
<context-root>test-context-root</context-root>
</weblogic-web-app>
Answered By - wassim
Answer Checked By - David Goodson (JavaFixing Volunteer)