freeswitch-初级-01-日志分割

1、前言

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

博主之前一直为它而头疼

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

2、日志分割方式

2.1 mod_logfile

mod_logfile 是 fs自带日志系统

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

bash 复制代码
./fs_cli -x "reload mod_logfile"

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

log 复制代码
<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 ,然后轻松得到日志分割情况

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

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

    shell 复制代码
    # 这是我本地的日志分割情况
    -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 测试后,确实也能某种程度的 按时间进行分割日志

bash 复制代码
# 每天 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 文件

bash 复制代码
/data/freeswitch/log/freeswitch-*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
}

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

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

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

log 复制代码
# 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 不满足条件去触发

xml 复制代码
  <param name="rollover" value="5120000000"/>
  <param name="maximum-rotate" value="30"/>
相关推荐
知守观1 分钟前
Snowflake 雪花算法实战:41位时间戳里的三个坑(时钟回拨、workerId、位运算)
后端
花间相见38 分钟前
【计算基础|网络04】—— HTTP接口实战(下):接口测试、鉴权与跨域排错
java·linux·人工智能·后端·python·计算机网络·postman
BryceBorder39 分钟前
Agent Memory 不只是聊天记录:手搓三大记忆系统
后端·agent·面经·codex·claude code·agent memory
一条泥憨鱼1 小时前
苍穹外卖【day11| 用户统计,订单统计,销量排名统计功能实现】
java·后端·苍穹外卖
她的男孩1 小时前
缓存明明命中了却报 ClassCastException:拆完多级缓存控制面,我挖出 5 个静默失效的坑
java·后端·架构
孙启超1 小时前
【AI开发之Rust】第 13 课:async/await 与 tokio 异步运行时
开发语言·后端·rust
SimonKing1 小时前
SpringBoot 集成 SSE 实现服务端推送或可代替Websocket
java·后端·程序员
字节探索1 小时前
Redis 只会当缓存用?这 10 大实战场景,让你的系统快到飞起
redis·后端
Sam_Deep_Thinking1 小时前
关于java final关键字的可见性
java·后端·面试·程序员