Log4j2 provides a SocketAppender that allows us to send logs over TCP or UDP. ELK is Logstash+ElasticSearch+Kibana. While Kibana provides an interface to check log information. This paper mainly describes the connection between Logstash and Log4j2. For building ELK platform, you can refer to this blog ELK Deployment Note (Docker-compose deployment).

Springboot’s default logging framework logback is changed to log4j2

1. Exclude logback (the first dependency exclusion in Dependencies) and introduce POM dependencies for log4j2

        <! The starter-Web is responsible for removing logbacks in the first place -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <! -- Remove the logback framework from SpringBoot, because the project will use the log4j2 framework -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
        <! Add jar for log4j2 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
Copy the code

2. Configure log4j2. XML and import it into the project


      
<Configuration>
    <Properties>
        <Property name="LOG_PATTERN">{"logger": "%logger", "level": "%level", "msg": "%message"}%n</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
        <Socket name="logstash" host="Logstash server IP address" port="Custom port" protocol="TCP">    
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Socket>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="logstash-tcp" />
        </Root>
    </Loggers>
</Configuration>
Copy the code

3. Introduce configuration in application.yml

# specify the path to log4j2
    logging:
      config:
        classpath:
          log4j2.xml
Copy the code

4. Use it in code

/ / introduction
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
public class MyClass{
    private static final Logger logger = LogManager.getLogger(CarController.class);
 
    public void myMethod(a){
        logger.error("error occurs here!"); }}Copy the code

2. Connect Logstash to Log4j2

Install the Logstash first (this article uses version 6.3.2 as an example)

The tar - ZXVF logstash - 6.3.2. Tar. GzCopy the code

Then go to the Logstash root directory and create the conf file directory

CD logstash - 6.3.2 mkdir confCopy the code

Add the Logstash configuration file

vim ./conf/logstash.conf
Copy the code

The configuration is as follows:

Input {TCP {port => Log4j2 port codec => json}} output {elasticSearch {hosts => "ELASticSearch IP address and port number" index => "es-message-%{+YYYY.MM.dd}" } stdout { codec => rubydebug } }Copy the code

Start the logstash

./bin/logstash -f ./conf/logstash.conf
Copy the code

Run the netstat -tlunp command to check whether port 9600 is started successfully.

Open the Web page and add indexes to view logs that can be monitored in real time