zoukankan      html  css  js  c++  java
  • logstash自动根据预定义template生成elasticsearch日志索引相关问题

    0:版本
    Logstash:官方logstash:5镜像
    Elasticsearch:5.4.1

    1:配置logstash.conf
    Input plugin使用tcp,配置信息如下:

    input {
        tcp {
            port => 7777
            mode => server
        }
    }    

    2:日志记录组件使用了Nlog,定制logger的target:

    var target = new NetworkTarget
    {
        Layout = "${message}${newline}",
        Address = _targetAddress
    };
    NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, loggerLevel);
    logger = LogManager.GetLogger(loggerName);
    

      

    3:根据logger定义的Layout,通过logstash的filter模块解析日志对象json:

    filter {
        json {
            source => "message"
            remove_field => [ "message" ]
        }
    }    
    

    这里message字符串即为nlog发送过来的日志对象json,在解析后原json字符串不再保留(remove_field的作用)

    4:logstash的日志输出落地选择到elasticsearch,并且项目中的日志对象分了两类,因此logstash配置的output plugin如下:

    output {
        if [DocumentType] == "esUserSearchLog" {
            elasticsearch {
                hosts => ["192.168.30.91:9200","192.168.30.92:9200"]
                index => ["searchlog-%{+YYYY-MM}"]
                document_type => "%{DocumentType}"
                template => "/app/temp.searchlog.json"
                template_name => "searchlog-*"
                template_overwrite => true
            }
        }
        if [DocumentType] == "esUserDetailLog" {
            elasticsearch {
                hosts => ["192.168.30.91:9200","192.168.30.92:9200"]
                index => ["detaillog-%{+YYYY-MM}"]
                document_type => "%{DocumentType}"
                template => "/app/temp.detaillog.json"
                template_name => "detaillog-*"
                template_overwrite => true
            }
        }
    }        
    

    关注:

    1)DocumentType为日志对象的一个属性,用于指定索引至ES时的document_type;

    2)index配置为按月区分日志,需注意:索引名字必须对应template_name,否则将不会使用该处指定的template,此处配置的template_name为"searchlog-*",在新建所有以"searchlog-"开头的索引将使用该template;
    3)document_type指定为日志对象的DocumentType属性即可;
    4)template属性指定对应template文件路径(json格式,见后文)
    5)template_overwrite为true则template的order高的,满足同样条件(如均以searchlog-开头)的template将覆盖order低的;

    5:对应的template文件:

    temp.searchlog.json:
    {
        "template": "searchlog-*",
        "order": 1,
        "settings": {
            "number_of_replicas": "1",
            "number_of_shards": "3",
            "index": {
                "refresh_interval": "10s"
        }
    },
    ...
    "mappings": {
        ...
         }  
    }    
    

    关注:

    1)template指定的"searchlog-*"表示该template将应用于searchlog-开头的索引的创建,并与order参数一起决定哪个模板生效(同名称规则的order更大的模板生效);

    2)setting节点下refresh_interval参数表示数据自动刷新的频率(默认1s),由于日志文件实时性要求并不是特别高,因此这里可以酌情降低频率以提高索引的写入性能;

  • 相关阅读:
    Vue+element UI实现“回到顶部”按钮组件
    JS判断字符串长度的5个方法(区分中文和英文)
    从vue源码看Vue.set()和this.$set()
    mac下git安装与使用
    JS数组reduce()方法详解及高级技巧
    vue中router.go、router.push和router.replace的区别
    上传及更新代码到github(以及如何在vscode上提交自己的代码)
    VSCode打开多个项目文件夹的解决方法
    get请求和post请求的区别
    android 进程的优先级
  • 原文地址:https://www.cnblogs.com/you-you-111/p/9844131.html
Copyright © 2011-2022 走看看