`
xieye
  • 浏览: 803868 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

phalcon 自定义超强日志类

    博客分类:
  • PHP
阅读更多
phalcon自带的日志文件类,有两个麻烦之处。
1、路径不能自动创建
2、打印消息非得要求是字符串。

自定义类如下:

<?php



use Phalcon\Logger\Adapter\File as FileAdapter;
use Phalcon\Logger\AdapterInterface;

/**
* 自定义 phalcon 日志类
*
* 解决两大痛点:
* 1、路径不能自动创建
* 2、打印消息非得要求是字符串。
*
* 使用方式
*
*  $logger = SuperLogger::getInstance(SuperLogger::CLIENT_LOG);
*  $logger
*     ->info(  'This is a {message}',
*      [
*          'message' => time()
*      ] )
*     ->log(  time() )
*     ->log(Logger::NOTICE,  time() )
*     ->error(  ['aa'=>11111,'b'=>22222] );
*
*  echo "读取日志:".$logger->getPath()."<br>";
*  echo file_get_contents($logger->getPath());
*
* 也可以构造方法参数为空,再 select,或 setPath
*
* Class SuperLogger
*
* @method AdapterInterface setFormatter(FormatterInterface $formatter)
* @method FormatterInterface getFormatter()
* @method AdapterInterface setLogLevel($level)
* @method AdapterInterface log($type, $message = null, array $context = null)
* @method AdapterInterface debug($message, array $context = null)
* @method AdapterInterface error($message, array $context = null)
* @method AdapterInterface info($message, array $context = null)
* @method AdapterInterface notice($message, array $context = null)
* @method AdapterInterface warning($message, array $context = null)
* @method AdapterInterface alert($message, array $context = null)
* @method AdapterInterface emergency($message, array $context = null)
* @method AdapterInterface begin()
* @method AdapterInterface commit()
*
*
* @author yyy
*/
class SuperLogger
{

    /**
     * @var FileAdapter
     */
    protected $logger;

    protected $file; // 文件完整路径

    protected $baseDir;// 预定义日志文件基路径

    /**
     * 这里自定义一些常量。日志类型。
     */
    const HTTP_LOG = 1;
    const CLIENT_LOG = 2;
    const MENU_LOG = 3;

    const PATH_ARR =[
        self::HTTP_LOG => 'http/',
        self::CLIENT_LOG => 'client/',
        self::MENU_LOG => 'menu/',
    ];

    /**
     * 这里自定义默认日志根路径。
     *
     * @param mixed $file
     */
    public function __construct( $file = null)
    {
        $this->baseDir = BASE_PATH . '/cache/logs/';
        if ($file) {
            if (is_int($file)) {
                $this->select($file);
            } else {
                $this->setPath($file);
            }
        }
    }

    public static function getInstance( $file = null):self
    {
        return new self($file);
    }

    /**
     * 选择不同的日志类型,自行修改。
     *
     * @param $type
     * @return $this
     */
    public function select(int $type=null):self
    {
        if (in_array( $type, array_keys( self::PATH_ARR) )) {
            $this->setPath($this->baseDir . self::PATH_ARR[$type] . date("Ymd") . '.log');
        } else {
            $this->setPath($this->baseDir . date("Ymd") . '.log');
        }
        return $this;
    }

    public function getPath(): string
    {
        return $this->file;
    }


    public function setPath(string $file):self
    {
        $this->file = $file;
        $folder = preg_replace('#^(.+)/[^/]+$#', '$1', $file);
        if (!file_exists($folder)) {
            mkdir($folder, 0777, true);
        }
        $this->logger = new FileAdapter($file);
        return $this;
    }

    function __call($funName, $arguments)
    {
        if ($this->logger == null) {
            $this->select();
        }

        if (in_array($funName, ['debug', 'error', 'info', 'notice', 'warning', 'alert', 'emergency',])
            || ($funName == 'log' && (!(is_int($arguments[0]) && $arguments[0] >= 0 && $arguments[0] <= 9)))
        ) {
            if (is_array($arguments[0])) {
                $arguments[0] = var_export($arguments[0], 1);
            } else {
                $arguments[0] = strval($arguments[0]);
            }
            $this->logger->$funName(...$arguments);
            return $this;
        } elseif ($funName == 'log' && is_int($arguments[0]) && $arguments[0] >= 0 && $arguments[0] <= 9) {
            if (is_array($arguments[1])) {
                $arguments[1] = var_export($arguments[1], 1);
            } else {
                $arguments[1] = strval($arguments[1]);
            }
            $this->logger->$funName(...$arguments);
            return $this;
        } else {
            return $this->logger->$funName(...$arguments);
        }
    }

}

0
0
分享到:
评论

相关推荐

    Logme-Phalcon:Logme 是一个基于 phalconPHP 的日志和报表系统

    Logme-Phalcon Logme 是一个基于 phalconPHP 的日志和报表系统

    PhalconPHP 3.4.0.zip

    PhalconPHP 3.4.0 更新日志:2018-05-28添加Phalcon\Mvc\Router::attach直接将路由对象添加到路由器#13326中;增加了侦听请求的功能:beforeAuthorizationResolve和request:afterAuthorizationResolve事件,这种...

    PHP7框架Phalcon7 v1.2.3

    开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。Phalcon 不只是为了卓越的性能, 我们的目标是让它...

    phalcon开发工具 phalcon-tools

    phalcon开发工具 phalcon-tools

    Phalcon7 (高性能 PHP 7框架) v1.2.3.zip

    Phalcon7 (高性能 PHP 7框架) v1.2.3更新日志 该版本修复了 Model、Security 和 Request 中的 BUG。 新增了 `PhalconIntrusiveAvltree` 类 Phalcon7 (高性能 PHP 7框架)页面展示 相关阅读 同类推荐:程序框架

    Phalcon php框架 v2.0.2.zip

    无需学习和使用 C 语言,所有函数都以 PHP 类的方式曾现。Phalcon 是一个松耦合的框架。   Phalcon 2.0 基于高性能编译语言Zephir,Zephir的目的是提供一个全新的更简单的语法来编写PHP扩展。   Phalcon 1.x还...

    Phalcon 3.1.1 中文文档 chm

    开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。 Phalcon 不只是为了卓越的性能, 我们的目标是让...

    phalcon 中文文档 全中文手册

    phalcon 中文文档 全中文手册 网页格式 放在环境中 或直接打开查看

    Phalcon 3.0.0 中文文档

    Phalcon 3.0.0 中文文档 Phalcon 3.0.0 中文文档 Phalcon 3.0.0 中文文档

    phalcon中文开发文档

    Phalcon 是什么?¶ Phalcon 是开源、全功能栈、使用 C 扩展编写、针对高性能优化的 PHP 5 框架。 开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是...

    PHP扩展Phalcon源码

    Phalcon is optimized for high performance. Its unique architecture allows the framework to always be memory resident, offering its functionality whenever its needed, without expensive file stats and ...

    phalcon_2.0_x64

    phalcon 2.0.0版本64位 包含PHP5.3、PHP5.4、PHP5.5、PHP5.6 本资料共包含以下附件: phalcon_x64_VC9_php5.3.9_2.0.0.zip phalcon_x64_VC9_php5.3.9_2.0.0_nts.zip phalcon_x64_VC9_php5.4.0_2.0.0.zip phalcon...

    Phalcon的PhpStorm代码提示说明

    Phalcon的PhpStorm代码提示说明 Phalcon的PhpStorm代码提示说明

    Phalcon Docs Mobi 格式文档

    Phalcon 官方最新版 v3.3 的mobi格式的文档: Phalcon is an open source full stack framework for PHP, written as a C-extension. Phalcon is optimized for high performance. Its unique architecture allows ...

    phalcon框架的demo

    phalcon框架的demo

    phalcon-v2.0.10 chm中文手册

    phalcon-v2.0.10 文档,大部分已翻译为中文

    phalcon2中文手册+教程+DEMO

    phalcon 最快的php框架 没有之一 phalcon2.06中文手册 入门配置教程 两个DEMO项目 学习phalcon 一步到位

    phalcon手册

    phalcon手册Welcome to Phalcon framework. Our mission is to give you an advanced tool for developing the faster web sites and applications with PHP.

    phalcon IDE代码提示库

    phalcon 框架 IDE代码提示 phpstrom NetBeans 等IDE 使用的代码提示

    phalcon-debugbar, Phalcon框架的强大调试和探查器工具.zip

    phalcon-debugbar, Phalcon框架的强大调试和探查器工具 Phalcon Debugbar将调试栏与 Phalcon框架集成。中文说明特性正常请求捕获Ajax请求捕获重定向请求链捕获简单应用,多模块应用和微型应用支持收集的数据持久:...

Global site tag (gtag.js) - Google Analytics