Issue
Well, I have 3 spring profiles: dev, prod, test, and I want to use different log4j2 configuration in different profiles. I checked the the rel="noreferrer">spring-boot reference and followed the way it said. But when I run spring application, I only get the log below:
2018-03-05 09:52:32,194 main ERROR Error processing element SpringProfile ([Configuration: null]): CLASS_NOT_FOUND
2018-03-05 09:52:32,194 main ERROR Error processing element SpringProfile ([Configuration: null]): CLASS_NOT_FOUND
I googled and stackoverflowed the error log, and can't still find an answer why springProfile
tag didn't work.
And here is my log4j2-spring.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<SpringProfile name="prod">
<Appenders>
<RollingFile name="RollingFile"
fileName="/home/prod/service.log"
filePattern="/home/prod/service.log.%d{yyyyMMddHH}"
append="true">
<PatternLayout pattern="[%level][%d{yyyy-MM-dd'T'HH:mm:ss.SSSXX}][%l] %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</SpringProfile>
<SpringProfile name="!prod">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%level][%d{yyyy-MM-dd'T'HH:mm:ss.SSSXX}][%l] %msg%n" />
</Console>
<File name="File" fileName="./logs/service.log" append="false">
<PatternLayout pattern="[%level][%d{yyyy-MM-dd'T'HH:mm:ss.SSSXX}][%l] %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="File"/>
<!--When WIP, you could uncomment the next line to show log to console.-->
<!--<AppenderRef ref="Console"/>-->
</Root>
</Loggers>
</SpringProfile>
</Configuration>
Solution
There's no relationship (or at least not that I know) between Apache Log4J2 and the Spring Framework.
The SpringProfile
tag you've used doesn't exist in the Log4J2's XML schema either.
You would have to play with different Spring Profile configurations and different Log4J2 configuration for each. On the other hand, Logback and Log4J2 are (almost completely) different libraries for the same purpose of logging.
Answered By - x80486
Answer Checked By - Robin (JavaFixing Admin)