OpenShift聚合日志记录:解析Apache访问日志

Nik*_*ams 6 fluentd openshift-origin

使用OpenShift聚合日志记录时,我将日志很好地输入到elasticsearch中.但是,apache记录的行最终会出现在message字段中.

我想在Kibana中创建查询,我可以单独访问URL,状态代码和其他字段.为此,需要完成特殊的apache访问日志解析.

我怎样才能做到这一点?

这是kibana中的示例条目:

{
  "_index": "42-steinbruchsteiner-staging.3af0bedd-eebc-11e6-af4b-005056a62fa6.2017.03.29",
  "_type": "fluentd",
  "_id": "AVsY3aSK190OXhxv4GIF",
  "_score": null,
  "_source": {
    "time": "2017-03-29T07:00:25.595959397Z",
    "docker_container_id": "9f4fa85a626d2f5197f0028c05e8e42271db7a4c674cc145204b67b6578f3378",
    "kubernetes_namespace_name": "42-steinbruchsteiner-staging",
    "kubernetes_pod_id": "56c61b65-0b0e-11e7-82e9-005056a62fa6",
    "kubernetes_pod_name": "php-app-3-weice",
    "kubernetes_container_name": "php-app",
    "kubernetes_labels_deployment": "php-app-3",
    "kubernetes_labels_deploymentconfig": "php-app",
    "kubernetes_labels_name": "php-app",
    "kubernetes_host": "itsrv1564.esrv.local",
    "kubernetes_namespace_id": "3af0bedd-eebc-11e6-af4b-005056a62fa6",
    "hostname": "itsrv1564.esrv.local",
    "message": "10.1.3.1 - - [29/Mar/2017:01:59:21 +0200] "GET /kwf/status/health HTTP/1.1" 200 2 "-" "Go-http-client/1.1"\n",
    "version": "1.3.0"
  },
  "fields": {
    "time": [
      1490770825595
    ]
  },
  "sort": [
    1490770825595
  ]
}
Run Code Online (Sandbox Code Playgroud)

so-*_*ude 0

免责声明:我没有在 openshift 中对此进行测试。我不知道您的微服务使用哪种技术堆栈。

这就是我在 Kubernetes 中部署的 Spring Boot 应用程序(带有 logback)中执行此操作的方法。

1.使用logstash编码器进行logback(这将以Json格式写入日志,这对ELK堆栈更友好)

我有一个 gradle 依赖项来启用此功能

compile "net.logstash.logback:logstash-logback-encoder:3.5"
Run Code Online (Sandbox Code Playgroud)

然后在 logback-spring.groovy/logback-spring.xml (或 logabck.xml)中将 LogstashEncoder 配置为附加程序中的编码器

2.有一些过滤器或库来写入访问日志

对于 2. 要么使用

A.使用“net.rakugakibox.springbootext:spring-boot-ext-logback-access:1.6”库

(这是我正在使用的)

它给出了一个很好的 json 格式,如下

{  
   "@timestamp":"2017-03-29T09:43:09.536-05:00",
   "@version":1,
   "@message":"0:0:0:0:0:0:0:1 - - [2017-03-29T09:43:09.536-05:00] \"GET /orders/v1/items/42 HTTP/1.1\" 200 991",
   "@fields.method":"GET",
   "@fields.protocol":"HTTP/1.1",
   "@fields.status_code":200,
   "@fields.requested_url":"GET /orders/v1/items/42 HTTP/1.1",
   "@fields.requested_uri":"/orders/v1/items/42",
   "@fields.remote_host":"0:0:0:0:0:0:0:1",
   "@fields.HOSTNAME":"0:0:0:0:0:0:0:1",
   "@fields.content_length":991,
   "@fields.elapsed_time":48,
   "HOSTNAME":"ABCD"
}
Run Code Online (Sandbox Code Playgroud)

或者

B.使用 Logback 的Tee 过滤器

或者

C. Spring 的 CommonsRequestLoggingFilter (没有真正测试过)

添加 bean 定义

    @Bean
    public CommonsRequestLoggingFilter requestLoggingFilter() {
        CommonsRequestLoggingFilter crlf = new CommonsRequestLoggingFilter();
        crlf.setIncludeClientInfo(true);
        crlf.setIncludeQueryString(true);
        crlf.setIncludePayload(true);
        return crlf;
    }
Run Code Online (Sandbox Code Playgroud)

然后将 org.springframework.web.filter.CommonsRequestLoggingFilter 设置为 DEBUG,这可以通过添加以下内容来完成application.properties

logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG
Run Code Online (Sandbox Code Playgroud)