Skip to content

Commit

Permalink
Merge pull request #5 from zunnu/4.x-dev
Browse files Browse the repository at this point in the history
4.x dev
  • Loading branch information
zunnu authored Oct 9, 2022
2 parents 288a2b4 + fae1ef0 commit 9cd7991
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 59 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/composer.lock
/composer.phar
/phpunit.xml
/vendor
config/Migrations/schema-dump-default.lock
/.phpunit.result.cache
/phpunit.phar
/config/Migrations/schema-dump-default.lock
/vendor/
/.idea/
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# LogReader plugin for CakePHP

A simple CakePHP log reader.
Log reader helps you quickly and clearly see individual log entries of your cakePHP application.
With log reader you no longer need to read raw log files from the server. Log reader allows you to read logs straight from the UI.

Log readers API allows you to create your own custom application to help you get head of errors and provide you with useful information.

## Documentation
See the API documentation of [Log Reader](https://github.com/zunnu/log-reader/wiki)

## Requirements
* CakePHP 3.x
* CakePHP 4.x
* PHP 7.2 >

## Installing Using [Composer][composer]
Expand All @@ -25,9 +31,6 @@ You can see the logs by going to
http://app-address/log-reader
<img src="https://i.imgur.com/8sCwLBh.png" alt="logs">

## Documentation
See the API documentation of [Log Reader](https://github.com/zunnu/log-reader/wiki)

## License

Licensed under [The MIT License][mit].
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"cakephp/cakephp": "^3.5"
"php": ">=7.2",
"cakephp/cakephp": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7.14|^6.0"
"phpunit/phpunit": "^8.5 || ^9.3"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 0 additions & 22 deletions config/routes.php

This file was deleted.

13 changes: 4 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>
<!-- Setup fixture extension -->
<extensions>
<extension class="Cake\TestSuite\Fixture\PHPUnitExtension" />
</extensions>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

</phpunit>
1 change: 1 addition & 0 deletions src/Controller/Api/V1/AppController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace LogReader\Controller\Api\V1;

Expand Down
28 changes: 21 additions & 7 deletions src/Controller/Api/V1/LogReaderController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace LogReader\Controller\Api\V1;

use LogReader\Controller\Api\V1\AppController;
Expand All @@ -7,15 +9,23 @@

class LogReaderController extends AppController
{
public function initialize() {
/**
* initialize method
*
* @return void
*/
public function initialize(): void
{
parent::initialize();
$this->RequestHandler->renderAs($this, 'json');
}

/**
* Logs method
*/
public function logs($date = null) {
$this->viewBuilder()->setLayout(false);
public function logs($date = null)
{
$this->viewBuilder()->disableAutoLayout();
$conditions = [];

// SEARCH
Expand All @@ -31,6 +41,8 @@ public function logs($date = null) {
$types = explode(',', $data['types']);
$conditions['types'] = array_map('trim', array_filter($types));
}
} elseif ($this->request->is('get')) {
$conditions['files'] = ['error.log', 'debug.log'];
}

$this->Reader = new Reader($conditions);
Expand All @@ -46,8 +58,9 @@ public function logs($date = null) {
* Types method
* Return available log types
*/
public function types($date = null) {
$this->viewBuilder()->setLayout(false);
public function types($date = null)
{
$this->viewBuilder()->disableAutoLayout();
$this->Reader = new Reader();
$types = $this->Reader->getLogTypes();

Expand All @@ -61,8 +74,9 @@ public function types($date = null) {
* Files method
* Return the available log files
*/
public function files($date = null) {
$this->viewBuilder()->setLayout(false);
public function files($date = null)
{
$this->viewBuilder()->disableAutoLayout();
$this->Reader = new Reader();
$files = $this->Reader->getFiles();

Expand Down
1 change: 1 addition & 0 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace LogReader\Controller;

Expand Down
10 changes: 7 additions & 3 deletions src/Controller/LogReaderController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace LogReader\Controller;

use LogReader\Controller\AppController;
Expand All @@ -12,15 +14,17 @@ class LogReaderController extends AppController
*
* @return void
*/
public function initialize() {
public function initialize(): void
{
parent::initialize();
}

/**
* Index method
*/
public function index($date = null) {
$this->viewBuilder()->setLayout(false);
public function index($date = null)
{
$this->viewBuilder()->disableAutoLayout();
$conditions = [];

// SEARCH
Expand Down
90 changes: 90 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,102 @@
<?php
declare(strict_types=1);

namespace LogReader;

use Cake\Core\BasePlugin;
use Cake\Core\ContainerInterface;
use Cake\Core\PluginApplicationInterface;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\RouteBuilder;
use Cake\Console\CommandCollection;

/**
* Plugin for LogReader
*/
class Plugin extends BasePlugin
{
/**
* Load all the plugin configuration and bootstrap logic.
*
* The host application is provided as an argument. This allows you to load
* additional plugin dependencies, or attach events.
*
* @param \Cake\Core\PluginApplicationInterface $app The host application
* @return void
*/
public function bootstrap(PluginApplicationInterface $app): void
{
}

/**
* Add routes for the plugin.
*
* If your plugin has many routes and you would like to isolate them into a separate file,
* you can create `$plugin/config/routes.php` and delete this method.
*
* @param \Cake\Routing\RouteBuilder $routes The route builder to update.
* @return void
*/
public function routes(RouteBuilder $routes): void
{
$routes->plugin(
'LogReader',
['path' => '/log-reader'],
function (RouteBuilder $builder) {
$builder->connect('/', ['controller' => 'LogReader', 'action' => 'index'])->setExtensions(['json']);

$builder->prefix('api', function (RouteBuilder $builder) {
$builder->prefix('v1', function (RouteBuilder $builder) {
$builder->connect('/files', ['controller' => 'LogReader', 'action' => 'files'])->setExtensions(['json']);
$builder->connect('/types', ['controller' => 'LogReader', 'action' => 'types'])->setExtensions(['json']);
$builder->connect('/logs', ['controller' => 'LogReader', 'action' => 'logs'])->setExtensions(['json']);
// // $builder->connect('/:controller');
});
});

$builder->fallbacks();
}
);
parent::routes($routes);
}

/**
* Add middleware for the plugin.
*
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to update.
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
// Add your middlewares here

return $middlewareQueue;
}

/**
* Add commands for the plugin.
*
* @param \Cake\Console\CommandCollection $commands The command collection to update.
* @return \Cake\Console\CommandCollection
*/
public function console(CommandCollection $commands) : CommandCollection
{
// Add your commands here

$commands = parent::console($commands);

return $commands;
}

/**
* Register application container services.
*
* @param \Cake\Core\ContainerInterface $container The Container to update.
* @return void
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
*/
public function services(ContainerInterface $container): void
{
// Add your services here
}
}
25 changes: 17 additions & 8 deletions src/Reader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace LogReader;
use Cake\Core\Exception\Exception;
use Cake\Filesystem\Folder;
Expand Down Expand Up @@ -30,15 +32,17 @@ class Reader {
'debug' => 'Debug'
];

public function __construct($config = []) {
function __construct($config = [])
{
$this->config = $config;
}

/**
* Get the date of the files
* @return array List of different dates of files
*/
public function getFileDates() {
public function getFileDates(): array
{
$dates = [];
$folder = new Folder(LOGS);
$files = $folder->findRecursive('.*', true);
Expand All @@ -62,7 +66,8 @@ public function getFileDates() {
* The files and types that are parsed need to be set in config
* @return array List of logs
*/
public function read() {
public function read(): array
{
$date = !empty($this->config['date']) ? $this->config['date'] : null;
$selectedTypes = !empty($this->config['types']) ? $this->config['types'] : [];
$selectedFiles = !empty($this->config['files']) ? $this->config['files'] : [];
Expand Down Expand Up @@ -107,7 +112,8 @@ public function read() {
* @param array $selectedFiles List of files to get the logs from
* @return array Content of the selected files
*/
private function getLogFile($selectedFiles = []) {
private function getLogFile($selectedFiles = []): array
{
$folder = new Folder(LOGS);
$files = $folder->findRecursive('.*', true);
$data = [];
Expand Down Expand Up @@ -158,14 +164,15 @@ private function getLogFile($selectedFiles = []) {
return $data;
}

return false;
return [];
}

/**
* Get list of log files inside the logs folder
* @return array List of files
*/
public function getFiles() {
public function getFiles(): array
{
$filesList = [];
$folder = new Folder(LOGS);
$files = $folder->findRecursive('.*', true);
Expand Down Expand Up @@ -201,7 +208,8 @@ public function getFiles() {
* @param array $data Content of log file
* @return array Parsed data with type, date and content
*/
private function _parseData($data) {
private function _parseData($data): array
{
$data = preg_split("/\r\n|\n|\r/", $data);
$buildData = [];
$tmp = '';
Expand Down Expand Up @@ -258,7 +266,8 @@ private function _parseData($data) {
* Return available log file types
* @return array
*/
public function getLogTypes() {
public function getLogTypes(): array
{
return $this->logTypes;
}
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9cd7991

Please sign in to comment.