code-distortion / clarity-logger
一个生成有用日志的 Laravel 扩展包
Requires
- php: 8.0.* | 8.1.* | 8.2.* | 8.3.*
- code-distortion/staticall: ^0.1.0
Requires (Dev)
- code-distortion/clarity-context: ^0.1.0
- code-distortion/clarity-control: ^0.1.0
- infection/infection: ^0.10 | ^0.11 | ^0.12 | ^0.13 | ^0.14 | ^0.15 | ^0.16 | ^0.17 | ^0.18 | ^0.19 | ^0.20 | ^0.21 | ^0.22 | ^0.23 | ^0.24 | ^0.25 | ^0.26 | ^0.27
- orchestra/testbench: ^6.12 | ^7.0 | ^8.0
- phpstan/phpstan: ^0.9 | ^0.10 | ^0.11 | ^0.12 | ^1.0
- phpunit/phpunit: ~4.8 | ^5.0 | ^6.0 | ^7.0 | ^8.4 | ^9.0 | ^10.0
- squizlabs/php_codesniffer: ^3.8.0
Suggests
- code-distortion/clarity-context: Understand Your Exceptions. Part of the Clarity Suite
- code-distortion/clarity-control: Handle Your Exceptions. Part of the Clarity Suite
README
code-distortion/clarity-logger 是一个 Laravel 扩展包,用于生成有用的异常日志。
EXCEPTION (CAUGHT):
exception Illuminate\Http\Client\ConnectionException: "cURL error 6: Could not resolve host: api.example-gateway.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.example-gateway.com"
- location app/Http/Controllers/CheckoutController.php on line 50 (method "submit")
- vendor vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php on line 856 (closure)
request POST https://my-website.com/checkout
- referrer https://my-website.com/checkout
- route cart.checkout
- middleware web
- action CheckoutController@submit
user 3342 - Bob - bob@example.com (123.123.123.123)
- agent Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
date/time Sunday 2nd April at 7:08pm (Australia/Sydney) 2023-04-02 19:08:23 AEST +10:00
Clarity 套件
Clarity Logger 是 Clarity 套件 的一部分,旨在让您更轻松地管理异常
- Clarity Context - 理解您的异常
- Clarity Logger - 有用的异常日志
- Clarity Control - 处理您的异常
目录
安装
通过 composer 安装此包
composer require code-distortion/clarity-logger
配置文件
如果您想发布 config/code_distortion.clarity_logger.php
配置文件,请使用以下命令
php artisan vendor:publish --provider="CodeDistortion\ClarityLogger\ServiceProvider" --tag="config"
更新您的异常处理器
Laravel 项目使用一个 异常处理器 类来记录异常。您需要更新此配置,以便 Clarity Logger 能够记录异常。
请将以下内容添加到 app/Exceptions/Handler.php
的 register()
方法中。
如果您使用它们,可以通过添加 $this->exceptionContext($e)
来包含 Laravel 自身的上下文细节。
默认情况下,Laravel 会在日志后添加 PHP 的堆栈跟踪。如果您喜欢,可以通过添加 ->stop()
来关闭此功能。
// app/Exceptions/Handler.php namespace App\Exceptions; use CodeDistortion\ClarityLogger\Logger; // <<< use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Throwable; class Handler extends ExceptionHandler { … /** * Register the exception handling callbacks for the application. */ public function register(): void { $this->reportable(function (Throwable $e) { Logger::log($e, $this->exceptionContext($e)); // <<< })->stop(); // <<< } }
Laravel 现在将使用 Clarity Logger 记录异常。
有关异常处理的更多信息,请参阅 Laravel 文档。
手动记录
如果您捕获了异常,或者只想记录一条消息,您可以通过自行触发记录功能
Logger::log($exception); Logger::log('message');
您可以指定报告级别
Logger::debug($exception); // or ::debug('message') Logger::info($exception); // or ::info('message') Logger::notice($exception); // or ::notice('message') Logger::warning($exception); // or ::warning('message') Logger::error($exception); // or ::error('message') Logger::critical($exception); // or ::critical('message') Logger::alert($exception); // or ::alert('message') Logger::emergency($exception); // or ::emergency('message') // or Logger::level(Settings::REPORTING_LEVEL_INFO)->log($exception); // or ->log('message');
如果您想将日志记录到特定的通道,请在触发记录操作之前指定它
Logger::channel('slack')->log($exception);
这些方法可以链式调用
Logger::channel('slack')->debug()->log($exception); Logger::emergency()->channel('slack')->log('message');
添加一些上下文
如果您将 Clarity Context 添加到项目中(这允许您向代码中添加上下文细节),Clarity Logger 将自动包含您的上下文细节。
这在调试异常时可以是一个强大的工具。例如。
EXCEPTION (UNCAUGHT):
exception Illuminate\Http\Client\ConnectionException: "cURL error 6: Could not resolve host: api.example-gateway.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.example-gateway.com"
- location app/Http/Controllers/CheckoutController.php on line 50 (method "submit")
- vendor vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php on line 856 (closure)
request POST https://my-website.com/checkout
- referrer https://my-website.com/checkout
- route cart.checkout
- middleware web
- action CheckoutController@submit
- trace-id 1234567890
user 3342 - Bob - bob@example.com (123.123.123.123)
- agent Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
date/time Sunday 2nd April at 7:08pm (Australia/Sydney) 2023-04-02 19:08:23 AEST +10:00
CONTEXT:
app/Domain/Checkout/PerformCheckoutAction.php on line 20 (method "handle")
- "Performing checkout"
- user-id = 5
- order-id = 123
app/Domain/Payments/MakePaymentAction.php on line 19 (method "handle") (last application frame)
- "Sending payment request to gateway"
- payment-gateway = 'examplexyz.com'
- card-id = 456
- amount = '10.99'
vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php on line 856 (closure)
- The exception was thrown
测试此包
- 克隆此包:
git clone https://github.com/code-distortion/clarity-logger.git .
- 运行
composer install
以安装依赖项 - 运行测试:
composer test
变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
语义化版本
此库使用 SemVer 2.0.0 版本化。这意味着对 X
的更改表示破坏性更改:0.0.X
、0.X.y
、X.y.z
。当此库更改到版本 1.0.0、2.0.0 等时,这并不一定表示它是一个值得注意的版本发布,它只是表示更改是破坏性的。
Treeware
此包是 Treeware。如果您在生产环境中使用它,我们要求您 为我们购买的树木付费 以感谢我们的工作。通过向 Treeware 森林捐款,您将为当地家庭创造就业机会并恢复野生动物栖息地。
贡献
请参阅贡献指南获取详细信息。
行为准则
请参阅行为准则获取详细信息。
安全性
如果您发现任何安全相关的问题,请发送电子邮件至tim@code-distortion.net,而不是使用问题跟踪器。
致谢
许可
MIT 许可证(MIT)。请参阅许可文件获取更多信息。