Try using Log4j 2.0

Introduction

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.

Maven dependency definition

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>

Create configuration file

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>

Implementation

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

Try using Log4j 2.0
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Try using Axon Framework
Try using JobScheduler's REST-API
Try using java.lang.Math methods
Try using PowerMock's WhiteBox
Try using Talend Part 1
Try using F # list
Try using each_with_index method
Try using Spring JDBC
Try using RocksDB in Java
Try using GloVe with Deeplearning4j
Try using view_component with rails
Try scraping using java [Notes]
Try using Cocoa from Ruby
Try using letter_opener_web for inquiries
[Swift] Try using Collection View
Try using IntelliJ IDEA once
Try using Spring Boot Security
Try using gRPC in Ruby
[Rails] Try using Faraday middleware
[Processing] Try using GT Force.
[Programming Encyclopedia] ยง2 Try using Ruby
People using docker Try using docker-compose
log4j2
Try using Redmine on Mac docker
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using the messaging system Pulsar
Try using IBM Java method tracing
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
Try using Java framework Nablarch [Web application]
Drafting log4j2
Try using || instead of the ternary operator
Try HiveRunner
Try using the service on Android Oreo
Try using the Rails API (zip code)
Try Mockito
Study Java Try using Scanner or Map
Try Selenium
Try DbUnit
Try using Spring Boot with VS Code
Try using Reladomo's MT Loader (Multi-Threaded Matcher Loader)
try docker-compose
Try using JobScheduler's REST-API --Java RestClient implementation--
Try using Kong + Konga with Docker Compose.
Try using the Emotion API from Android
Try using the Wii remote with Java
Try Lombok
Try using simple_form / edit even child models
Try implementing GraphQL server using grahpql-java-tools (+ kotlin)
Try using Firebase Cloud Functions on Android (Java)
Try using conditional branching other than if statement
Prepare for log output using log4j in Eclipse.
Beginners try using android studio Part 2 (event processing)
Try local file search using Fess on CentOS7