ELK-log4j2异步输出+logstash

Posted linjiqin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ELK-log4j2异步输出+logstash相关的知识,希望对你有一定的参考价值。

1、pom.xml配置文件

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${log4j.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- log4j2 AsyncLogger need disruptor-->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.2</version>
</dependency>

 

2、log4j2.xml配置信息

<?xml version="1.0" encoding="UTF-8" ?>

<Configuration>
    <Properties>
        <property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-30.30C{1.} : %m%n</property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}" />
        </Console>
        
        <RollingFile name="RollingFile" fileName="d:/logs/main.log"
            filePattern="d:/logs/$${date:yyyy-MM-dd}/xinyar-%d{MM-dd-yyyy}-%i.log.gz"
            immediateFlush="false" append="true">
            <PatternLayout pattern="${LOG_PATTERN}" />
            <!-- 每个文件大小 -->
            <!-- <SizeBasedTriggeringPolicy size="50M" /> -->
            <!-- <SizeBasedTriggeringPolicy size="50KB" /> -->
            <SizeBasedTriggeringPolicy size="20M" />
            <!-- 不设置,同一文件夹下默认最多7个文件,这里设置同一文件夹下最多50个文件 -->
            <DefaultRolloverStrategy max="50" />
        </RollingFile>
     <!-- 47.*.*.159为logstash主机外网IP,4560为logstash端口 -->
        <Socket name="logstash-tcp" host="47.*.*.159" port="4560" protocol="TCP">
            <PatternLayout pattern="${LOG_PATTERN}" />
        </Socket>
    </Appenders>
    <Loggers>
        <!-- 异步发送logstash -->
        <!-- 如果使用<asyncRoot> 或 <asyncLogger>,includeLocation="true"是必须要设置才会有类路径等一些信息打印出来 -->
        <AsyncLogger name="com.xinyartech" level="info" includeLocation="true" >
            <appender-ref ref="logstash-tcp" />
        </AsyncLogger>
     
        <Root level="INFO">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFile"/>
        </Root>    
    </Loggers>
</Configuration>

 

3、logstash配置信息--test4.conf

input {
    tcp {  
        port => 4560 
        codec => json
    }  
} 
 
output {    
    elasticsearch {
        hosts => "127.0.0.1:9200"
            index => "es-message-%{+YYYY.MM.dd}"    
    }
    stdout { codec => rubydebug }
}

 

4、启动logstash
$ ./logstash -f ../config/test4.conf &

以上是关于ELK-log4j2异步输出+logstash的主要内容,如果未能解决你的问题,请参考以下文章

logback之Appender源码解读

Log4j2中的同步日志与异步日志

Spring Boot 使用 Log4j2 & Logback 输出日志到 EKL

springboot日志输出到logstash,logstash端没有日志输出,为啥?

slf4j-logback 日志以json格式导入ELK

logback怎样通过http访问log