I have a problem applying the log4j2.xml auto configuration properly, and I think it has something to do with my folder arrangement.
I'm using maven to add log4j2 libs and arrange my projects as follows:
- one project to contain all "common" classes, used by server and client side of my system.
- another "core" project - the server side application.
Both projects use the same general package hierarchy (like com.foo.specific.package)
In the Common project I define a logger wrapper:
public class LogWrapper
{
static Logger systemParentLogger = LogManager.getLogger("com.foo");
public static Logger getLogger(Class<?> cls)
{
return LogManager.getLogger(cls.getName());
}
}
Moreover, the Common project contains the log4j2.xml file under META-INF (alongside the persistence.xml file for Hibernate usage).
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="PRODUCTION" status="OFF">
<appenders>
<appender type="RollingFile"
name="MyFileAppender"
fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n"/>
</appender>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="MyFileAppender"/>
</root>
<logger name="com.foo" level="info" additivity="false">
<appender-ref ref="MyFileAppender"/>
</logger>
<logger name="org.hibernate" level="error">
<appender-ref ref=MyFileAppender"/>
</logger>
</loggers>
</configuration>
While running a sample code in the Core project (using the LogWrapper I wrote and some JPA voodoo), I could still see INFO hibernate logs, and no log file was created. I should state that while debugging the code, I could see that the logger fetched was given some weird value "com.foo.core.persistence.PersistenceXMLTest:ERROR in sun.misc.Launcher$AppClassLoader@2f600492"
The log4j2.xml was placed in a "Folder" which in eclipse terms is "not on classpath".
Changing META-INF to be a "source folder" solved the problem.
In addition, the log4j2.xml file was not defined properly.
These are the modifications needed:
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="PRODUCTION" status="OFF">
<appenders>
<RollingFile name="MyFileAppender"
fileName="../Logs/app.log"
filePattern="../Logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="MyFileAppender"/>
</root>
<logger name="com.foo" level="info" additivity="false">
<appender-ref ref="MyFileAppender"/>
</logger>
</loggers>
</configuration>
Still couldn't make the org.hibernate logger to be redirected to my logs, but at least I got the logger to work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With