input {
file {
# 指定日志文件的路径
path => "/path/to/your/java/application/logs/app.log"
# 只在文件有新内容时触发读取
start_position => "beginning"
# 读取文件时使用的字符编码
codec => "plain" { charset => "UTF-8" }
# 检测文件变化的时间间隔(秒)
sincedb_path => "/dev/null"
# 忽略旧数据
ignore_older => 0
}
}
filter {
使用 grok 插件来解析日志
grok {
match => { "message" => "%{TIMESTAMP_ISO8601:timestamp} [%{DATA:thread}] %{LOGLEVEL:level} %{DATA:logger} - %{GREEDYDATA:message}" }
}
可以添加其他过滤器,如 date、mutate 等
date {
match => ["timestamp", "ISO8601"]
target => "@timestamp"
}
转换日志级别为小写(可选)
mutate {
lowercase => ["level"]
}
}
output {
输出到 Elasticsearch
elasticsearch {
hosts => ["http://localhost:9200"]
index => "java-app-logs-%{+YYYY.MM.dd}"
document_type => "_doc"
# 如果 Elasticsearch 设置了用户名和密码
# user => "your_username"
# password => "your_password"
}
可以在控制台打印日志,用于调试
stdout {
codec => rubydebug
}
}