├── registration.php ├── etc ├── module.xml └── adminhtml │ ├── routes.xml │ └── menu.xml ├── Controller └── Adminhtml │ ├── Download │ ├── GetFile.php │ └── AbstractLog.php │ ├── View │ └── Index.php │ └── Grid │ └── Index.php ├── README.md ├── view └── adminhtml │ ├── layout │ ├── logviewer_grid_index.xml │ └── logviewer_view_index.xml │ └── templates │ ├── view.phtml │ └── grid.phtml ├── Block ├── View │ └── Index.php └── Index.php └── Helper └── Data.php /registration.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Controller/Adminhtml/Download/GetFile.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LogViewer 2 | 3 | Simple module to allow admins to download log files via System > LogViewer. 4 | 5 | * Ordered by last modified 6 | * Limited to 30 last logs 7 | * Preview last 10 lines of log file 8 | 9 | ### Preview 10 | 11 | **Main Log Grid** 12 | ![Preview Image](https://i.imgur.com/yJLiScN.png "Preview of LogViewer") 13 | 14 | **Log Preview** 15 | ![Preview Image](https://i.imgur.com/QD38PHS.png "Preview of LogViewer") -------------------------------------------------------------------------------- /view/adminhtml/layout/logviewer_grid_index.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /view/adminhtml/layout/logviewer_view_index.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /view/adminhtml/templates/view.phtml: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | getFileName() . __(' - last 10 lines') ?> 5 |
6 |
7 | 8 |
9 |
10 |
-------------------------------------------------------------------------------- /etc/adminhtml/menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Block/View/Index.php: -------------------------------------------------------------------------------- 1 | logDataHelper = $logDataHelper; 23 | parent::__construct($context, $data); 24 | } 25 | 26 | public function getLogFile() 27 | { 28 | $params = $this->_request->getParams(); 29 | return $this->logDataHelper->getLastLinesOfFile($params[0], 10); 30 | } 31 | 32 | public function getFileName() 33 | { 34 | $params = $this->_request->getParams(); 35 | return $params[0]; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Controller/Adminhtml/View/Index.php: -------------------------------------------------------------------------------- 1 | resultPageFactory = $resultPageFactory; 23 | } 24 | 25 | /** 26 | * 27 | * @return \Magento\Framework\View\Result\Page 28 | */ 29 | public function execute() 30 | { 31 | $this->_view->loadLayout(); 32 | //$this->_setActiveMenu('Magento_Backend::system'); 33 | $this->_view->getPage()->getConfig()->getTitle()->prepend(__('View Log')); 34 | $this->_view->renderLayout(); 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /Controller/Adminhtml/Grid/Index.php: -------------------------------------------------------------------------------- 1 | resultPageFactory = $resultPageFactory; 23 | } 24 | 25 | /** 26 | * 27 | * @return \Magento\Framework\View\Result\Page 28 | */ 29 | public function execute() 30 | { 31 | 32 | $this->_view->loadLayout(); 33 | $this->_setActiveMenu('Magento_Backend::system'); 34 | $this->_view->getPage()->getConfig()->getTitle()->prepend(__('Log Viewer')); 35 | $this->_view->renderLayout(); 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /Block/Index.php: -------------------------------------------------------------------------------- 1 | logDataHelper = $logDataHelper; 23 | parent::__construct($context, $data); 24 | } 25 | 26 | public function getLogFiles() 27 | { 28 | return $this->logDataHelper->buildLogData(); 29 | } 30 | 31 | public function downloadLogFiles($fileName) 32 | { 33 | return $this->getUrl('logviewer/download/getfile', [$fileName]); 34 | } 35 | 36 | public function previewLogFile($fileName) 37 | { 38 | return $this->getUrl('logviewer/view/index', [$fileName]); 39 | } 40 | } -------------------------------------------------------------------------------- /Controller/Adminhtml/Download/AbstractLog.php: -------------------------------------------------------------------------------- 1 | fileFactory = $fileFactory; 18 | parent::__construct($context); 19 | } 20 | 21 | public function execute() 22 | { 23 | $param = $this->getRequest()->getParams(); 24 | $filePath = $this->getFilePathWithFile($param[0]); 25 | 26 | $fileName = basename($filePath); 27 | try { 28 | return $this->fileFactory->create( 29 | $fileName, 30 | [ 31 | 'type' => 'filename', 32 | 'value' => $filePath 33 | ] 34 | ); 35 | } catch (\Exception $e) { 36 | throw new NotFoundException(__($e->getMessage())); 37 | } 38 | } 39 | 40 | /** 41 | * @param $filename 42 | * @return string 43 | */ 44 | abstract protected function getFilePathWithFile($filename); 45 | } -------------------------------------------------------------------------------- /view/adminhtml/templates/grid.phtml: -------------------------------------------------------------------------------- 1 | getLogFiles(); ?> 2 | 3 |
4 |
5 |
6 | 7 |
8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 27 | 28 | 29 |
FilenameDirectorySizeLast ModifiedActions
var/log 24 | 25 | 26 |

30 |
31 |
32 |
33 | -------------------------------------------------------------------------------- /Helper/Data.php: -------------------------------------------------------------------------------- 1 | directoryList = $directoryList; 20 | parent::__construct( 21 | $context 22 | ); 23 | } 24 | 25 | /** 26 | * @return string 27 | */ 28 | public function getPath() 29 | { 30 | $rootPath = $this->directoryList->getRoot(); 31 | $path = 32 | $rootPath . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'log'. DIRECTORY_SEPARATOR; 33 | return $path; 34 | } 35 | 36 | /** 37 | * @return array 38 | */ 39 | protected function getLogFiles() 40 | { 41 | $path = $this->getPath(); 42 | return scandir($path); 43 | } 44 | 45 | /** 46 | * @param $bytes 47 | * @param int $precision 48 | * @return string 49 | */ 50 | protected function filesizeToReadableString($bytes, $precision = 2) 51 | { 52 | $units = array('B', 'KB', 'MB', 'GB', 'TB'); 53 | 54 | $bytes = max($bytes, 0); 55 | $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 56 | $pow = min($pow, count($units) - 1); 57 | 58 | $bytes /= pow(1024, $pow); 59 | 60 | return round($bytes, $precision) . ' ' . $units[$pow]; 61 | } 62 | 63 | /** 64 | * @return array 65 | */ 66 | public function buildLogData() 67 | { 68 | $maxNumOfLogs = 30; 69 | $logFileData = []; 70 | $path = $this->getPath(); 71 | $files = $this->getLogFiles(); 72 | 73 | //remove rubbish from array 74 | array_splice($files, 0, 2); 75 | 76 | //build log data into array 77 | foreach ($files as $file) { 78 | $logFileData[$file]['name'] = $file; 79 | $logFileData[$file]['filesize'] = $this->filesizeToReadableString((filesize($path . $file))); 80 | $logFileData[$file]['modTime'] = filemtime($path . $file); 81 | $logFileData[$file]['modTimeLong'] = date("F d Y H:i:s.", filemtime($path . $file)); 82 | } 83 | 84 | //sort array by modified time 85 | usort($logFileData, function ($item1, $item2) { 86 | return $item2['modTime'] <=> $item1['modTime']; 87 | }); 88 | 89 | //limit the amount of log data $maxNumOfLogs 90 | $logFileData = array_slice($logFileData, 0, $maxNumOfLogs); 91 | 92 | return $logFileData; 93 | } 94 | 95 | public function getLastLinesOfFile($fileName, $numOfLines) 96 | { 97 | $path = $this->getPath(); 98 | $fullPath = $path . $fileName; 99 | exec('tail -'. $numOfLines . ' ' . $fullPath, $output); 100 | return implode($output); 101 | } 102 | } --------------------------------------------------------------------------------