Reputation: 509
This is my configuration file:
<Configuration status="INFO">
<Properties>
<Property name="log-path">./log</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/mylog.log" filePattern="${log-path}/mylog-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%p]\t%m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="32" />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT" ignoreExceptions="false">
<PatternLayout>
<pattern>%n%d{yyyy-MM-dd HH:mm:ss.SSS} [%p]\t%m</pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Logger name="mylog" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="DEBUG" />
<appender-ref ref="STDOUT" level="DEBUG" />
</Logger>
</Loggers>
</Configuration>
But I get the following error:
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
How can I solve it?
This is my Maven dependency:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
The log configuration file is very clearly read, since it correctly writes using the defined patterns in my /log directory.
This is how I instantiate it in my code:
LoggerContext lcontext = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)
Logger logger = LogManager.getLogger("mylog");
Upvotes: 4
Views: 17859
Reputation: 1063
If you are using a web servlet, you will also need this dependency:
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
https://logging.apache.org/log4j/2.x/maven-artifacts.html
Upvotes: 1
Reputation: 460
I have fixed this in my Spring Boot app by putting log4j2.xml inside src/main/resources
folder. It STILL gives me the "StatusError No Log4j 2 configuration file found" during compilation, BUT when it is runnung, it ACTUALLY follows my configuration. Hope this helps.
Upvotes: 4
Reputation: 1
from log4j-user-guide 6.1.1 Configuration:
- if no location...."log4j2" in WEB-INF directory ...
But this does not work (at least in my case). The below one works:
WEB-INF/classes/log4j2.xml
Hope this helps.
Application Server: wildfly 14
Upvotes: 0
Reputation: 51
In my case I had kept the file at src/main/resources. But Log4J was still unable to find it as the JAR created by Maven plugin didn't contain the log4j2.xml file.
To get rid of this problem, I kept the file at root of the folder (just next to pom.xml). Now, when I created a JAR file using Maven Plugin, Log4j could access it. And it worked ! :)
Hope this will help you.
Upvotes: 0
Reputation: 182
You should be able to tell the system where the log4j2 configuration file is by using System.setProperty("log4j.configurationFile", log4j2ConfigFile);
Upvotes: 0