acplo/acplolog

AcploLog为您的ZF2应用程序提供了一些日志实用工具

0.0.1 2015-04-25 22:55 UTC

This package is not auto-updated.

Last update: 2024-10-02 07:50:42 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

介绍

本模块提供了一些有用的日志类

  • ErrorLogger = PHP错误和异常
  • EntityLogger = Doctrine ORM实体
  • SqlLogger = Doctrine DBAL SQL
  • StaticLogger = “快捷方式”到通用的文件日志记录器。可以附加到Zend Server的Z-Ray中(新功能)

需求

本模块仅需要zendframework 2 框架.zend.com

安装

可以使用composer或手动安装

使用composer安装

有关composer文档,请参阅getcomposer.org

  1. 进入您的项目目录

  2. 创建或编辑您的composer.json文件,内容如下

    {
        "minimum-stability": "dev",
        "require": {
            "acplo/acplolog": "1.*"
        }
    }
  3. 运行php composer.phar install

  4. 打开my/project/directory/config/application.config.php并将AcploLog添加到您的modules

    通常将其作为第一个模块插入,以启用所有模块的错误和异常捕获。

```php
<?php
return array(
    'modules' => array(
        'AcploLog',
        'Application'
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);
```

不使用composer的安装

  1. 将此模块AcploLog克隆到您的供应商目录
  2. 在config/application.config.php中启用它,就像上一节中的第4步一样。

用法

要更改选项,请将文件acplolog.global.php.dist复制到您的config/autoload/,将其重命名为acplolog.global.php并更改默认选项。

ErrorLogger

要启用ErrorLogger,只需在public/index.php中添加registerHandlers即可

chdir(dirname(__DIR__));

require 'init_autoloader.php';

AcploLog\Log\ErrorLogger::registerHandlers();

Zend\Mvc\Application::init(require 'config/application.config.php')->run();

您可以在phpunit测试中使用logger。只需在创建自动加载后调用它即可

AcploLog\Log\ErrorLogger::registerHandlers();

输出示例

PHP错误
2012-10-30T17:58:10-02:00 ERR (3): Error: Call to a member function format() on a non-object in <filename> on line <line>
异常
2012-11-01T09:23:53-02:00 ERR (3): Exception- An exception was raised while creating "Application\Service\Test"; no instance returned in <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php in line 733.
Previous: "data/logs2/erros.log" cannot be opened with mode "a" in <dir>/vendor/zendframework/zendframework/library/Zend/Log/Writer/Stream.php in line 87.
Previous: fopen(data/logs2/erros.log): failed to open stream: No such file or directory in <dir>/vendor/zendframework/zendframework/library/Zend/Log/Writer/Stream.php in line 84.
Trace:
#0 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(843): Zend\ServiceManager\ServiceManager->createServiceViaCallback(Object(Closure), 'teste', 'Application\Service\Tes...')
#1 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(487): Zend\ServiceManager\ServiceManager->createFromFactory('teste', 'Application\Service\Tes...')
#2 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(442): Zend\ServiceManager\ServiceManager->create(Array)
#3 <dir>/src/Application/Module.php(29): Zend\ServiceManager\ServiceManager->get('Application\Service\Tes...')
#4 [internal function]: Application\Module->onBootstrap(Object(Zend\Mvc\MvcEvent))
#5 <dir>/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#6 <dir>/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(208): Zend\EventManager\EventManager->triggerListeners('bootstrap', Object(Zend\Mvc\MvcEvent), Array)
#7 <dir>/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(146): Zend\EventManager\EventManager->trigger('bootstrap', Object(Zend\Mvc\MvcEvent))
#8 <dir>/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(243): Zend\Mvc\Application->bootstrap()
#9 <dir>/public/index.php(23): Zend\Mvc\Application::init(Array)
#10 {main}

默认日志文件是data/log/error.log

EntityLogger

第一个用法是将Doctrine实体转储到屏幕上,无论是HTML还是控制台。

为此,只需调用一个静态函数

echo \AcploLog\Log\EntityLogger::dump($entity);

然后它会打印

stdClass Object
(
    [__CLASS__] => User\Entity\User
    [name] => Admin
    [email] => admin@foo.com
    [password] => $2y$14$4n/JYSM7ZtSaZpg1/PgFZefzoblrmaMmMZga.nhf7TZNAd
    [id] => 1
    [created] => DateTime
)

"dump"函数提供了两个额外的参数

public static function dump($entity, $maxDepth = 1, $toHtml = true)

第二个参数指示转储的深度将如何(其他类作为属性,包括集合),默认为1,第三个参数表示输出是否为HTML友好(将输出包含在"pre"标签中)或为控制台版本(默认为true)。

echo \AcploLog\Log\EntityLogger::dump($entity, 2);

然后它会打印

stdClass Object
(
    [__CLASS__] => User\Entity\User
    [name] => Admin
    [email] => admin@foo.com
    [password] => $2y$14$4n/JYSM7ZtSaZpg1/PgFZefzoblrmaMmMZga.nhf7TZNAd
    [id] => 1
    [access] => Array
        (
            [0] => User\Entity\Access
            [1] => User\Entity\Access
            [2] => User\Entity\Access
        )
    [created] => stdClass Object
        (
            [__CLASS__] => DateTime
            [date] => 2014-09-28T07:06:29-03:00
            [timezone] => America/Sao_Paulo
        )
)
echo \AcploLog\Log\EntityLogger::dump($entity, 3);

然后它会打印

stdClass Object
(
    [__CLASS__] => User\Entity\User
    [name] => Admin
    [email] => admin@foo.com
    [password] => $2y$14$4n/JYSM7ZtSaZpg1/PgFZefzoblrmaMmMZga.nhf7TZNAd
    [id] => 1
    [access] => Array
        (
            [0] => stdClass Object
                (
                    [__CLASS__] => User\Entity\Access
                    [ip] => 10.1.1.2
                    [agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/32.0
                    [id] => 1
                    [created] => DateTime
                    [updated] => DateTime
                )

            [1] => stdClass Object
                (
                    [__CLASS__] => User\Entity\Access
                    [ip] => 10.1.1.3
                    [agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/6.1.3 Safari/537.75.14
                    [id] => 2
                    [created] => DateTime
                    [updated] => DateTime
                )
        )
    [created] => stdClass Object
        (
            [__CLASS__] => DateTime
            [date] => 2014-09-28T07:06:29-03:00
            [timezone] => America/Sao_Paulo
        )
)

此类的第二个用途是保存由您的实体生成的数据库操作。

注意:此记录器依赖于DoctrineORMModule。由于它的使用是可选的,因此我没有将其要求放在composer.json中

要启用此记录器,请将其注册到您的doctrine配置中(例如,config/autoload/global.php)

namespace App;
return array(
    // Doctrine config
    'doctrine' => array(
        'driver' => array(
            __NAMESPACE__ . '_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(
                    __DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'
                )
            ),
            'orm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                )
            )
        ),
        'eventmanager' => array(
            'orm_default' => array(
                'subscribers' => array(
                    'AcploLog\Log\EntityLogger'
                )
            )
        )
    )
);

将生成

2012-10-29T19:30:46-02:00 DEBUG (7): Inserting entity Application\Entity\Client. Fields: {"nome":[null,"ClientName"],"created_on":[null,{"date":"2012-10-29 19:30:46","timezone_type":3,"timezone":"America\/Sao_Paulo"}]}
2012-10-29T19:32:23-02:00 DEBUG (7): Updating entity Application\Entity\Client with id 3. Fields: {"nome":["ClientName","ClientName2"]}
2012-10-29T19:36:53-02:00 DEBUG (7): Deleting entity Application\Entity\Client with id 3.

默认日志文件是data/log/entity.log

SqlLogger

使用此记录器,您可以在您的应用程序中保存所有Doctrine数据库操作。

注意:此记录器依赖于DoctrineModule。由于它的使用是可选的,因此我没有将其要求放在composer.json中

编辑config/autoload/acplolog.global.php文件以启用此记录器。

默认日志文件是data/log/sql.log

StaticLogger

此记录器通常用于记录开发或调试消息、数组和对象。只需在任何代码位置静态调用它即可。

AcploLog\Log\StaticLogger::save("Test message");
AcploLog\Log\StaticLogger::save("Test message 2", 'test.log');

将生成

2012-10-29T19:32:30-02:00 DEBUG (6): Test message

或一个对象

AcploLog\Log\StaticLogger::save($myObj);

将生成

2013-07-30T17:26:37-03:00 DEBUG (7): {"User\\Entity\\User":[],"nome":{"type":"string","content":"Leandro"},"sobrenome":{"type":"string","content":"Silva"},"permissao":{"type":"string","content":"usuario"},"email":{"type":"string","content":"leandro@leandrosilva.info"},"acessos":{"type":"object","class":"Doctrine\\ORM\\PersistentCollection"},"login":{"type":"NULL","content":null},"senha":{"type":"string","content":"admin"},"inputFilter":{"type":"NULL","content":null},"id":{"type":"integer","content":3},"cadastrado":{"type":"object","class":"DateTime"},"atualizado":{"type":"object","class":"DateTime"}}

可选地,您可以通过服务定位器获取它

$logger = $sm->get('AcploLog\Log\StaticLogger');
$logger->debug("Test message");

默认日志文件是data/log/static.log

Z-Ray

Z-Ray 是 Zend Server 提供的一个极好的资源,它提供了关于请求、错误和框架的多个信息。它还允许您添加自己的信息,所以我将其中的 StaticLogger 消息添加到了它里面。

更多信息请查看这里

警告:Z-Ray 扩展只适用于 Zend Server 8 或更高版本。

安装

要使用 StaticLogger 与 Z-Ray,请按照以下步骤操作

1- 在 Zend Server 中定位 zray 扩展目录。

例如,在 Mac/Linux 系统上,可以在以下位置找到:/usr/local/zend/var/zray/extensions

2- 创建一个名为 AcploLog 的目录。

3- 将 zray.php 和 logo.png 复制到该目录。

最终结果应该是

ls /usr/local/zend/var/zray/extensions/AcploLog/
logo.png	zray.php
用法

只需使用 StatticLogger,消息就会出现在 Z-Ray 栏的 AcploLog 部分。

可选地,您可以将文件参数传递为 "null" 值,仅使用 Z-Ray,而不将消息写入文件

AcploLog\Log\StaticLogger::save("Test message", null);