Log::write PHP Method

write() static public method

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

Usage Example

Example #1
0
 public function index()
 {
     if ($this->config->get('amazon_status') != '1') {
         return;
     }
     $this->load->model('openbay/amazon_product');
     $logger = new Log('amazon.log');
     $logger->write('amazon/listing_reports - started');
     $token = $this->config->get('openbay_amazon_token');
     $incomingToken = isset($this->request->post['token']) ? $this->request->post['token'] : '';
     if ($incomingToken !== $token) {
         $logger->write('amazon/listing_reports - Incorrect token: ' . $incomingToken);
         return;
     }
     $decrypted = $this->openbay->amazon->decryptArgs($this->request->post['data']);
     if (!$decrypted) {
         $logger->write('amazon/listing_reports - Failed to decrypt data');
         return;
     }
     $logger->write('Received Listing Report: ' . $decrypted);
     $request = json_decode($decrypted, 1);
     $data = array();
     foreach ($request['products'] as $product) {
         $data[] = array('marketplace' => $request['marketplace'], 'sku' => $product['sku'], 'quantity' => $product['quantity'], 'asin' => $product['asin'], 'price' => $product['price']);
     }
     if ($data) {
         $this->model_openbay_amazon_product->addListingReport($data);
     }
     $this->model_openbay_amazon_product->removeListingReportLock($request['marketplace']);
     $logger->write('amazon/listing_reports - Finished');
 }
All Usage Examples Of Log::write