Skip to content

Commit

Permalink
Working bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
evotodi committed Mar 4, 2020
1 parent 30961a5 commit 2ae4b1f
Show file tree
Hide file tree
Showing 17 changed files with 439 additions and 265 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"require": {
"php": "^7.1.3",
"greenskies/collection": "^0.0.3",
"jdorn/sql-formatter": "^1.2",
"ddtraceweb/monolog-parser": "^1.2"
"jdorn/sql-formatter": "^1.2"
},
"require-dev": {
"ext-json": "*",
Expand Down
41 changes: 8 additions & 33 deletions src/Controller/LogListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,29 @@

namespace Evotodi\LogViewerBundle\Controller;

use Evotodi\LogViewerBundle\Model\LogList;
use Psr\Container\ContainerInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Evotodi\LogViewerBundle\Service\LogList;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

class LogListController extends AbstractController implements ServiceSubscriberInterface
{
private $parameterBag;
private $logFiles;
private $useAppLogs;
/**
* @var LogList
*/
private $logList;

public function __construct(ParameterBagInterface $parameterBag, array $logFiles, bool $useAppLogs = false)
public function __construct(LogList $logList)
{
$this->parameterBag = $parameterBag;
$this->logFiles = $logFiles;
$this->useAppLogs = $useAppLogs;
$this->logList = $logList;
}

/**
* @return Response
*/
public function logListAction()
{
dump($this->logFiles);
dump($this->useAppLogs);

$logs = [];

if($this->useAppLogs){
$finder = new Finder();
$finder->files()->in($this->parameterBag->get('kernel.logs_dir'));
foreach ($finder as $file){
$logs[] = ['name' => $file->getFilename(), 'path' => $file->getRealPath(), 'pattern' => null, 'days' => 0];
}
}

foreach ($this->logFiles as $logFile){
if(is_null($logFile['name'])){
$logFile['name'] = basename($logFile['path']);
}
$logs[] = ['name' => $logFile['name'], 'path' => $logFile['path'], 'pattern' => $logFile['pattern'], 'days' => $logFile['days']];
}

$logs = $this->logList->getLogList();
return $this->render('@EvotodiLogViewer/listView.html.twig', [
'logs' => $logs
]);
Expand Down
62 changes: 45 additions & 17 deletions src/Controller/LogViewerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,72 @@

namespace Evotodi\LogViewerBundle\Controller;

use Evotodi\LogViewerBundle\Model\LogView;
use Evotodi\LogViewerBundle\Reader\LogReader;
use Evotodi\LogViewerBundle\Service\LogList;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class LogViewerController extends AbstractController
{
/**
/**
* @var LogList
*/
private $logList;

public function __construct(LogList $logList)
{
$this->logList = $logList;
}

/**
* @param Request $request
* @return Response
*/
public function logViewAction(Request $request)
{
$log = urldecode($request->query->get('log'));
$name = urldecode($request->query->get('name'));
$pattern = urldecode($request->query->get('pattern'));
$id = urldecode($request->query->get('id'));
$delete = filter_var($request->query->get('delete'), FILTER_VALIDATE_BOOLEAN);
$logs = $this->logList->getLogList();

dump($name);
dump($log);
dd($pattern);

if(!file_exists($log)){
throw new FileNotFoundException(sprintf("Log file \"%s\" was not found!", $log));
if(!file_exists($logs[$id]['path'])){
throw new FileNotFoundException(sprintf("Log file \"%s\" was not found!", $logs[$id]['path']));
}

if($delete) {
unlink($log);
unlink($logs[$id]['path']);
return $this->redirectToRoute('_log_viewer_list');
}

if (file_exists($log)) {
$log = file_get_contents($log);
$context['log'] = LogView::logToArray($log);
} else {
$context['noLog'] = true;
$reader = new LogReader($logs[$id]['path'], $logs[$id]['date_format'], $logs[$id]['days']);
if(!is_null($logs[$id]['pattern'])){
$reader->getParser()->registerPattern('NewPattern', $logs[$id]['pattern']);
$reader->setPattern('NewPattern');
}

$lines = [];
foreach ($reader as $line){
try{
$lines[] = [
'dateTime' => $line['date'],
'type' => $line['logger'],
'level' => $line['level'],
'message' => $line['message'],
];
}catch (Exception $e){
continue;
}

}

if(!empty($lines)){
$context['log'] = $lines;
}else{
$context['noLog'] = true;
}

return $this->render('@EvotodiLogViewer/logView.html.twig', $context);
}
}
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function getConfigTreeBuilder()
->scalarNode('name')->info("Pretty name to display else file name")->defaultNull()->end()
->integerNode('days')->info('Number of days to pull from log. See ddtraceweb/monolog-parser.')->defaultValue(0)->end()
->scalarNode('pattern')->info('See ddtraceweb/monolog-parser for patterns.')->defaultNull()->end()
->scalarNode('date_format')->info('PHP style date format of log file')->defaultValue('Y-m-d H:i:s')->end()
->end()
->end()
->end()
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/EvotodiLogViewerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$definition = $container->getDefinition('evotodi_log_viewer.log_list_controller');
$definition = $container->getDefinition('evotodi_log_list.log_list_service');
$definition->replaceArgument(1, $config['log_files']);
$definition->replaceArgument(2, $config['show_app_logs']);

Expand Down
143 changes: 0 additions & 143 deletions src/Model/LineView.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/Model/LogList.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/Model/LogView.php

This file was deleted.

Loading

0 comments on commit 2ae4b1f

Please sign in to comment.