Example #1
0
 /**
  * 写入日志
  *
  * @param string $tag 日志类型
  * @param string $data
  * @return boolean
  */
 protected static function write_log($tag = 'log', $data)
 {
     /**
      * 按项目记录每个项目的日志保存方式
      */
     static $st = array();
     if (!isset($st[Core::$project])) {
         # 读取项目日志保存方式
         if (preg_match('#^(db|cache|fd)://([a-z0-9_]+)/([a-z0-9_]+)?$#i', DIR_LOG, $m)) {
             if ($m[1] === 'fd') {
                 $m[0] = 'tcp://' . $m[2] . ':' . $m[3];
             }
             $st[Core::$project] = array('link' => $m[0], 'type' => $m[1], 'host' => $m[2], 'prefix' => $m[3]);
         } else {
             $log_config = Core::config('log');
             if (isset($log_config['fluent']) && $log_config['fluent']) {
                 $st[Core::$project] = array('link' => $log_config['fluent'], 'type' => 'fd', 'host' => $m[2], 'prefix' => $m[3]);
             } else {
                 $write_mode = Core::config('file_write_mode');
                 # 禁用写入
                 if ($write_mode === 'disable') {
                     return true;
                 }
                 # 判断是否有转换储存处理
                 if (preg_match('#^(db|cache)://([a-z0-9_]+)/([a-z0-9_]+)$#i', $write_mode, $m)) {
                     $st[Core::$project] = array('link' => $m[0], 'type' => $m[1], 'host' => $m[2], 'prefix' => $m[3]);
                 } else {
                     $st[Core::$project] = false;
                 }
             }
         }
     }
     if (!$tag) {
         $tag = 'log';
     }
     $pro = $st[Core::$project];
     if (false === $pro) {
         # 以文件的形式保存
         if ($log_config['file']) {
             $file = date($log_config['file']);
         } else {
             $file = date('Y/m/d/');
         }
         $file .= $tag . '.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, (defined('JSON_UNESCAPED_UNICODE') ? json_encode($data, JSON_UNESCAPED_UNICODE) : json_encode($data)) . CRLF, FILE_APPEND) ? false : true;
     } else {
         # 以db或cache或fd方式保存
         if ($pro['type'] === 'fd') {
             return Fluent::instance($pro['link'])->push($tag, $data);
         } elseif ($pro['type'] === 'db') {
             $obj = new Database($pro['host']);
             $db_data = array('type' => $tag, 'day' => (int) date('Ymd'), 'time' => TIME_FLOAT, 'value' => $obj->is_support_object_value() ? $data : Core::json_encode($data));
             $status = $obj->insert($pro['prefix'], $db_data) ? true : false;
         } else {
             $obj = Cache::instance($pro['host']);
             if ($pro['prefix']) {
                 $obj->set_prefix($pro['prefix']);
             }
             $status = $obj->set(date('Ymd') . '_' . $tag, $data, 86400 * 30);
             // 存1月
         }
         return $status;
     }
 }