It's easy, but I'll show you how to use log4j 2.0 as a reminder. Investigate for use when creating small Java apps.
Added log4j to Maven used for library management
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>
Set to output logs to standard output.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<Configuration status="off">
<Properties>
<Property name="format1">%d{yyyy/MM/dd HH:mm:ss.SSS} [%t] %-6p %c{10} %m%n</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<pattern>${format1}</pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
The name of the logger should just be copied and pasted using lookup.
public class Hoge {
private static final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass());
public void sayHoge() {
logger.info("hoge hoge");
}
}
Recommended Posts