美文网首页
hyperf3.0日志简单使用

hyperf3.0日志简单使用

作者: geeooooz | 来源:发表于2023-06-14 17:53 被阅读0次

    安装

    composer require hyperf/logger

    配置文件 (日志按照日期存储) logger.php

    <?php
    
    declare(strict_types=1);
    /**
     * This file is part of Hyperf.
     *
     * @link     https://www.hyperf.io
     * @document https://hyperf.wiki
     * @contact  group@hyperf.io
     * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
     */
    return [
        'default' => [
            'handler' => [
                'class' => Monolog\Handler\RotatingFileHandler::class,
                'constructor' => [
                    // filename 日志按照日期存储
                    'filename' => BASE_PATH . '/runtime/logs/suiyi.log',
                    'level' => Monolog\Logger::DEBUG,
                ],
            ],
            'formatter' => [
                'class' => Monolog\Formatter\LineFormatter::class,
                'constructor' => [
                    'format' => null,
                    'dateFormat' => null,
                    'allowInlineLineBreaks' => true,
                ],
            ],
        ],
    ];
    
    

    Index中使用

    <?php
    
    declare(strict_types=1);
    /**
     * This file is part of Hyperf.
     *
     * @link     https://www.hyperf.io
     * @document https://hyperf.wiki
     * @contact  group@hyperf.io
     * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
     */
    namespace App\Controller;
    use Psr\Log\LoggerInterface;
    use Hyperf\Logger\LoggerFactory;
    
    class IndexController extends AbstractController
    {
        protected LoggerInterface $logger;
    
        public function __construct(LoggerFactory $loggerFactory)
        {
            // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
            $this->logger = $loggerFactory->get('index', 'default');
        }
    
        public function index()
        {
            $this->logger->info('日志记录');
        }
    }
    
    
    image.png

    相关文章

      网友评论

          本文标题:hyperf3.0日志简单使用

          本文链接:https://www.haomeiwen.com/subject/mekkydtx.html