Reputation: 29
I'm using Spring boot and Gradle build tools. I want to add a logger which logging my events. I chose Log4j2. first of all, I have added these dependencies:
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.14.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.14.1'
then added this configuration file, to my resources folder
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
</Appenders>
<RollingFile name="FileAppender" fileName="logs/log4j2-demo.log"
filePattern="log4j2-demo-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender" />
</Root>
</Loggers>
</Configuration>
then I use it via this command:
private static final Logger logger = LogManager.getLogger(Application.class);
logger.error("Hello from Log4j 2");
I'm in doubt that Spring uses the configuration file at all. Because it won't create the log folder and log4j2-demo.log file. What is the problem?
Upvotes: 0
Views: 370
Reputation: 481
Use LoggerFactory
private static final Logger LGR = LoggerFactory.getLogger(ObjectsController.class);
Upvotes: 1