yaf thrift连接scribe


由于yaf寻找文件时加载类文件时会使用下划线分割 作为文件夹去寻找文件,旧版本的php接口文件不支持名字空间,因此手工做了下修改
查找相关文章:yaf thrift scribe

具体内容已经方法哦附件里面了


http://www.dayanmei.com/download.php?filename=yaf_scribe_thrift.zip


用法    

<?php
/**
 * 使用示例
 * $scribe = ScribeLog::get_instance();
 * $scribe->log('error',['名称','数据']);
 */

use Thrift\Base\TBase;
use Thrift\ClassLoader\ThriftClassLoader;
use Thrift\Transport\TSocket;
use Thrift\Transport\TFramedTransport;
use Thrift\Protocol\TBinaryProtocol;
use Scribe\LogEntry;
use Scribe\scribeClient;
use Scribe\ResultCode;

class ScribeLog {

    //单例模式
    private static $instance    = NULL;

    private $transport = null;

    /**
     * @var scribeClient
     */
    private $client = null;

    private $log_num = 0;

    //初始时间
    private $begin_time = 0;

    //事件开始时间
    private $event_begin_time = 0;

    //event事件结束时间
    private $event_end_time = 0;

    private $host = null;
    private $port = null;
    private $separator = '';
    private $prefix = '';

    /**
     * 构造函数
     */
    protected function __construct(){
        $config = Yaf\Registry::get('config');
        if(!is_array($config) || !isset($config['scribe'])){
            return;
        }
        $this->host = isset($config['scribe']['host']) ? $config['scribe']['host'] : 'scribe';
        $this->port = isset($config['scribe']['port']) ? $config['scribe']['port'] : 1463;
        $this->separator = isset($config['scribe']['separator']) ? $config['scribe']['separator'] : '';
        $this->prefix = isset($config['scribe']['prefix']) ? $config['scribe']['prefix'] : '';

        $this->begin_time = $this->getMicroTime();

        $this->open($this->host,$this->port);
    }

    /**
     * 单例模式
     */
    public static function get_instance(){
        if(!self::$instance instanceof self){
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 获取微秒数
     * @return mixed
     */
    private function getMicroTime(){
        return microtime(true)*1000;
    }

    /**
     * 开始计时
     */
    public function start(){
        $this->event_begin_time = $this->getMicroTime();
    }

    /**
     * 结束计时
     */
    public function end(){
        $this->event_end_time = $this->getMicroTime();
    }

    public function log($category, $message, $separator = null){
        if($this->transport == null) {
            return false;
        }
        if($separator!=null) {
            $this->separator = $separator;
        }
        if(is_array($message)) {
            $message = implode($this->separator, $message);
        }

        if($this->prefix != '' && substr($category,0,strlen($this->prefix)) != $this->prefix){
            $category = $this->prefix.$category;
        }

        try{
            $entry = new LogEntry;
            $entry->category = $category;
            $entry->message = $message;
            $messages = array($entry);
            $result = $this->client->Log($messages);
            if($result == ResultCode::OK) {
                $this->log_num++;
                return true;
            }else{
                return false;
            }
        }catch(Exception $e){
            return false;
        }
    }

    private function open($host, $port){
        try{
            $socket = new TSocket($host, $port, true);
            $socket->setSendTimeout(10);
            $socket->setRecvTimeout(10);

            $this->transport = new TFramedTransport($socket);
            $protocol = new TBinaryProtocol($this->transport, false, false);
            $this->client = new scribeClient($protocol, $protocol);

            $this->transport->open();
        }catch(Exception $e){
            return false;
        }
        return true;
    }

    private function close() {
        if ($this->transport!=null) {
            $this->transport->close();
        }
    }

    function __destruct(){
        $this->close();
    }
}


  • 你可能对这些文章感兴趣