Communitales 日志组件

3.2.0 2024-04-19 18:02 UTC

This package is auto-updated.

Last update: 2024-09-19 19:02:33 UTC


README

方便集成的消息和异常日志记录。

设置

只需使用LogAwareTrait

然后通过$this->setLogger($logger);设置日志记录器,或者使用Psr\Log\LoggerAwareInterface作为标记接口,让依赖注入为您完成这项工作。

Symfony 示例

services:

    _instanceof:
        Psr\Log\LoggerAwareInterface:
            calls:
                - [setLogger, ['@logger']]

使用方法


use App\Component\Log\LogAwareTrait;
use Psr\Log\LoggerAwareInterface;
use \RuntimeException;

class SomeClass implements LoggerAwareInterface
{

    use LogAwareTrait;

    public function testLog(): void
    {
        // Log your message including debug information
        $this->log('Test message', LogLevel::DEBUG, ['param1' => 'useful debug information']);

        // Log an error
        $this->log('This should not happen');
    }

    public function testLogException(): void
    {
        try {
            throw new RuntimeException('Something gone wrong');
        } catch (RuntimeException $exception) {

            // Log with one line
            $this->logException($exception);
        }

    }
}

开箱即用的 Sentry 日志记录

如果存在\Sentry\captureException函数,异常也将记录到 Sentry。