在CentOS中,Filebeat可以通過配置文件中的processors來進行日志過濾。以下是一些常用的日志過濾方法:
在Filebeat的配置文件(filebeat.yml)中,可以使用ignore_files
和ignore_dirs
選項來忽略特定的文件或目錄。例如:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
ignore_files: ['*.log.1', 'temp.log']
ignore_dirs: ['cache', 'logs']
這將忽略所有以.log.1
結尾的文件和名為temp.log
的文件,同時忽略cache
和logs
目錄。
如果需要包含特定的文件或目錄,可以使用include_files
和include_dirs
選項。例如:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
include_files: ['access.log']
include_dirs: ['logs']
這將只包含名為access.log
的文件和logs
目錄。
Filebeat支持使用processors對日志進行更復雜的過濾。例如,可以使用dissect
處理器解析日志,并根據解析結果進行過濾。以下是一個示例配置:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/myapp/*.log
processors:
- dissect:
tokenizer: '%{timestamp} %{level} %{message}'
field: 'message'
target_prefix: ''
when:
contains:
message: 'ERROR'
output.elasticsearch:
hosts: ["localhost:9200"]
這個配置將解析名為myapp
的目錄下的所有日志文件,并從中提取時間戳、日志級別和消息。然后,它將只輸出包含"ERROR"的消息。
注意:在使用processors時,需要確保已安裝并啟用了相應的數據處理器。在上面的示例中,需要安裝filebeat-module-discover-nginx
模塊以啟用dissect
處理器。
更多關于Filebeat日志過濾的信息,請參考官方文檔:https://www.elastic.co/guide/en/beats/filebeat/current/filtering-events.html