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