php扩展SeasLog应用于 yii2 组件

一.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'    => 'appmvc\_basesrvSeaslog',
       '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);
    }
.....(后续的设置多等级,可以看官网)
复制代码

    

由此可以得到 如下的日志目录:

进入查看:

 最后,我们来完善下,自动补全,像这样

 在  pathvendoryiisoftyii2Yii.php 总的$app 总 添加

/**
* @var yiiaseBaseApplication|yiiwebWebApplication|yiiconsoleConsoleApplication the application instance
*/
public static $app;

然后 生成对应的 文件到目录 ,空文件就行
比如:BaseApplication.php
复制代码
<?php
/**
 * Created by PhpStorm.
 * User: FredGui
 * Date: 2016/06/12
 * Time: 14:17
 */

namespace yiiase;


/**
 * Class BaseApplication
 * Used for properties that are identical for both WebApplication and ConsoleApplication
 *
 * @property appmvc\_basesrvSeasLog $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组件了~~~~~
原文地址:https://www.cnblogs.com/zhengxingpeng/p/6679224.html