集成log4jlog4j2logback与elk
Posted 奥林匹斯圣山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集成log4jlog4j2logback与elk相关的知识,希望对你有一定的参考价值。
集成log4j、log4j2、logback与elk
目前应用开发用的规范日志框架有log4j、log4j2、logback,而ELK目前官方的插件仅支持log4j,log4j日志架构目前已经不再更新,更多的应用使用log4j2, logback框架,在目前的形式下集成log4j,log4j2, logback与elk,实现不同日志框架的集中日志收集显得很有必要。
log4j2, logback提供json格式的日志,logstash通过使用TCP Plugin收集日志,无需做grok的解析,日志按相应的格式输出到elk平台。
logback配置
pom.xml
文件中添加如下内容
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.9</version>
</dependency>
编辑logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8"> <!-- encoder 可以指定字符集,对于中文输出有意义 -->
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
</pattern>
</encoder>
</appender>
<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>10.72.243.131:5046</destination>
<!-- encoder is required -->
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"application":"myApplication"}</customFields>
</encoder>
</appender>
<root level="info"> <!-- 设置日志级别 -->
<appender-ref ref="STDOUT" />
<appender-ref ref="stash" />
</root>
</configuration>
log4j配置
log4j-1.2.17.jar、
log4j-socketappender-hostname.jar和'jsonevent-layout-1.7.jar'
放到/usr/local/apache-tomcat-7.0.69/webapps/
应用/WEB-INF/lib
两种配置文件将日志输送到elk平台
2.1. #vim log4j.properties 添加以下内容(
/usr/local/apache-tomcat-7.0.69/webapps/application/WEB-INF/classes
)log4j.rootLogger log4j.rootLogger=info,stdout,info,debug,error,Kibana log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=net.logstash.log4j.JSONEventLayoutV1 log4j.appender.Kibana=org.apache.log4j.net.SocketAppenderWithHostname log4j.appender.Kibana.Port=4560 log4j.appender.Kibana.RemoteHost=10.72.243.131 log4j.appender.Kibana.layout=net.logstash.log4j.JSONEventLayoutV1 log4j.appender.Kibana.ReconnectionDelay=10000 log4j.appender.Kibana.LocationInfo=true log4j.appender.Kibana.application=application名字 log4j.appender.Kibana.MdcKeyForHostname=Docker_Name
2.1. #vi log4j.xml 添加以下内容(
/usr/local/apache-tomcat-7.0.69/webapps/application/WEB-INF/classes
)<appender name="logserver" class="org.apache.log4j.net.SocketAppenderWithHostname"> <param name="Port" value="4560"/> <param name="RemoteHost" value="10.72.2413.131"/> <param name="ReconnectionDelay" value="10000"/> <param name="LocationInfo" value="true"/> <param name="application" value="application名字"/> <param name="MdcKeyForHostname" value="Docker_Name"/> <layout class="net.logstash.log4j.JSONEventLayoutV1"> </layout> </appender>
log4j2配置
配置
pom.xml
文件<!—从Spring中取消默认logback --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!—从Spring中增加log4j2 --> <!- 开发不使用Maven和Spring的,要自己增加相关的Jar包引用—> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>
配置
log4j2.xml
文件<?xml version="1.0" encoding="UTF-8"?> <configuration status="warn"> <Properties> <Property name="hostName2">$${ctx:hostName}</Property> <property name="applicationName">Demo8</property> </Properties> <appenders> <console name="Console" target="SYSTEM_OUT"> <JSONLayout complete="false"></JSONLayout> </console> <Socket name="Logstash" host="10.72.243.131" port="5046" protocol="TCP"> <JSONLayout complete="false" compact="true" eventEol="true" properties="true"> </JSONLayout> </Socket> </appenders> <loggers> <root level="error"> <property name="hostName">${hostName}</property> <property name="applicationName">"${applicationName}"</property> <appender-ref ref="Logstash" /> <appender-ref ref="Console" /> </root> </loggers> </configuration>
ELK平台配置
logstash.conf添加如下内容:
input{ log4j { type => "log4j" port => 4560 } tcp { port => 5046 tags => ["tags"] codec => json_lines } }
重启logstash服务,查看端口监听状态:
#systemctl stop logstash
#systemctl start logstash
#lsof -i:4560
#lsof -i:5046
以上是关于集成log4jlog4j2logback与elk的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 两步集成 日志收集ELK与分布式系统监控CAT