communitales / log
Communitales 日志组件
3.2.0
2024-04-19 18:02 UTC
Requires
- php: >=8.3
- psr/log: ^1|^2|^3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.51
- monolog/monolog: ^3.6
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.19.0
- rector/rector: ^1.0
- sentry/sentry: ^4.7
- vimeo/psalm: ^5.23
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。