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"/>