codeigniter
by Seun Matt
通過Seun Matt
如何在瀏覽器中查看CodeIgniter日志文件 (How to View CodeIgniter Log Files in the Browser)
Just like any other page, it is now possible to read CodeIgniter log files in the browser. My Sweet Goodness!
與其他頁面一樣,現在可以在瀏覽器中讀取CodeIgniter日志文件。 我的天哪!
I began using CodeIgniter in my day to day coding after joining an awesome company. The company’s tech stack includes the PHP Framework — among others. Hitherto now, I’ve used (and still use) Laravel to build some awesome apps.
加入一家很棒的公司后,我開始使用CodeIgniter進行日常編碼。 該公司的技術堆棧包括PHP框架等。 到目前為止,我已經使用( 并且仍然使用 )Laravel構建了一些很棒的應用程序。
Laravel has a great logging system that is simple and elegant. Furthermore, there’s a library for showing the logs in the browser. Being able to read the logs in the browser is good for application debugging and insight. Especially in a production environment.
Laravel有一個很棒的日志系統,簡單而優雅。 此外,還有一個用于在瀏覽器中顯示日志的庫 。 能夠在瀏覽器中讀取日志有助于進行應用程序調試和洞察。 特別是在生產環境中。
So here I am in the world of CodeIgniter and couldn’t find an equivalent library to read my logs for debugging and insight.
因此,我在CodeIgniter的世界中,找不到等效的庫來讀取我的日志以進行調試和分析。
So I took up the challenge and created my first Open Source project of the year — codeigniter-log-viewer.
因此,我接受了挑戰,并創建了本年度的第一個開源項目-codeigniter-log-viewer 。
用法 (Usage)
First, let’s add it to a dependency. We can do that by executing:
首先,讓我們將其添加到依賴項中。 我們可以通過執行以下操作來做到這一點:
composer require seunmatt/codeigniter-log-viewer
Then, we can create a CodeIgniter application controller, LogViewerController.php:
然后,我們可以創建一個CodeIgniter應用程序控制器LogViewerController.php :
private $logViewer;
public function __construct() { $this->logViewer = new \CILogViewer\CILogViewer(); //...}
public function index() { echo $this->logViewer->showLogs(); return;}
What we did is to instantiate $logViewer in the constructor and then echo the result of showLogs() in the index() function.
我們要做的是在構造函數中實例化$ logViewer ,然后在index()函數中回顯showLogs()的結果。
The showLogs() method of codeigniter-log-viewer will parse the content of the log files in application/logs . It will return it for display on the browser.
codeigniter-log-viewer的showLogs()方法將解析application / logs中日志文件的內容。 它將返回顯示在瀏覽器上。
Finally, we can map any route of our choice to the index() we created above. This can be done by adding an entry to the $route array in application/config/routes.php:
最后,我們可以將選擇的任何路徑映射到上面創建的index() 。 這可以通過在application / config / routes.php中的$ route數組中添加一個條目來完成:
$route['logs'] = "logViewerController/index";
Now we can visit /logs on the browser and see all the log files there. It’s also possible to delete and download the log files.
現在,我們可以在瀏覽器上訪問/ logs并在其中查看所有日志文件。 也可以刪除和下載日志文件。
Note: It is advisable to use a protected route in production environment to avoid general public access.
注意 :建議在生產環境中使用受保護的路由,以避免一般公眾訪問。
這個怎么運作 (How it works)
Internally, the library read the name of all the log files that are available in the default logs directory into an array and reverse it. If no file is specified in the URL query parameters, the latest log file is processed for display by default.
在內部,該庫將默認日志目錄中可用的所有日志文件的名稱讀取到一個數組中,并將其反轉。 如果在URL查詢參數中未指定文件,則默認情況下將處理最新的日志文件以進行顯示。
Processing of a log file for display involves reading its contents, using regex to determine the log level and the CSS class and icon of each entry.
處理用于顯示的日志文件涉及讀取其內容,使用正則表達式確定日志級別以及每個條目CSS類和圖標。
Each entry is also checked to know if it’s a new log line or a continuation of the previous line (due to a newline character).
還檢查每個條目以知道它是新的日志行還是上一行的繼續( 由于換行符 )。
Finally, the log entries are processed into HTML content that is then sent to the browser for display.
最后,將日志條目處理為HTML內容,然后將其發送到瀏覽器進行顯示。
The complete source code is available on Github if you want to play around with it or/and adapt it for use in other frameworks.
如果您想使用它或/和使其適應其他框架,請在Github上獲得完整的源代碼。
結論 (Conclusion)
Now it’s easier and faster to debug CodeIgniter application — even in production. Spread the word around to friends and colleagues at work.
現在,調試CodeIgniter應用程序變得更加輕松快捷,即使在生產中也是如此。 向工作中的朋友和同事傳話。
I want to hear about your experience (and opinions) of using the library in the comment section. Thanks!
我想在評論部分中聽到您使用該庫的經驗( 和見解 )。 謝謝!
Visit the Github Link
訪問Github鏈接
翻譯自: https://www.freecodecamp.org/news/how-to-view-codeigniter-log-files-in-the-browser-e4ec7a9e8b23/
codeigniter