yii1框架定时清理自定义日志文件的脚本
系统运行生成了一些日志文件,需要定期清理掉,以下是一个代码参考,用到了exec函数执行shell命令完成日志打包和删除:
日志文件处理、清理说明:
- 每月执行一次,可用crontab执行protected/yii logFile run [--test=0|1]
- 处理 protected/runtime 目录下的日志文件,将其移动到 protected/runtime/bak 目录下,打包成zip文件,并删除原文件
- 删除 protected/runtime/bak 目录下超过 180 天的zip文件
BaseConsoleCommand.php
<?php
class BaseConsoleCommand extends CConsoleCommand {
protected $debugEnable = false;
protected function msg($message, $exitCode=null) {
echo date('Y-m-d H:i:s') . ": {$message}\n";
if (!is_null($exitCode)) return exit($exitCode);
}
protected function cliOut($message) {
if (is_array($message)) $message = json_encode($message, JSON_UNESCAPED_UNICODE);
echo "{$message}\n";
}
}
LogfileCommand.php
<?php
require_once __DIR__ . '/BaseConsoleCommand.php';
class LogfileCommand extends BaseConsoleCommand
{
private $debug = false;
private $logPath, $logBackPath;
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
//日志文件目录路径,以/结尾
$this->logPath = Yii::app()->basePath . '/runtime/';
//日志文件备份目录路径,以/结尾
$this->logBackPath = Yii::app()->basePath . '/runtime/bak/';
}
/**
* 日志文件处理、清理
* 每月执行一次
* 处理 protected/runtime 目录下的日志文件,将其移动到 protected/runtime/bak 目录下,打包成zip文件,并删除原文件
* 删除 protected/runtime/bak 目录下超过 180 天的zip文件
* 通过调用shell命令完成操作
* 不提供手动定义文件名的参数,以免对系统文件造成误操作
* @param int $test 测试模式,0为正式执行,1为测试执行
* @return void
*/
public function actionRun($test = 0)
{
$this->debug = boolval($test);
if (!$this->checkShellRun()) return;
$prevMonth = date('Ym', strtotime('-1 month'));
$date = date('Ymd');
/**
* 待处理日志文件列表,格式:[文件名=>zip文件名,文件名=>zip文件名,...],文件名可以用*代替多个任意字符,参考shell语法
* 如果不想备份直接删除文件,则把zip文件名设置为 NULL 空值
*/
$logFileList = [
'role-assign.log' => "role-assign-{$date}.log.zip",
'caijicluemeituan.log' => "caijicluemeituan-{$date}.log.zip",
'supplies.log' => "supplies-{$date}.log.zip",
'log.txt' => "log-{$date}.txt.zip",
'task_oceanengine.log' => "task_oceanengine-{$date}.log.zip",
'cluetyd_assign.log' => "cluetyd_assign-{$date}.log.zip",
'wxwork_callback.log' => "wxwork_callback-{$date}.log.zip",
"site_all_log{$prevMonth}*.log" => "site_all_log{$prevMonth}-{$date}.log.zip",
];
//循环处理待处理日志文件列表
foreach ($logFileList as $filename => $zipFileName) {
$this->msg("处理待处理日志文件:{$filename}");
$logFile = $this->logPath . $filename;
//执行shell命令移动文件,并压缩
$zipFile = $this->logBackPath . $zipFileName;
if (is_null($zipFileName)) {
//直接删除原文件
$cmd = "rm -f {$logFile}";
} else {
//移动文件到备份目录,然后压缩目标文件后删除目标文件
$logFileMoved = $this->logBackPath . $filename;
$cmd = "mv {$logFile} {$this->logBackPath} && zip -r {$zipFile} {$logFileMoved} && rm -f {$logFileMoved}";
}
$this->exec($cmd);
echo "\n";
}
/**
* 删除 protected/runtime/bak 目录下超过 180 天的zip文件
*/
$this->msg("删除 protected/runtime/bak 目录下超过 180 天的zip文件");
$cmd = "find {$this->logBackPath} -mtime +180 -name '*.zip' -delete";
$this->exec($cmd);
}
/**
* 检查shell命令是否可用
* @return bool
*/
private function checkShellRun()
{
//判断shell_exec 或者 exec 是否可用
if (!function_exists('shell_exec') && !function_exists('exec')) {
$this->msg('需要取消禁用 shell_exec 或者 exec 函数,请检查php配置!');
return false;
}
return true;
}
/**
* 执行shell命令
* @param $cmd
* @return array [code, ret] 执行结果 code:0=成功,1=失败
*/
private function exec($cmd)
{
$this->msg("执行shell命令:{$cmd}");
$code = 0;
if ($this->debug) {
//测试
$ret = '测试模式,未真实执行命令'; //shell_exec($cmd);
} else {
//判断使用哪个函数执行命令
if (function_exists('exec')) {
exec($cmd, $ret, $code);
$ret = implode("\n", $ret);
} elseif (function_exists('shell_exec')) {
$ret = shell_exec($cmd);
$code = $ret === false ? 1 : 0;
} else {
$ret = 'shell_exec 或者 exec 函数不可用,请检查php配置!';
$code = 1;
}
}
$this->msg("执行结果:code: {$code}; ret:{$ret}");
return [$code, $ret];
}
}
版权属于:Joyber
本文链接:https://blog.qqvbc.com/default/1144.html
转载时须注明出处及本声明