本篇文章中,将介绍使用 filebeat + logstash + elasticsearch 进行日志收集和展示。

安装 Filebeat

此处只介绍 Windows 下面的安装,至于其他系统, 请参考:官方文档

下载并解压后,有两种方式运行,一种是注册为 Windows 服务,另一种是直接通过命令行运行;下面分别介绍两种方式。
注册为 Windows 服务
前提:系统必须有 PowerShell ,因为官方安装包中提供的脚本只能在 PowerShell 中运行,若是 win10 系统,可忽略,因为它已经自带了 PowerShell , 否则请下载 PowerShell 并安装。

  • 下载安装包 点我下载.
  • 解压到以下目录: C:\Program Files 。
  • 重命名 filebeat-<version>-windows 为 Filebeat 。
  • 以 管理员 身份运行 PowerShell 。
  • 在 PowerShell 中运行以下命令:
cd 'C:\Program Files\Filebeat'
C:\Program Files\Filebeat> .\install-service-filebeat.ps1

注:> 如果此处提示你没有权限,请运行以下的命令注册 Filebeat 服务 :

PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-filebeat.ps1

到这,已经将 Filebeat 成功注册为系统服务,当下一次开机时它会自动启动,当然你也可以手动通过服务控制面板启动它。
通过命令行运行 Filebeat
通过命令行运行 Filebeat 非常简单,只需将 Filebeat 文件解压到某个目录后,通过以下命令运行:

filebeat -e -c filebeat.yml

配置 Filebeat

日志输入配置
Filebeat 使用了安装目录下的 filebeat.yml 文件进行相关配置。此处我们主要会用到以下的配置:

filebeat.prospectors:
- type: log
# 此处需特别注意,官方默认配置为 false,需要修改为 true
  enabled: true
  paths:
  # 此处配置的是需要收集的日志所在的位置,可使用通配符进行配置
    - D:/logs/*.log

日志输出配置
因为我们使用的是 logstash 收集日志,所以得注释掉默认的 elasticsearch 配置,并取消 logstash 的注释,最终的效果为:

#output.elasticsearch:
  # Array of hosts to connect to.
  #hosts: ["localhost:9200"]

  # Optional protocol and basic auth credentials.
  #protocol: "https"
  #username: "elastic"
  #password: "changeme"
output.logstash:
  # The Logstash hosts
  hosts: ["localhost:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

此处仅介绍了最基础的配置,如需查看更多高级配置,请查看:官方文档

关于 Filebeat 的配置已经介绍完毕,下面我介绍 log4j2 的配置。

配置 Log4j2

因为我们使用的是 Filebeat 进行日志收集,所以我们只需要简单的将日志输出到本地文件中即可,这里我将使用 RollingFile 进行相关配置:

log4j-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Properties>
    <Property name="pattern" value="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  </Properties>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="${pattern}"/>
    </Console>
    <RollingFile name="RollingFile" fileName="D:/logs/app.log"
      filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
      <PatternLayout pattern="${pattern}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="250 MB"/>
      </Policies>
    </RollingFile>
    <Async name="AsyncRollingFile">
      <AppenderRef ref="RollingFile"/>
    </Async>
  </Appenders>
  <Loggers>
    <Root level="DEBUG">
      <AppenderRef ref="Console"/>
      <AppenderRef ref="AsyncRollingFile"/>
    </Root>
  </Loggers>
</Configuration>

配置 logstash

这里我们只需要在加入以下的配置即可:

input {
  beats {
    port => 5044
  }
}

此处的端口需要和 Filebeat 中配置的端口一致。

好了,所有的配置都已经完成,这里就不再重复 kibana 和 elasticsearch 的配置了,如有需要,请查看上一篇文章。

运行效果

我们启动其它服务并登陆 kibana 后,就可以看到以下的结果了:

运行效果

查看源码

关于 Filebeat 的介绍就到此结束了,关于 logstash 的更多高级功能将在后续文章中一一介绍。

最后,附上本文源码: Github