freeswitch-初级-01-日志分割

36 阅读4分钟

1、前言

你还在为fs日志太大而烦恼么,还在为fs不能按天分割而烦恼么

是不是期待日志这样

➜  logs ll
total 11M
drwxr-x--- 2 root root 4.0K Jan 23 17:34 cdr-csv
-rw------- 1 root root   13 Jan 24 21:32 freeswitch.history
-rw-r--r-- 1 root root 247K Jan 25 09:38 freeswitch_http.log
-rw-r--r-- 1 root root 1.4M Jan 23 00:00 freeswitch_http.log-2026-01-22
-rw-r--r-- 1 root root 651K Jan 24 00:00 freeswitch_http.log-2026-01-23
-rw-r--r-- 1 root root 829K Jan 25 00:00 freeswitch_http.log-2026-01-24
-rw-r--r-- 1 root root 321K Jan 25 09:38 freeswitch.log
-rw-r--r-- 1 root root 3.8M Jan 23 00:00 freeswitch.log-2026-01-22
-rw-r--r-- 1 root root 2.5M Jan 24 00:00 freeswitch.log-2026-01-23
-rw-r--r-- 1 root root 666K Jan 25 00:00 freeswitch.log-2026-01-24
-rw-r--r-- 1 root root 708K Jan 24 21:35 freeswitch.xml.fsxml
drwxr-x--- 2 root root 4.0K Jan 22 18:17 json_cdr

按天分割

博主之前一直为它而头疼

经过调研,总算是解决了这个心腹大患

2、日志分割方式

2.1 mod_logfile

mod_logfile 是 fs自带日志系统

可以通过 下面命令进行加载

./fs_cli -x "reload mod_logfile"

下面是 /home/freeswitch/etc/freeswitch/autoload_configs/logfile.conf.xml 的默认配置文件

<configuration name="logfile.conf" description="File Logging">
  <settings>
   <!-- true to auto rotate on HUP, false to open/close -->
   <param name="rotate-on-hup" value="true"/>
  </settings>
  <profiles>
    <profile name="default">
      <settings>
        <!-- File to log to -->
        <!--<param name="logfile" value="/var/log/freeswitch.log"/>-->
        <!-- At this length in bytes rotate the log file (0 for never) -->
        <param name="rollover" value="1048576000"/>
                <!-- Maximum number of log files to keep before wrapping -->
                <!-- If this parameter is enabled, the log filenames will not include a date stamp -->
                <param name="maximum-rotate" value="32"/>
        <!-- Prefix all log lines by the session's uuid  -->
        <param name="uuid" value="true" />
      </settings>
      <mappings>
        <!-- 
             name can be a file name, function name or 'all' 
             value is one or more of debug,info,notice,warning,err,crit,alert,all
             Please see comments in console.conf.xml for more information
        -->
        <map name="all" value="console,debug,info,notice,warning,err,crit,alert"/>
      </mappings>
    </profile>
  </profiles>
</configuration>

2.1.1 特点

mod_logfile 支持按照大小分割(不支持按时间驱动轮转),分割的日志默认是按照时间点来命名的

  • 验证: 可以通过 把 rollover 的值改成 5120 ,然后轻松得到日志分割情况

    # 修改了两个重要参数
    <param name="rollover" value="5120"/>
    <param name="maximum-rotate" value="3"/>
    

    然后测试得到下面的日志文件,发现没有保留 3个日志文件

    # 这是我本地的日志分割情况
    -rw-r--r-- 1 root root 1.2K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-17.8
    -rw-r--r-- 1 root root 1.1K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-17.9
    -rw-r--r-- 1 root root 1.1K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-18.1
    -rw-r--r-- 1 root root 1.1K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-18.2
    -rw-r--r-- 1 root root 1.1K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-18.3
    -rw-r--r-- 1 root root 1.1K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-25.1
    -rw-r--r-- 1 root root 1.1K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-25.2
    -rw-r--r-- 1 root root 1.1K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-25.3
    -rw-r--r-- 1 root root 1.1K Jan  8 15:59 freeswitch.log.2026-01-08-15-59-25.4
    ...
    

    一番调研才知道,如果是 滚动按照 freeswitch.log.1 freeswitch.log.2 freeswitch.log.3 这种,它不会生成 freeswitch.log.4,超过回自动覆盖最早的文件

根据上面特点,我们看起来做不到 按天分割日志了,但是 mod_logfile 切割日志除了上面的自动触发,我们还可以有办法帮它自动触发,比如

  • 自动重启fs
  • 或着不重启fs, 只需要定时 发送信号给FreeSWITCH,让其重新打开日志文件

我自己写了cron 测试后,确实也能某种程度的 按时间进行分割日志

# 每天 00:00 HUP FS,强制切文件
0 0 * * * kill -HUP $(pidof freeswitch)

2.1.2 特别注意

如果我们是按照日期来命名日志,那么 maximum-rotate 参数就无效了,因为不会周期性重复日志名!!!

需要我们使用到下面的方式 logrotate, 或者你自己用别的方式来清理过期日志

2.2 logrotate

logrotate 是 linux 系统自带日志系统

路径在 /etc/logrotate.d/,当我们需要处理fs 的日志文件,只需要在该路径下新增 freeswitch 文件

/data/freeswitch/log/freeswitch-*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
}

/data/freeswitch/log/ 是fs的日志路径,可以自行更改

mod_logfile 结合 定时任务,结合 logrotate 就可以管理日志

但是这个有没有太麻烦,其实 logrotate 本身就可以 管理fs日志,下面给出我本地的 logrotate fs日志配置

# FreeSWITCH 主日志
/home/fs/docker/logs/freeswitch.log {
    daily
    # 保留 7 天
    rotate 7          
    missingok
    notifempty
    compress
    delaycompress
    create 0640 root root
    
    # 关键:用日期做后缀
    dateext        
    # 文件名格式:-2026-01-08
    dateformat -%Y-%m-%d  
    # 保证后缀是 .log,否则会被截断
    extension .log   
    
    sharedscripts
    postrotate
        /usr/bin/pkill -SIGUSR1 freeswitch
    endscript
}

# FreeSWITCH HTTP 模块日志
/home/fs/docker/logs/freeswitch_http.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    delaycompress
    create 0640 root root
    
    # 关键:用日期做后缀
    dateext        
    # 文件名格式:-2026-01-08
    dateformat -%Y-%m-%d  
    # 保证后缀是 .log,否则会被截断
    extension .log   
    
    sharedscripts
    postrotate
        /usr/bin/pkill -SIGUSR1 freeswitch
    endscript
}

其中需要注意,需要 把 mod_logfile 里面配置改大些,让 mod_logfile 不满足条件去触发

  <param name="rollover" value="5120000000"/>
  <param name="maximum-rotate" value="30"/>

最后 立即手动执行一次logrotate(真正生效)

sudo logrotate -f /etc/logrotate.conf

3、最终配置

logfile.conf.xml 配置如下

<configuration name="logfile.conf" description="File Logging">
  <settings>
    <param name="rotate-on-hup" value="true"/>
  </settings>

  <profiles>
    <profile name="default">
      <settings>
        <!-- 固定文件名,绝不带日期 -->
        <param name="logfile"
               value="/home/freeswitch/var/log/freeswitch/freeswitch.log"/>

        <!-- 🔴 关键:彻底禁用 FS 内部轮转 -->
        <param name="rollover" value="0"/>
        <param name="maximum-rotate" value="0"/>
        <param name="max-file-size" value="0"/>

        <param name="uuid" value="true"/>
      </settings>

      <mappings>
        <map name="all"
             value="debug,info,notice,warning,err,crit,alert"/>
      </mappings>
    </profile>

    <profile name="http">
      <settings>
        <param name="logfile"
               value="/home/freeswitch/var/log/freeswitch/freeswitch_http.log"/>

        <!-- 同样禁用 -->
        <param name="rollover" value="0"/>
        <param name="maximum-rotate" value="0"/>
      </settings>

      <mappings>
        <map name="mod_http" value="info,notice,warning,err"/>
      </mappings>
    </profile>
  </profiles>
</configuration>

/etc/logrotate.d/freeswitch 配置如下

/home/fs/docker/logs/freeswitch.log 
/home/fs/docker/logs/freeswitch_http.log {
    daily
    rotate 30
    dateext
    dateformat -%Y-%m-%d

    # <--- 加上这一行,19号产生的日志重命名后就叫 19号
    dateyesterday

    missingok
    # 核心修改:使用 copytruncate
    copytruncate
    # 既然用了 copytruncate,就不再需要发送 HUP 信号了
    # postrotate
    #    docker kill -s HUP freeswitch
    # endscript
}

changlog

  • 2026-01-08 编辑文章,以为解决了
  • 2026-01-25 再次更新文章,运行了几天发现终于如预期了