zooxsmart / los-log
LosLog提供了一些日志工具
Requires
- php: ^8.2
- laminas/laminas-diactoros: ^3.3
- laminas/laminas-log: ^2.17
- laminas/laminas-stratigility: ^3.11
- psr/container: ^1.0 || ^2.0
- psr/log: ^1.0
Requires (Dev)
- laminas/laminas-coding-standard: ^1.0
- mikey179/vfsstream: ^1.6
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^7.0
- rollbar/rollbar: ^1.4
- satooshi/php-coveralls: ^2.0
- squizlabs/php_codesniffer: ^3.7.2
Suggests
- rollbar/rollbar: Needed if you want to use Rollbar logger
README
介绍
这是2.0版本文档。请参考README-1.0获取1.0版本的文档。
此模块提供了一些有用的日志类
- LosLog = 适用于PSR-7兼容框架/应用程序的错误中间件
- HttpLog = 用于记录PSR-7应用程序的请求和响应
- ErrorLogger = PHP错误
- ExceptionLogger = PHP异常
- StaticLogger = “快捷”访问通用文件记录器。可以附加到Zend Server的Z-Ray
- Rollbar writer = 一个Rollbar写入器。将错误和异常上传到Rollbar服务
要求
- php >= 5.6.0
- laminas/laminas-stratigility
- laminas/laminas-diactoros
- laminas/laminas-log
安装
有关composer文档,请参阅getcomposer.org。
php composer.phar require los/loslog
用法
将文件loslog.global.php.dist复制到您的config/autoload/,将其重命名为loslog.global.php,并根据需要更改默认选项。
LosLog中间件
Zend Expressive
Expressive 2.0引入了一种新的处理错误的方法,使用ErrorHandler的监听器和委托工厂,因此这是首选方法。
将委托工厂添加到ErrorHandler中,如下所示
return [ 'dependencies' => [ 'factories' => [ LosMiddleware\LosLog\LosLog::class => LosMiddleware\LosLog\LosLogFactory::class, ], 'delegators' => [ ErrorHandler::class => [ LosMiddleware\LosLog\ErrorHandlerListenerDelegatorFactory::class, ], ], ], ];
通用使用
如果使用其他框架,可以将LosLogFactory添加到您的工厂系统中,手动创建一个LosLog实例或直接调用LosLogFactory。
HttpLog中间件
它将以紧凑或完整模式记录请求和响应。如果存在,它将包括X-Request-Id和X-Response-Time头信息。
Zend Expressive
将中间件添加到您的管道中的第一个中间件,如下所示
return [ 'middleware_pipeline' => [ 'before' => [ 'middleware' => [ LosMiddleware\LosLog\HttpLog::class, ], 'priority' => 10000, ], ], ];
在loslog.global.php(或loslog.local.php)中设置所需的选项
'http_logger_file' => 'http.log', 'log_request' => true, 'log_response' => true, 'full' => false,
您可以将它与los/request-id和los/response-time集成。顺序很重要,如下所示
return [ 'middleware_pipeline' => [ 'before' => [ 'middleware' => [ LosMiddleware\RequestId\RequestId::class, LosMiddleware\LosLog\HttpLog::class, LosMiddleware\ResponseTime\ResponseTime::class ], 'priority' => 10000, ], ], ];
这将产生
2015-11-20T14:04:51+00:00 INFO (6): Request: GET /shop/v1/item/1 RequestId: 12dbf2d2-52c5-4954-b573-3aa2fee58612
2015-11-20T14:04:51+00:00 INFO (6): Response: 200 OK RequestId: 12dbf2d2-52c5-4954-b573-3aa2fee58612 ResponseTime: 14.90ms
ErrorLogger
要启用ErrorLogger,只需在public/index.php中添加registerHandlers即可
Zend Framework 2
chdir(dirname(__DIR__)); require 'init_autoloader.php'; \LosMiddleware\LosLog\ErrorLogger::registerHandlers(); Laminas\Mvc\Application::init(require 'config/application.config.php')->run();
Zend Expressive
chdir(dirname(__DIR__)); require 'vendor/autoload.php'; /** @var \Interop\Container\ContainerInterface $container */ $container = require 'config/container.php'; \LosMiddleware\LosLog\ErrorLogger::registerHandlers('error.log', '/tmp'); /* @var \Mezzio\Application $api */ $app = $container->get('Mezzio\Application'); $app->run();
您可以使用记录器与您的phpunit测试一起使用。只需在创建自动加载后在其引导文件中调用它即可
\LosLog\Log\ErrorLogger::registerHandlers();
输出示例
2015-10-30T17:58:10-02:00 ERR (3): Error: Call to a member function format() on a non-object in <filename> on line <line>
默认日志文件是data/log/error.log
ExceptionLogger
要启用ExceptionLogger,只需在public/index.php中添加registerHandlers即可
Zend Framework 2
chdir(dirname(__DIR__)); require 'init_autoloader.php'; \LosMiddleware\LosLog\ExceptionLogger::registerHandlers('exception.log', '/tmp'); Laminas\Mvc\Application::init(require 'config/application.config.php')->run();
Zend Expressive
chdir(dirname(__DIR__)); require 'vendor/autoload.php'; /** @var \Interop\Container\ContainerInterface $container */ $container = require 'config/container.php'; \LosMiddleware\LosLog\ExceptionLogger::registerHandlers('exception.log', '/tmp'); /* @var \Mezzio\Application $api */ $app = $container->get('Mezzio\Application'); $app->run();
输出示例
2015-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): Laminas\ServiceManager\ServiceManager->createServiceViaCallback(Object(Closure), 'teste', 'Application\Service\Tes...')
#1 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(487): Laminas\ServiceManager\ServiceManager->createFromFactory('teste', 'Application\Service\Tes...')
#2 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(442): Laminas\ServiceManager\ServiceManager->create(Array)
#3 <dir>/src/Application/Module.php(29): Laminas\ServiceManager\ServiceManager->get('Application\Service\Tes...')
#4 [internal function]: Application\Module->onBootstrap(Object(Laminas\Mvc\MvcEvent))
#5 <dir>/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Laminas\Mvc\MvcEvent))
#6 <dir>/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(208): Laminas\EventManager\EventManager->triggerListeners('bootstrap', Object(Laminas\Mvc\MvcEvent), Array)
#7 <dir>/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(146): Laminas\EventManager\EventManager->trigger('bootstrap', Object(Laminas\Mvc\MvcEvent))
#8 <dir>/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(243): Laminas\Mvc\Application->bootstrap()
#9 <dir>/public/index.php(23): Laminas\Mvc\Application::init(Array)
#10 {main}
默认日志文件是data/log/exception.log
StaticLogger
此记录器通常用于记录开发或调试消息、数组和对象。只需在任何地方静态调用它即可。
\LosMiddleware\LosLog\StaticLogger::save("Test message"); \LosMiddleware\LosLog\StaticLogger::save("Test message 2", 'test.log');
将生成
2015-10-29T19:32:30-02:00 DEBUG (6): Test message
或一个对象
\LosMiddleware\LosLog\StaticLogger::save($myObj);
将生成
2015-10-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"}}
默认日志文件是data/log/static.log
Z-Ray
Z-Ray是来自Zend Server的出色资源,它提供了有关请求、错误和框架的多个信息。它还具有添加您自己的信息的可能性,因此我添加了StaticLogger消息。
更多信息请参阅这里。
安装
LosLog模块可通过官方Z-Ray插件系统获取,只需从您的Zend Server UI访问选项卡并安装它。
用法
只需使用StaticLogger,消息就会出现在Z-Ray栏的LosLog部分中。
可选地,您可以将文件参数传递为“null”值,以便仅使用Z-Ray,而无需将消息写入文件
\LosMiddleware\LosLog\StaticLogger::save("Test message", null);