Discuz!运行日志记录函数runlog详解介绍
在discuz!的\data\log\目录下,我们会看到很多格式如YYYYMM_XX.php的文件(如下图所示),打开文件我们就会看到很多操作日志,这些日志怎么生成的呢,今天我们就来看看Discuz!运行日志记录runlog函数runlog函数定义文件 \source\function\function_core.php
function runlog($file, $message, $halt=0) {
helper_log::runlog($file, $message, $halt);
}我们看到这个函数是调用的helper_log小助手,helper_log定义于:\source\class\helper\helper_log.php相关方法如下
public static function runlog($file, $message, $halt=0) {
global $_G;
$nowurl = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:($_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']);
$log = dgmdate($_G['timestamp'], 'Y-m-d H:i:s')."\t".$_G['clientip']."\t$_G\t{$nowurl}\t".str_replace(array("\r", "\n"), array(' ', ' '), trim($message))."\n";
helper_log::writelog($file, $log);//写入文件
if($halt) {
exit();
}
}
public static function writelog($file, $log) {
global $_G;
$yearmonth = dgmdate(TIMESTAMP, 'Ym', $_G['setting']['timeoffset']);
$logdir = DISCUZ_ROOT.'./data/log/';
$logfile = $logdir.$yearmonth.'_'.$file.'.php';//日志文件
if(@filesize($logfile) > 2048000) {
$dir = opendir($logdir);
$length = strlen($file);
$maxid = $id = 0;
while($entry = readdir($dir)) {
if(strpos($entry, $yearmonth.'_'.$file) !== false) {
$id = intval(substr($entry, $length + 8, -4));
$id > $maxid && $maxid = $id;
}
}
closedir($dir);
$logfilebak = $logdir.$yearmonth.'_'.$file.'_'.($maxid + 1).'.php';
@rename($logfile, $logfilebak);
}
if($fp = @fopen($logfile, 'a')) {
@flock($fp, 2);
if(!is_array($log)) {
$log = array($log);
}
foreach($log as $tmp) {
fwrite($fp, "<?PHP exit;?>\t".str_replace(array('<?', '?>'), '', $tmp)."\n");
}
fclose($fp);
}
}我看可以看到运行日志按月存储在各个分类(日志的分类大致有'illegal', 'rate', 'credit', 'mods', 'medal', 'ban', 'cp', 'magic', 'error', 'invite', 'payment', 'warn', 'crime', 'sendmail'几种)的文件下,每条记录的不同字段以制表符\t分隔;
这样就可以了
页:
[1]