forked from philaturner/LogViewer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add method to convert file size to readable format
- Loading branch information
1 parent
1e3fadb
commit d88a11d
Showing
14 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |