php擴充SeasLog應用於 yii2 組件

來源:互聯網
上載者:User

標籤:ges   添加   命名   tab   開發   target   user   exist   程式   

一.seaslog 簡單介紹及使用原因

  它是C 寫的PHP擴充,效能很高,使用簡單,能滿足大部分簡單的日誌需求。(個人感覺)

     其他優勢請看--》https://github.com/Neeke/SeasLog, http://www.oschina.net/p/seaslog , 

缺點:(使用時碰到的坑)

    1.如果配置有差錯會直接報錯,程式不健壯會導致頁面直接掛掉,而且由於C寫的這種意外報錯你很難處理。(建議基本路徑和模組路徑一定要寫對;環境變更時 也要時刻注意)

    2. cli模式和CGL 模式下的讀寫混合的話,會報錯,你注意區分下執行使用者,即使記錄檔是 777,也要區分 (正常坑)

 

二.安裝與裝置(比較簡單,稍提一下)

  1.下載安裝包,https://github.com/Neeke/SeasLog,

      2. 解壓並進入目錄,執行phpize,$/path/to/phpize

      3.$ ./configure --with-php-config=/path/to/php-config

      4.make && make install

  (如有不清楚  http://www.oschina.net/p/seaslog ,https://github.com/Neeke/SeasLog,或者 部落格下面留言)

三.因為公司使用的YII2 架構,我想要把這個日誌系統加入使用,並且可配置化,無論是加入還是移除都比較方便;其次我想要他自動補全,那樣開發起來效率高一些。

  1.首先yii2 中的設定檔web.php 總加入日誌類的指向:

   說明: 我把我寫的seaslog類放在base層,並且配置日誌目錄,架構的runtime 目錄

return [    // ...    ‘components‘ => [        ‘authManager‘ => [            ‘class‘    => ‘app\mvc\_base\srv\Seaslog‘,
       ‘basePath‘ => dirname(dirname(__DIR__)) . ‘/runtime/seaslog‘
// ‘logPath‘ => ‘default‘
], // ... ],];

    2. 封裝日誌類

      思想:為了配合的方便與查看的清晰,我的每個記錄檔與其命名空間對應,這樣記錄檔對應的產生日誌的地方將一目瞭然(但是進入目錄時有點小麻煩);繼承yii2 的 object組件,以及修改部分架構的代碼以達到自動補全功能;

  

class SeasLog extends Object{    public $basePath ;    //日誌存放的目錄    public $loggerPath ;  // 日誌模組下面分模組    public function __construct()    {        parent::__construct();       //todo  初始化工作    }    /**     * @desc  通用記錄日誌的 方法,可以順位     * @Author  FredGui     * @param $message     * @param string $loggerPath   預設是default, 一般使用調用者的命名空間 目錄,php.ini 配置裡面 是 defaultLog     * @param $level  總共8個 層級 ,暫且用三個,SEASLOG_INFO,SEASLOG_WARNING,SEASLOG_ERROR,     *  SEASLOG_DEBUG,SEASLOG_INFO,SEASLOG_NOTICE,SEASLOG_WARNING,SEASLOG_ERROR,SEASLOG_CRITICAL,SEASLOG_ALERT,SEASLOG_EMERGENCY     */    public function log($message,$loggerPath = ‘default‘ ,$level = SEASLOG_WARNING ){        try{            $message = (is_array($message) || is_object($message)) ? json_encode($message,JSON_UNESCAPED_UNICODE) : $message;            if(file_exists($this -> basePath ) && is_writable($this -> basePath ) ) {                $this -> setBasePath($this -> basePath);  // 設定 日誌目錄                $this -> setLogger($loggerPath);  // moming模組名 即為 命名空間                \SeasLog::log($level, $message, array() ,‘‘);            }else{                // 預設 /var/log/www   日誌目錄                // 預設 defaultLog    存放記錄檔                \SeasLog::log($level, $message, array() ,‘‘);                throw new Exception(‘日誌路徑沒有讀寫權限!!‘,100001);            }        }catch (Exception $e){            // 出現錯誤異常記錄下,存放在 日誌路徑下的 seaslogError.log            $errorPath = realpath(\Yii::$app->basePath . ‘/../runtime/seaslog‘);            // 目錄問題             if(!is_dir($errorPath)){                @mkdir($errorPath, 0777, true);                chmod($errorPath, 0777);                if (!is_dir($errorPath)) exit(‘no write permission‘);            }            file_put_contents($errorPath .‘/seaslogError.log‘ , ‘time:‘.date(‘Y-m-d H:i:s‘).‘,‘.$e->getMessage() . ‘錯誤碼:‘ .$e->getCode() ,FILE_APPEND);        }    }    /**     * 記錄info日誌     * @param $message     * @param string $loggerPath     */     public function info($message, $loggerPath = ‘default‘)    {        $this -> log($message,$loggerPath,SEASLOG_INFO);    }
.....(後續的設定多等級,可以看官網)

    

由此可以得到 如下的日誌目錄:

進入查看:

 

 最後,我們來完善下,自動補全,像這樣

 

 在  path\vendor\yiisoft\yii2\Yii.php 總的$app 總 添加

/**
* @var \yii\base\BaseApplication|\yii\web\WebApplication|\yii\console\ConsoleApplication the application instance
*/
public static $app;

然後 產生對應的 檔案到目錄 ,空檔案就行
比如:BaseApplication.php
<?php/** * Created by PhpStorm. * User: FredGui * Date: 2016/06/12 * Time: 14:17 */namespace yii\base;/** * Class BaseApplication * Used for properties that are identical for both WebApplication and ConsoleApplication * * @property \app\mvc\_base\srv\SeasLog $seaslog The auth manager for this application. Null is returned if auth manager is not configured. This property is read-only. Extended component. */abstract class BaseApplication extends Application{}
WebApplication.php 和 ConsoleApplication 也是如此。

至此 yii2 中就可以方便的使用seaslog組件了~~~~~

 

php擴充SeasLog應用於 yii2 組件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.