Skip to content

Commit

Permalink
add method to convert file size to readable format
Browse files Browse the repository at this point in the history
  • Loading branch information
philaturner committed Nov 2, 2017
1 parent 1e3fadb commit d88a11d
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Block/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace PhilTurner\LogViewer\Block;

class Index extends \Magento\Framework\View\Element\Template
{
/**
* @var \PhilTurner\LogViewer\Helper\Data
*/
protected $_logDataHelper;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \PhilTurner\LogViewer\Helper\Data $logDataHelper
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\PhilTurner\LogViewer\Helper\Data $logDataHelper,
array $data = []
)
{
$this->_logDataHelper = $logDataHelper;
parent::__construct($context, $data);
}

public function getLogFiles()
{
return $this->_logDataHelper->buildLogData();
}

public function downloadLogFiles($fileName)
{
return $this->getUrl('logviewer/download/getfile', [$fileName]);
}
}
49 changes: 49 additions & 0 deletions Controller/Adminhtml/Download/AbstractLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace PhilTurner\LogViewer\Controller\Adminhtml\Download;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Controller\Adminhtml\System;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\Exception\NotFoundException;
use Zend_Filter_BaseName;

abstract class AbstractLog extends System
{
/**
* @var FileFactory
*/
protected $fileFactory;
public function __construct(Context $context, FileFactory $fileFactory)
{
$this->fileFactory = $fileFactory;
parent::__construct($context);
}

public function execute()
{
$filePath = $this->getFilePath();
$param = $this->getRequest()->getParams();

//handles filenames being passed as params
if ($filePath === null){
$filePath = $this->getFullPath($param[0]);
}

$filter = new Zend_Filter_BaseName();
$fileName = $filter->filter($filePath);
try {
return $this->fileFactory->create(
$fileName,
[
'type' => 'filename',
'value' => $filePath
]
);
} catch (\Exception $e) {
throw new NotFoundException(__($e->getMessage()));
}
}
/**
* @return string
*/
abstract protected function getFilePath();
}
10 changes: 10 additions & 0 deletions Controller/Adminhtml/Download/DebugLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace PhilTurner\LogViewer\Controller\Adminhtml\Download;

class DebugLog extends AbstractLog
{
protected function getFilePath()
{
return 'var/log/debug.log';
}
}
10 changes: 10 additions & 0 deletions Controller/Adminhtml/Download/ExceptionLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace PhilTurner\LogViewer\Controller\Adminhtml\Download;

class ExceptionLog extends AbstractLog
{
protected function getFilePath()
{
return 'var/log/exception.log';
}
}
15 changes: 15 additions & 0 deletions Controller/Adminhtml/Download/GetFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace PhilTurner\LogViewer\Controller\Adminhtml\Download;

class GetFile extends AbstractLog
{
protected function getFilePath()
{
return null;
}

protected function getFullPath($fileName)
{
return 'var/log/' . $fileName;
}
}
10 changes: 10 additions & 0 deletions Controller/Adminhtml/Download/SysLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace PhilTurner\LogViewer\Controller\Adminhtml\Download;

class SysLog extends AbstractLog
{
protected function getFilePath()
{
return 'var/log/system.log';
}
}
39 changes: 39 additions & 0 deletions Controller/Adminhtml/Grid/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace PhilTurner\LogViewer\Controller\Adminhtml\Grid;

class Index extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;

/**
* Constructor
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}

/**
* Load the page defined in view/adminhtml/layout/logviewer_grid_index.xml
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{

$this->_view->loadLayout();
$this->_setActiveMenu('Magento_Backend::system');
$this->_view->getPage()->getConfig()->getTitle()->prepend(__('Log Viewer'));
$this->_view->renderLayout();

}
}
83 changes: 83 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace PhilTurner\LogViewer\Helper;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Data extends AbstractHelper
{
/**
* @var DirectoryList
*/
protected $directoryList;

public function __construct(
Context $context,
DirectoryList $directoryList
) {
$this->directoryList = $directoryList;
parent::__construct(
$context
);
}

public function _getPath()
{
$rootPath = $this->directoryList->getRoot();
$path =
$rootPath . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'log'. DIRECTORY_SEPARATOR;
return $path;
}

protected function _getLogFiles()
{
$path = $this->_getPath();
return scandir($path);
}

protected function convertToMegabytes($bytes, $precision = 2)
{
$units = array('B', 'KB', 'MB', 'GB', 'TB');

$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);

// Uncomment one of the following alternatives
$bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));

return round($bytes, $precision) . ' ' . $units[$pow];
}

public function buildLogData()
{
$maxNumOfLogs = 30;
$logFileData = [];
$path = $this->_getPath();
$files = $this->_getLogFiles();

//remove rubbish from array
array_splice($files, 0, 2);

foreach ($files as $file) {
$logFileData[$file]['name'] = $file;
$logFileData[$file]['filesize'] = $this->convertToMegabytes((filesize($path . $file)));
$logFileData[$file]['modTime'] = filemtime($path . $file);
$logFileData[$file]['modTimeLong'] = date("F d Y H:i:s.", filemtime($path . $file));
//$logFileData[$file]['downloadURL'] = $path . $file;
//$logFileData[$file]['contents'] = file_get_contents($path . $file);
}

//sort array by modified time
usort($logFileData, function ($item1, $item2) {
return $item2['modTimeLong'] <=> $item1['modTimeLong'];
});

//limit the amount of log to return
$logFileData = array_slice($logFileData, 0, $maxNumOfLogs);

return $logFileData;
}
}
6 changes: 6 additions & 0 deletions etc/adminhtml/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="PhilTurner_LogViewer::logs" title="Log Viewer" module="PhilTurner_LogViewer" sortOrder="90" parent="Magento_Backend::system" resource="Magento_Backend::system" />
<add id="PhilTurner_LogViewer::report_logs" title="View Logs" module="PhilTurner_LogViewer" sortOrder="3" parent="PhilTurner_LogViewer::logs" action="logviewer/grid/index" resource="Magento_Backend::system"/>
</menu>
</config>
8 changes: 8 additions & 0 deletions etc/adminhtml/routes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="logviewer" frontName="logviewer">
<module name="PhilTurner_LogViewer" />
</route>
</router>
</config>
4 changes: 4 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PhilTurner_LogViewer" setup_version="0.0.1"/>
</config>
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'PhilTurner_LogViewer',
__DIR__
);
8 changes: 8 additions & 0 deletions view/adminhtml/layout/logviewer_grid_index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="PhilTurner\LogViewer\Block\Index" name="main" template="grid.phtml"/>
</referenceContainer>
</body>
</page>
27 changes: 27 additions & 0 deletions view/adminhtml/templates/grid.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php $logs = $block->getLogFiles(); ?>

<section class="admin__page-section">
<div class="admin__page-section-content">
<div class="admin__page-section-item-title">
<span class="title"><?php echo __('Log Info') ?></span>
</div>
<div class="admin__page-section-item-content">
<table class="admin__table-primary">
<tr>
<th>Filename</th>
<th>Directory</th>
<th>Size</th>
<th>Last Modified</th>
</tr>
<?php foreach($logs as $log) { ?>
<tr>
<td><a href="<?php echo $block->downloadLogFiles($log['name']); ?>"><?php echo $log['name'] ?></a></td>
<td>var/log</td>
<td><?php echo $log['filesize'] ?></td>
<td><?php echo $log['modTimeLong'] ?></td>
</tr>
<?php } ?>
</table><br>
</div>
</div>
</section>

0 comments on commit d88a11d

Please sign in to comment.