'Simple Log4j2 application does not logging to file. log4j xml say "Element File is not allowed here"
I have create a simple application that write lines to log using IntelliJ
Below is my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config" packages="com.test.log4j">
<Properties>
<Property name="logPath">./Log4j2Example/logs</Property>
</Properties>
<Appenders>
<!-- File Appender -->
<File name="FILE" fileName="${logPath}/logfile.log" append="true">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</File>
<!-- Console Appender -->
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.test" level="debug" />
<Root level="info">
<AppenderRef ref="STDOUT" />
<AppenderRef ref="FILE" />
</Root>
</Loggers>
</Configuration>
My Main class is as follow:
package com.test.log4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
public class MainTest {
private static final Logger LOG = LogManager.getLogger(MainTest.class);
public static void main(String[] args){
//private final static Logger LOG = LogManager.getLogger(MainTest.class);
System.out.println("Hello World"); // This printed out in console
LOG.info("This Will Be Printed On Info xxx");
LOG.info("This Will Be Printed On Info yyy");
}
}
I'm also curious why in the intelliJ file editor it say "Element File is not allowed here" for log4j2.xml when I hover mouse over "File" element:
What is the IDE trying to tell when it say "Element xx is not allowed here"? I thoughts this is some intelliJ misbehavior so I've tried restart IDE and restart the machine, but it does not seems to fix it.
How can I get this to write the log file?
Edit:
log4j2.xml is placed in "C:\Git\POC\TestLog4j\src\main\resources"
I'm not using gradle or maven to build. I downloaded the log4j 2.17.1 (core and api) and placed it in "C:\Git\POC\TestLog4j\lib" I've added lib path in the [project Structure -> Modules -> dependencies].
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

