Reputation: 1561
I am relatively new to log4j2. I am trying to create a log file using log4j2 but it is not getting created. Any help is appreciated. Thanks in advance.
log4j.xml Configuration File(below):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
<Properties>
<Property name="basePath">../resources/logs</Property>
</Properties>
<Appenders>
<!-- File Appender -->
<File name="FILE" fileName="${basePath}/logfile.log" append="true">
<PatternLayout
pattern="[%-5p][%d{dd-MMM-yyyy HH:mm:ss a}] %C{0}.%M:(%L) - %m%n" />
</File>
<!-- Console Appender -->
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5p][%d{dd-MMM-yyyy HH:mm:ss a}] %C{0}.%M:(%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="log" level="all" />
<Root level="all">
<AppenderRef ref="FILE" level="all" />
<AppenderRef ref="STDOUT" level="all" />
</Root>
</Loggers>
</Configuration>
Project Folder Structure(below):
My Java Code(below):
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Demo {
public static Logger log;
public static void main(String[] args) {
System.err.println("Start");
System.setProperty("log4j.configurationFile", "./resources/config/logger/log4j2.xml");
log = LogManager.getLogger(Demo.class.getName());
System.err.println("Initialisied Logger");
log.trace("TRACE");
log.debug("DEBUG");
log.info("INFO");
log.warn("WARN");
log.error("ERROR");
log.fatal("FATAL");
System.err.println("END");
}
}
Maven Dependency(below):
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
Console Outout(below):
Start
Initialisied Logger
[TRACE][28-Feb-2018 14:45:11 PM] Demo.main:(17) - TRACE
[DEBUG][28-Feb-2018 14:45:11 PM] Demo.main:(18) - DEBUG
[INFO ][28-Feb-2018 14:45:11 PM] Demo.main:(19) - INFO
[WARN ][28-Feb-2018 14:45:11 PM] Demo.main:(20) - WARN
[ERROR][28-Feb-2018 14:45:11 PM] Demo.main:(21) - ERROR
[FATAL][28-Feb-2018 14:45:11 PM] Demo.main:(22) - FATAL
END
Upvotes: 2
Views: 18372
Reputation: 1561
Solved it by replacing the two dots to one dot ie ../resources
to ./resources
I know it is a silly mistake. :-)
<Properties>
<Property name="basePath">../resources/logs</Property>
</Properties>
Credit to @vikingsteve for pointing it out in the comments.
Upvotes: 1
Reputation: 1059
Had similar issues. It would be easier to just show you a working code, I'll give you mine below ;)
pom.xml
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>log4j2.xml</include>
</includes>
</resource>
</resources>
Demo.java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Demo {
public static Logger log;
public static void main(String[] args) {
System.err.println("Start");
private static final Logger log = LogManager.getLogger(Demo.class);
System.err.println("Initialisied Logger");
log.trace("TRACE");
log.debug("DEBUG");
log.info("INFO");
log.warn("WARN");
log.error("ERROR");
log.fatal("FATAL");
System.err.println("END");
}
}
It is better if you would copy your xml log file inside src/main/resources
.
log4j 2 - configuration issue
(i just took a random question about log4j2, because there are thousands, this would have helped you solved but i believe the code i posted here would help you too).
UPDATE:
I forgot to paste a working log4j2.xml
file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<RollingFile name="RollingFile" filename="/var/log/mylog.log"
filepattern="${logPath}/%d{YYYYMMddHHmmss}-fargo.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
UPDATE 2:
And, since everybody love screenshots, you'd have this folder structure. Netbeans, Eclipse ... Names could change, but src/main/resources
and pom.xml
are the same across all IDE.
Upvotes: 3