rnr1721 / le7-logger
le7 PHP MVC 框架或任何 PHP 项目的 PSR 日志模块
1.0.5
2023-05-20 16:31 UTC
Requires
- php: >=8.1
- psr/log: ^3.0
Requires (Dev)
- phpunit/phpunit: ^10.0
- vimeo/psalm: ^5.7
Provides
- psr/log-implementation: 3.0.0
README
要求
- PHP 8.1 或更高版本。
- Composer 2.0 或更高版本。
它能做什么?
- 将日志记录到任何目的地(需要编写类似的适配器),默认为文件、标准输出和空
- 写入多个目的地(例如,写入两个相似的文件或写入文件和数据库)
安装
composer require rnr1721/le7-logger
测试
composer test
如何使用它?
use Core\Logger\LoggerFactoryGeneric; $factory = new LoggerFactoryGeneric(); // $log = $this->factory->logStd(); - for docker etc $log = $this->factory->logFile('/home/www/example.com/myproject/log.txt'); // Use it as any PSR logger $log->info('info message'); $log->critical('alert message');
如何使用不同的目的地?
例如,我们需要将文件写入两个不同的目的地
use Core\Logger\LoggerFactoryGeneric; $factory = new LoggerFactoryGeneric(); $path1 = '/home/www/example.com/myproject/log1.txt'; $path2 = '/home/www/example.com/myproject/log2.txt'; $log = $this->factory->getLogger(); $log->addBroadcast(new LoggerRouteFile(['filePath' => $path1])) ->addBroadcast(new LoggerRouteFile(['filePath' => $path2])); // Use it as any PSR logger $log->info('info message'); $log->critical('alert message');
文件适配器中的不同格式
use Core\Logger\Adapters\LoggerRouteFile; // ... $params = [ 'filePath' => '/path/to/your/log.log', 'template' => '{date} {level} {message} {context}'; ]; $log->addBroadcast(new LoggerRouteFile($params));
或不同的方式
use Core\Logger\Adapters\LoggerRouteFile; // ... $loggerRouteFile = new LoggerRouteFile(); $loggerRouteFile->setFilePath('/your/file/path') ->setTemplate('{date} {level} {message} {context}'); $log->addBroadcast($loggerRouteFile);