最近用hyperf上线了个项目,守护进程挂着,发现平常控制台输出的日志要是查起来有点尴尬,想让日志写入文件并忽略掉DEBUG和INFO等级的日志,研究一番终于找到调整的方法,奔走相告希望能帮到大家
(实际上hyperf文档关于这块都有介绍,这里介绍操作一下帮助理解)
首先app目录创建stdou接管类StdoutLoggerFactory
<?php
declare(strict_types=1);
namespace App;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;
class StdoutLoggerFactory
{
public function __invoke(ContainerInterface $container)
{
return ApplicationContext::getContainer()->get(\Hyperf\Logger\LoggerFactory::class)->get('log','stdou');
}
}
修改config/dependencies.php 接管StdoutLoggerInterface类
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
return [
Hyperf\Contract\StdoutLoggerInterface::class => App\StdoutLoggerFactory::class,
];
修改config/logger.php 增加日志组别
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
return [
'stdou' => [
'handler' => [
'class' => Monolog\Handler\RotatingFileHandler::class,
'constructor' => [
'filename' => BASE_PATH . '/runtime/logs/stdou/stdou.log',
'level' => Monolog\Logger::DEBUG,
],
],
'formatter' => [
'class' => Monolog\Formatter\LineFormatter::class,
'constructor' => [
'format' => null,
'dateFormat' => null,
'allowInlineLineBreaks' => true,
],
],
],
];
然后就可以让日志以文件的方法输出了,不过一些DEBUG和INFO级别的日志用处不大所以我们有时候需要隐藏这两级
设置日志等级的方法是更改config/logger.php的这一段 如文中只处理日志级别 >=Logger::
NOTICE 的日志
顺便附上级别,也可在源码中找到
Monolog\Logger::DEBUG
Monolog\Logger::INFO
Monolog\Logger::NOTICE
Monolog\Logger::WARNING
Monolog\Logger::ERROR
Monolog\Logger::CRITICAL
Monolog\Logger::ALERT
Monolog\Logger::EMERGENCY