linio / microlog
为monolog提供静态包装器
3.0.0
2022-02-22 23:41 UTC
Requires
- php: ^7.4 || ^8.0
- psr/log: ^1.0.1 || ^2.0 || ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.6
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-08-28 19:01:55 UTC
README
Microlog - Monolog的静态包装器
用途
日志记录是所有应用程序的一部分。需要在日志服务中注入需要依赖,而这个依赖与您正在编写的代码无关。Microlog的创建就是为了消除这个需求。
Microlog允许您从应用程序的任何部分创建不同上下文中的日志条目。Microlog构建时考虑到了永远不会抛出异常。由日志引起的异常会导致应用程序的正常流程失败,而日志记录不应该是这样做的。
安装
要安装microlog
$ composer require linio/microlog
基本用法
<?php declare(strict_types=1); use Linio\Component\Microlog\Log; use Monolog\Handler\StreamHandler; // Register a logger for a channel $defaultLogger = new StreamHandler(); Log::setLoggerForChannel($defaultLogger, Log::DEFAULT_CHANNEL); // Log an emergency Log::emergency('This is a test emergency'); // Log an alert Log::alert('This is a test alert'); // Log a critical error Log::critical('This is a test critical error'); // Log an error Log::error('This is a test error'); // Log a warning Log::warning('This is a test warning'); // Log a notice Log::notice('This is a test notice'); // Log a bit of information Log::info('This is test information'); // Log debug information Log::debug('This is test debug information'); // Log at an arbitrary level Log::log('emergency', 'This is a test entry at an arbitrary level of emergency');
多个通道
通道允许您为不同类型的消息配置不同的处理器。
默认情况下,所有日志都将创建在默认日志通道 Log::DEFAULT_CHANNEL (default) 中。如果您希望在日志记录时指定不同的通道,请将所有非任意级别日志方法的第二个参数指定为通道。
<?php declare(strict_types=1); use Linio\Component\Microlog\Log; use Monolog\Handler\StreamHandler; $defaultLogger = new NullLogger(); Log::setLoggerForChannel($defaultLogger, 'emergency'); Log::emergency('This is an emergency in the emergency channel', 'emergency');
贡献
请随时以PR的形式发送您的贡献。确保您更新/编写新的测试以支持您的贡献。请遵循PSR-2。