写入日志
若有特殊写入需求,可以扩展本方法(比如调用数据库类克写到数据库里)
boolean Core_Core::write_log( string $data [, string $type = string(3) "log" , stirng $file = null ] )
参数列表
参数 类型 描述 默认值 $data
string
$data $type
string
日志类型 string(3) "log" $file
stirng
指定文件名,不指定则默认 null
boolean
protected static function write_log($data, $type = 'log', $file = null)
{
static $pro = null;
if (!$type)$type = 'log';
if (null===$pro)
{
if (preg_match('#^(db|cache)://([a-z0-9_]+)/([a-z0-9_]+)$#i', DIR_LOG , $m))
{
$pro = $m;
}
else
{
$pro = false;
}
}
# Log目录采用文件目录
if (false===$pro)
{
$write_mode = Core::config('core.file_write_mode');
# 禁用写入
if ($write_mode=='disable')return true;
# 再判断是否有转换储存处理
if (preg_match('#^(db|cache)://([a-z0-9_]+)/([a-z0-9_]+)$#i', $write_mode , $m))
{
$pro = $m;
}
}
if (false===$pro)
{
# 以文件的形式保存
$log_config = Core::config('log');
if (!$file)
{
if ($log_config['file'])
{
$file = date($log_config['file']);
}
else
{
$file = date('Y/m/d/');
}
$file .= $type . '.log';
}
$dir = trim(dirname($file), '/');
# 如果目录不存在,则创建
if (!is_dir(DIR_LOG.$dir))
{
$temp = explode('/', str_replace('\\', '/', $dir) );
$cur_dir = '';
for($i=0; $i<count($temp); $i++)
{
$cur_dir .= $temp[$i] . "/";
if (!is_dir(DIR_LOG.$cur_dir))
{
@mkdir(DIR_LOG.$cur_dir, 0755);
}
}
}
return false===@file_put_contents(DIR_LOG . $file, $data . CRLF , FILE_APPEND)?false:true;
}
else
{
# 以db或cache方式保存
if ($pro[1]=='db')
{
$db_data = array
(
'key' => md5($file),
'key_str'=> substr($file, 0, 255),
'type' => $type,
'day' => date('Ymd'),
'time' => TIME,
'value' => $data,
);
$obj = new Database($pro[2]);
$status = $obj->insert($pro[3], $db_data) ? true:false;
}
else
{
if ($pro[3])
{
$pro[1]['prefix'] = trim($pro[3]) . '_';
}
$pro[1]['prefix'] .= $type.'_';
$obj = new Cache($pro[2]);
$status = $obj->set(date('Ymd').'_'.md5($file), $data, 86400*30); // 存1月
}
return $status;
}
}