其他分享
首页 > 其他分享> > hyperf 日志

hyperf 日志

作者:互联网

配置 config/autoload/logger.php

<?php

declare(strict_types=1);

return [
    'default' => [
        'handler' => [
            'class' => Monolog\Handler\StreamHandler::class,
            'constructor' => [
                'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
                'level' => Monolog\Logger::DEBUG,
            ],
        ],
        'formatter' => [
            'class' => Monolog\Formatter\LineFormatter::class,
            'constructor' => [
                'format' => null,
                'dateFormat' => 'Y-m-d H:i:s',
                'allowInlineLineBreaks' => true,
            ],
        ],
    ],
];

使用 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Logger\Logger;
use Hyperf\Logger\LoggerFactory;

/**
 * @AutoController();
 */
class IndexController
{
        /**
         * @var \Psr\Log\LoggerInterface
         */
        protected $logger;

        public function __construct(LoggerFactory $loggerFactory)
        {
                // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
                $this->logger = $loggerFactory->get('log', 'default');
        }

        public function index(){
                $this->logger->info("log message.");
        }
}

客户端测试

curl 118.195.173.53:9501/index/index

服务端查看日志 runtime/logs/hyperf.log

tail -f runtime/logs/hyperf.log

Log代理类

控制器 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
use App\Utils\Log;
/**
 * @AutoController();
 */
class IndexController
{
        public function index(){
                Log::getInstance()->info('Your log message.');
        }
}

Log代理类

<?php

declare(strict_types=1);

namespace App\Utils;


use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\ApplicationContext;

class Log
{
        public static function getInstance(string $name = 'app')
        {
                return ApplicationContext::getContainer()->get(LoggerFactory::class)->get($name);
        }

        public static function __callStatic($name, $arguments)
        {
                self::getInstance()->$name(...$arguments);
        }
}

访问测试

curl 118.195.173.53:9501/index/index

查看服务端日志文件

tail -f runtime/logs/hyperf.log

标签:index,logs,hyperf,日志,runtime,class,log
来源: https://www.cnblogs.com/hu308830232/p/15321324.html