日志非常重要,在用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;
}
}
```