Log::trance PHP Method

trance() static public method

日志保存
static public trance ( $message, string $destination = '', $level = 'ERR', integer $type = 3, string $extra = '' ) : void
$destination string 写入目标
$type integer 日志记录方式
$extra string 额外参数
return void
    static function trance($message, $destination = '', $level = 'ERR', $type = 3, $extra = '')
    {
        $config_obj = Yaf_Registry::get("config");
        $log_config = $config_obj->log->toArray();
        $type = $type ? $type : 3;
        $log_config['record'] = isset($log_config['record']) ? $log_config['record'] : false;
        if (self::FILE == $type) {
            // 文件方式记录日志信息
            if (!is_dir(MYPATH . '/logs/')) {
                mkdir(MYPATH . '/logs/', 0777, true);
            }
            if (!is_dir(MYPATH . '/logs' . '/' . date('Ymd'))) {
                mkdir(MYPATH . '/logs' . '/' . date('Ymd'), 0777, true);
            }
            if (empty($destination)) {
                $destination = MYPATH . '/logs/' . date('Ymd') . '/' . date('y_m_d') . '.log';
            } else {
                $destination = MYPATH . '/logs/' . date('Ymd') . '/' . $destination;
            }
            //检测日志文件大小,超过配置大小则备份日志文件重新生成
            if (is_file($destination) && floor('2097152') <= filesize($destination)) {
                rename($destination, dirname($destination) . '/' . time() . '-' . basename($destination));
            }
        } else {
            $destination = $destination ? $destination : 'mubiao';
            $extra = $extra ? $extra : '额外信息';
        }
        $now = date(self::$format);
        self::append($destination, "{$now} {$level}: {$message}\r\n", $type);
        // 保存后清空日志缓存
        self::$log = array();
        //clearstatcache();
    }

Usage Example

Example #1
0
function logs($message, $destination = '', $level = 'DEBUG')
{
    Log::trance($message, $destination, $level);
}