I am trying to configure log4j 2.0 to report logs.
My config is saved as log4j2.xml and this is its content:
<?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="trace">
<appender-ref ref="MyFileAppender"/>
</root>
</loggers>
</configuration>
It exists in the classpath of the project and I tried putting it in many other directories..
I created a logger in the code like so:
Logger logger = LogManager.getLogger(MyClass.class.getName());
logger.info("test");
And nothing is written and no file is created. When I debug the code I see that the logger is the default logger(console).
In the documentation of log4j 2: http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration
but it is not working with classpath. Instead if we keep it in src folder, then it is working.
similar problem is mentioned here : https://issues.apache.org/jira/browse/LOG4J2-357
Ok, I solved the problem. I had to specify in the xml the package="myPackage"
I also faced the same problem.I kept my log4j2.xml file in System environment variable.
Variable name : sys_logroot Variable value : D:\user\gouse
and no logs are created for me.
use the system variable
-Dlog4j.configurationFile=path/to/log4j2.xml
See hereThis solve my problem
I'm quite sure that you have to write down the full qualified name of the class whose messages you want to be logged - something like
com.application.log4jtest.YourClass
. If that doesn't work, try fiddling with the log level.Also - just as a notice - you can also write
I had the same 'ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.' message over and over. It made me crazy. The log4j2.xml file was placed correctly at src/main/resources, like i did at thousands of projects before.
The solution in my case was to remove <packaging>pom</packaging> from the root pom.xml. packaging pom causes the content of src/main/resources not to be copied to target/classes.
Happy logging for anyone with the same root cause.
I had similar problem. I put the file under src folder and it worked. I did not mention any package name in the log4j2.xml file.