将我的 java 应用程序的日志消息写入 elk 堆栈的最佳方法是什么?

What is the best way to write log message of my java app into the elk stack?

我想将我的 Java 应用程序的日志数据保存到 ELK 堆栈中。我可以自由配置日志数据的格式。不幸的是我只能使用 log4j 1.x。

在 ELK Stack 中存储结构化日志数据的最简单方法是什么?

如果您想要一种将 Java 应用日志提取到 Elasticsearch 中的简单方法,请安装 & 运行 filebeat 以读取您的应用日志并将它们提取到 ES 中。您可以使用 filebeat 摄取节点来解析您的日志并将它们存储到 Elasticsearch 中的各个字段中以帮助可视化等。或者,您也可以安装 logstash 来进行解析而不是 filebeat 摄取管道。一些帮助您入门的阅读链接:

https://www.elastic.co/guide/en/beats/filebeat/current/configuring-howto-filebeat.html

https://www.elastic.co/guide/en/beats/filebeat/current/configuring-ingest-node.html

https://www.elastic.co/guide/en/logstash/current/configuration.html

编写结构化的文本日志(今天它意味着 Json),FileBeat 直接将它们传送到 ES(无需使用 Logstash 或 Fluentd 的麻烦):

filebeat.inputs:
- type: filestream
  enabled: true
  paths:
    - /app/log/test.log.json
  encoding: utf-8
  prospector.scanner.check_interval: 20s
  prospector.scanner.symlinks: false
  parsers:
    - ndjson:
        keys_under_root: true
  clean_removed: true

# For debugging.
output.console:
  enabled: true
  codec.json:
    pretty: true

output.elasticsearch:
  enabled: true
  hosts: ["es1:9200"]
  #protocol: "https"
  #api_key: "id:api_key"
  #username: "elastic"
  #password: "changeme"
  index: "test-%{+yyyy-MM-dd-hh-mm-ss}"

PS 目前我更喜欢 Fluent Bit 而不是 FileBeat。两者都可以直接将事件发送到 ES,中间没有 Logstash/Fluentd。

您可以使用 Filebeat RollingFileAppenders 编写您的应用程序级日志 将以下内容添加到您的 logback.xml 以将应用程序日志写入目录:

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/etc/logging/sample.log</file>
        <immediateFlush>false</immediateFlush>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>
                /etc/logging/hsvt_akka.%d{yyyy-MM-dd}.%i.log.zip
            </fileNamePattern>
            <maxFileSize>5KB</maxFileSize>
            <maxHistory>20</maxHistory>
            <totalSizeCap>1MB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>[%date{ISO8601}] [%level] [%logger] [%marker] [%thread] - %msg MDC: {%mdc}%n</pattern>
        </encoder>
    </appender>

现在配置 filebeat 以读取这些写入目录 /etc/logging、运行 的日志,作为集群中 java 应用程序的 sidecar,例如 k8s

为 filebeat 创建 configMap:

filebeat.yml: |
    filebeat:
      inputs:
      - type: filestream
        enabled: true
        paths:
          - /etc/logging/*.log
          - /etc/logging/*.log.*
        prospector:
          scanner:
            check_interval: 1s

      config:
        inputs:
          path: /etc/filebeat.yml
          reload:
            enabled: true
            period: 10s
    output:
      elasticsearch:
        hosts: []
        protocol: 
        username: 
        password: 

现在将其作为 sidecar 容器添加到您的原始 java 应用中。

- name: filebeat-sidecar
          image: elastic/filebeat:7.16.3
          command: ["filebeat"]
          args: ["-c","/etc/filebeat.yml","-e", "-strict.perms=false"]
          resources:
            limits:
              memory: "80Mi"
            requests:
              memory: "60Mi"
          securityContext:
            runAsUser: 2000
          volumeMounts:
            - name: filebeat-app-logs
              mountPath: /etc/logging
            - name: filebeat-config
              mountPath: /etc/filebeat.yml
              subPath: filebeat.yml
      volumes:
        - name: filebeat-app-logs
        - name: filebeat-config
          configMap:
            defaultMode: 0600
            name: filebeat-configmap
            items:
              - key: filebeat.yml
                path: filebeat.yml

您需要配置 java 应用程序和 filebeat 以读取写入日志的同一卷