hyperf如何开启日志

日志非常重要,在用hyperf开发如何记录日志呢?

1、安装包`composer require hyperf/logger`

2、查看配置文件`config/autoload/logger.php`

```
[
'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,
],
],
],
];

```

3、添加路由

`Router::get('/log', 'App\Controller\LogController@index');`

4、添加controller

```
logger = $loggerFactory->get('log', 'default');
}
public function index(RequestInterface $request)
{
$id = $request->input('id',1);
$this->logger->info("log message.");
$list = Db::connection('default')->select("select *from user where id = $id");
var_dump(Arr::last(Db::getQueryLog()));
error_log(print_r($id,true),3,"/tmp/1.txt");
return $list;
}
}
```

    A+
发布日期:2021年12月28日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: