springboot logback/log4j配置 log输出到ELK

logstash配置文件如下

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

input {
  tcp {
    mode => "server"
    host => "127.0.0.1"
    port => 4567
    codec => json_lines
  }
}

output {
 elasticsearch {
    action => "index"          #The operation on ES
    hosts  => "127.0.0.1:9200"   #ElasticSearch host, can be array.
    index  => "message-center-applog-%{+YYYY.MM.dd}"         #The index to write data to.
  }
  stdout{  
      codec=>rubydebug  
  }
}

logback配置appender

添加依赖

 // https://mvnrepository.com/artifact/net.logstash.logback/logstash-logback-encoder
    compile group: 'net.logstash.logback', name: 'logstash-logback-encoder', version: '6.1'

springboot 默认采用logback作为日志工具,只需在resources目录下添加logback.xml即可



    
    
        
            ERROR
        
        127.0.0.1:4567
        
    

    
        
        
    

log4j配置

如果使用log4j,在springboot中需要先exclude默认的logback,然后添加springboot的log4j依赖

gradle排除依赖

configurations {
	providedRuntime
	// remove default logger
	all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

添加log4j依赖

implementation('org.springframework.boot:spring-boot-starter-log4j2:2.0.5.RELEASE')

同样需要在resources目录下添加log4j2-spring.xml配置文件


    
        
            
        
        
            
        
    
    
        
            
            
        
    

配置完成。logback的filter配置成功,只往ELK输出指定级别的日志,log4j暂时还没有配置成功,请参考其他案例。

kibana打开面板查询

springboot logback/log4j配置 log输出到ELK_第1张图片

你可能感兴趣的:(ELK,springboot)