Springboot consolidates log4j2 and sends logs to Flume

Source: Internet
Author: User

Springboot Integration Log4j2 The general steps are simple, but there are a few small details to pay attention to.

First add log4j2 to the Pom file

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId> Spring-boot-starter-log4j2</artifactid>
    </dependency>

To force the use of LOG4J2 when the Springboot is started, you need to exclude the default log:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId> spring-boot-starter-web</artifactid>
      <exclusions>
        <exclusion> 
          <groupId> Org.springframework.boot</groupid>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

The LOG4J2 configuration file Log4j2.xml is then added under resource.

Then specify the configuration file for Log4j2 in the default profile application.properties:

Logging.config=classpath:log4j2.xml

It should be noted that, according to the official documentation, the log configuration file is automatically discovered, and it is true in the development and commissioning process. But if the war package is to be deployed to Tomcat, it is Tomcat under Linux. There will be a problem, manually specified before it will take effect. It's no problem if you just hit the jar pack to start with the main method.

This springboot integration log4j2 is basically complete.

Then configure the flume in the LOG4J2

Or start with the Pom file:

<dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId> log4j-flume-ng</artifactid>
	</dependency>
    <dependency>
     <groupId> org.apache.flume</groupid>
      <artifactId>flume-ng-embedded-agent</artifactId>
      < version>1.7.0</version>
      <exclusions>
      	<exclusion>
      		<groupId>org.slf4j< /groupid>
      		<artifactId>slf4j-api</artifactId>
      	</exclusion>
      	<exclusion >
      		<groupId>org.slf4j</groupId>
      		<artifactId>slf4j-log4j12</artifactId>
      	</exclusion>
      	<exclusion>
      		<groupId>log4j</groupId>
      		<artifactid >log4j</artifactId>
      	</exclusion>
      </exclusions>
    </dependency>

The flume supported by LOG4J2 is embedded mode, so join Flume-ng-embedded-agent

Then add flumeappend to the Log4j2 configuration 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 SSS} [MyApp]%m%n"/> < /console> <flume name= "Flumeappender" compress= "false" Type= "Embedded" > <property name= "Channel.typ" E ">memory</Property> <property name=" channel.capacity ">200</Property> <property name=" Sinks ">agent1</Property> <property name=" Agent1.type ">avro</Property> <property name=" Agent1.hostname ">192.168.0.100</Property> <property name=" Agent1.port ">44444</Property> & Lt Property Name= "Agent1.batch-size" >100</Property> <property name= "Processor.type" >failover</ property> <patternlayout charset= "UTF-8" pattern= "%d{yyyy-mm-dd HH:mm:ss SSS} [MyApp]%m%n"/> </flu
Me> </Appenders>  <Loggers> <logger name= "SysLog" level= "Trace" > <appenderref ref= "Flumeappender"/> ogger> <root level= "info" > <appenderref ref= "Console"/> </Root> </Loggers> < /configuration>

OK, it's over.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.