log4j2 和logstash整合,logstash重启之后,程序需要重启问题解决

最近在做后台项目改造,将原来的项目变成springCloud微服务架构的。其中做了elk日志收集,人手有限没有用到异步消息,filebeat之类的,先做个低配的吧。直接log4j2将日志扔到logstash里面去,大体配置如下:


   
       name="Console" target="SYSTEM_OUT">
           
           level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
           pattern="${log.pattern}"/>
       

       name="Logstash_client" host="xxx.xxx.xxx.xx" port="1560" protocol="TCP">
           pattern="${PATTERN}" />
       
       name="Logstash_edit" host="xxx.xxx.xxx.xx" port="1561" protocol="TCP">
           pattern="${PATTERN}" />
       



   
       name="client_log" level="debug">
           ref="Logstash_client"/>
           ref="Console"/>
       
       name="edit_log" level="debug" additivity="false">
           ref="Logstash_edit"/>
           ref="Console"/>
       
   

logstash那边的配置tcp接收

  1. input {  
  2.     tcp{  
  3.         port => 1560
  4.     }
  5.     tcp{
  6.         port => 1561
  7.     }  

java里面调用:

//编辑后台的logger
Logger logger_edit= LogManager.getLogger("edit_log");
//客户端的logger
Logger logger_client= LogManager.getLogger("client_log");

ok,可以接收到。

可是有个问题,logstash重启之后,就接收不到日志了,除非log4j2程序重启才可以,这不行啊,线上环境岂能随便重启。百度谷歌群里大咖一通问,找到办法了。不适用socket长连接甩日志了,改用gelf就可以,基本配置:

pom:


   biz.paluch.logging
   logstash-gelf
   1.11.1

log4j2:

name="Logstash_client" host="udp:ip地址" port="1560" version="1.1" extractStackTrace="true"
             filterStackTrace="true" mdcProfiling="true" includeFullMdc="true" maximumMessageSize="8192"
             originHost="%host{fqdn}" additionalFieldTypes="fieldName1=String,fieldName2=Double,fieldName3=Long">
           name="level" pattern="%level" />
           name="message" pattern="%message" />
       

       name="Logstash_edit" host="udp:ip地址" port="1561" version="1.1" extractStackTrace="true"
             filterStackTrace="true" mdcProfiling="true" includeFullMdc="true" maximumMessageSize="8192"
             originHost="%host{fqdn}" additionalFieldTypes="fieldName1=String,fieldName2=Double,fieldName3=Long">
           name="level" pattern="%level" />
           name="message" pattern="%message" />
       
       




   
       name="client_log" level="debug">
           
           ref="Console"/>
       
       name="edit_log" level="debug">
           
           ref="Console"/>
       
       level="debug">
           
           ref="Console"/>
       
   

logstash:

input{
    gelf {
        port => 1561
        tags => ["cms_log"]
        codec => "json"
    }
    gelf {
        port => 1560
        tags => ["client_log"]
        codec => "json"
    }
    stdin{}


}
filter {
  date {
    match => [ "timeMillis", "UNIX_MS" ]
  }
  json {
   source => "message"
   remove_field => ["short_message","facility","host","message"]
  }
  if "cms_log" in [tags] {
    geoip {
      source => "ip"
    }
  }
}

java程序调用方法一样,ok这样重启一下logstash,应用程序就不互相影响了。不过貌似要想更好的使用还得需要加异步消息缓冲储存,以后再研究吧~


你可能感兴趣的:(日常工作)