You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Logstash按小时生成带时间戳的apache.log文件配置咨询

配置Logstash Output File插件实现按小时生成指定格式日志

没问题,这就帮你搞定按小时生成apache-YYYY-MM-DD-HH:00.log格式日志的需求!你只需要在现有配置基础上添加对应的output段即可,完整配置示例如下:

完整配置代码

# INPUT HERE
input {
  beats {
    port => 5044
  }
}

# FILTER HERE
filter {
  if [source] == "/var/log/apache2/error.log" {
    mutate {
      remove_tag => [ "beats_input_codec_plain_applied" ]
      add_tag => [ "apache_logs" ]
    }
  }
  if [source] == "/var/log/apache2/access.log" {
    mutate {
      remove_tag => [ "beats_input_codec_plain_applied" ]
      add_tag => [ "apache_logs" ]
    }
  }
}

# OUTPUT HERE
output {
  # 仅处理标记为apache_logs的日志事件
  if "apache_logs" in [tags] {
    file {
      # 替换为你实际的输出目录,文件名按指定时间格式生成
      path => "/your/output/directory/apache-%Y-%m-%d-%H:00.log"
      # 使用line codec保证输出单行原始日志内容
      codec => line {
        format => "%{message}"
      }
      # 指定按时间策略切割文件
      rotation_strategy => "time"
      # 每3600秒(1小时)生成一个新文件
      time_rotation_interval => 3600
      # 每30秒刷新一次日志到文件,避免内存缓存堆积
      flush_interval => 30
      # 禁用按文件大小切割,确保仅按小时生成新文件
      file_chunk_size => 0
    }
  }
}

关键参数说明

  • path:通过strftime时间占位符实现动态文件名,%Y-%m-%d-%H会自动替换为日志事件@timestamp对应的年、月、日、小时,加上固定的:00就完全匹配你要的格式。记得把/your/output/directory/改成Logstash有写入权限的实际目录。
  • codec => line:确保输出内容是单行原始日志(直接取message字段),避免混入Logstash自带的元数据。
  • rotation_strategy => "time":强制按时间维度切割文件,而非默认的文件大小维度。
  • time_rotation_interval => 3600:明确设置每1小时生成一个新文件。
  • flush_interval => 30:缩短日志写入磁盘的间隔,减少内存中缓存的日志量。

额外小提示

如果需要把access.log和error.log分开生成独立文件,可以在filter段给它们分别添加apache_access和apache_error标签,然后在output中配置两个file插件,对应不同的文件名(比如apache-access-%Y-%m-%d-%H:00.log和apache-error-%Y-%m-%d-%H:00.log)即可。

内容的提问来源于stack exchange,提问作者user9303650

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:21:35