Setting Up a Log Viewer in Hyperf with Routing and View Components
This guide explains how to install and configure the Hyperf log‑viewer component, publish its configuration, register a route, add view and template engine support, and adjust view and log settings to enable file log browsing and simple search within a PHP backend application.
Reading font
1. Interface
The article begins with a screenshot illustrating the appearance of the log‑viewer page.
2. Usage
Step 1: Install the log‑viewer component. composer require sett/hyperf-log-viewer Step 2: Publish the component’s configuration file. php bin/hyperf.php vendor:publish sett/hyperf-log-viewer Step 3: Register a route that points to the viewer controller.
Router::get('/logs', 'Sett\LogViewer\Controller\LogViewController@index');Step 4: Install the Hyperf view component. composer require hyperf/view Step 5: Install a template engine (think‑template). composer require sy-records/think-template Step 6: Configure the view settings in config/autoload/view.php (create the file if it does not exist).
return [
'engine' => ThinkEngine::class,
'mode' => Mode::TASK,
'config' => [
// create these directories if they are missing
'view_path' => BASE_PATH . '/storage/view/',
'cache_path' => BASE_PATH . '/runtime/view/',
],
];Step 7: Add the log directory configuration in config/autoload/logViewer.php.
return [
"path" => BASE_PATH . "/runtime/logs/",
"pattern" => "*.log",
"size" => 10,
];3. Notes
The log timestamps must follow the YYYY-MM-DD HH:MM:SS format; otherwise records may not be displayed.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.