logstash抽取日志文件数据到ES中

input {
  stdin {
 }
 file{
    #扫描路径下特定名称的日志文件
    #path => "/home/elastic/logs/a.log"
    #扫描路径下的.log结尾的所有日志文件
    path => "/home/elastic/logs/*.log"
    #类型名称
    type=>"log"
    #若日志为多行信息显示,需要codec配置
    codec => multiline{
        # 正则表达式,匹配开头为 "[" 的为一条日志的开始
        pattern => "^["
        negate => true
        # 设置未匹配的内容是向前合并还是向后合并,previous, next 两个值选择,必选
        what => "previous"
    }
    start_position=>"beginning"
 }
}

# filter为logstash的解析日志模块
filter{
    if [type] == "log" {
        # 解析日志生成相关的IP,访问地址,日志级别
        grok {
            match => { 
              "message" => "%{SYSLOG5424SD:time} %{IP:hostip} %{URIPATHPARAM:url}s*%{LOGLEVEL:loglevel}" 
                 }
        }
        # 解析log生成的时间为时间戳
        grok{
            match => {
              "message" => "%{TIMESTAMP_ISO8601:log_create_date}"
                 }
        }
        # 替换插入信息的时间戳为日志生成的时间戳
        date {
            match => ["log_create_date", "yyyy-MM-dd HH:mm:ss" ]
            target => "@timestamp"             
        }
    }
}

#定义日志输出的配置,此处为写入解析结果到es集群
output {
 stdout {
  codec => json_lines
 }
 if [type] == "log" {
     elasticsearch {
      hosts => "localhost:9200"
      index => "log"
      user => elastic
      password => elastic
     }
 }
}
原文地址:https://www.cnblogs.com/wueryuan/p/14179154.html