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 &

上一篇:C# 监测每个方法的执行次数和占用时间(测试4)


下一篇:Java项目出现的问题01