Home Backend Development PHP Tutorial How CodeIgniter records error logs

How CodeIgniter records error logs

Jun 02, 2018 am 10:31 AM
codeigniter method log

This article mainly introduces the method of CodeIgniter recording error logs, analyzes the file structure and corresponding functions of the CodeIgniter framework in detail, analyzes the implementation techniques of CodeIgniter framework recording error logs with examples, and analyzes hidden index files and data. The summary of transmission and other techniques is very comprehensive. Friends who need it can refer to

. The example in this article describes the method of CodeIgniter recording error logs. Share it with everyone for your reference, the details are as follows:

CI workflow:

All entries are entered from index.php in the root directory, determine the directory where the application is located Finally, load the codeigniter/CodeIgniter.php file, which will load the following files sequentially to execute the entire process.

index.php: Detect the file path and load the codeigniter.php file

codeigniter.php: Load the Common/constants.... file . Get file mode, set timer, instantiate class (error class, extension class, hook class, system extension, configuration class, encoding class, routing class, process class, output class, security class, language class, controller), load Request method, render output view.

A class of CodeIgniter will be saved as a php file. The class name has the same name as the file name. Its core application class will have "CI_" in front of the class name.

system/core/common.php: Contains detecting php version, file permissions, loading core classes, obtaining configuration parameters, loading exception/error classes, obtaining http request status, etc. Public functions

application/config/constants.php: Set file permission constants, application macro definition files

system/core/Benchmark.php:Used to record execution time

system/core/Hooks.php: Detect whether there is a hook object call

system/core/Config.php: Provide methods for managing configuration files and detecting application/config/config.php parameters

application/config/config.php: Configuring global parameters

system/core/URI.php: Parse url parameters

system/core/Router.php: Detect routing configuration and parse HTTP requests to determine who will handle it

system/core/Output.php: Check whether there is a cache file, and if it exists, output the content directly.

system/core/Input.php: Filter HTTP requests and any user-submitted data

system/core/Long.php: Initialization Prompt language variable

system/core/controller.php:Control output class

Record error log:

Default program Do not record error logs. If necessary, you can set it:

1. Set it in application/config/config.php:

$config['log_threshold'] = 1//(可设置:1/2/3/4)
Copy after login

If it is 0, it means no error log is output. For details, please see the introduction inside;

2. Call the global function log_message('level','message') on the page where errors need to be written. There are three levels, one The first one is error, that is, the PHP running error. The second one is debug, system debugging. CI itself has also added its own system debug on many pages. The third one is info, which introduces some messages during operation. The message content is written by yourself;

3. By default, the error log is stored in application/logs/log-[time].php. It stores files by date. For example: log-2011-6-26 means that today’s log content is stored. Generally, it is hidden. The log content must move this address. You can set the path in $config['log_path']. It is best to use the complete path information as required.

Set your own global variables/configuration:

Sometimes you need to define your own full process variables for use in other places, such as customized sessions, etc., in CI This job is also very easy.

1. Create your own config file in application/config/, and pay attention to the location of the file. For example, create your own configuration file mysetting.php, content,

$config['try'] = 'this is my trying';
Copy after login

2. Use $this->config where you need to call custom global variables. ->load('settingfile') function, for example:

$this->config->load('mysetting');
Copy after login

If necessary, it can also be set through application/config/autoload.php Load automatically.

3. Next, use the

$this->config->item('varname')
Copy after login

function on the same page, for example: $this->config->item(' try'); will output: this is my trying;
As can be seen from the above, The function call in CI is in the form of: $this->filename. It can also be seen that CI looks at the entire system. into a large class, and then obtain the corresponding methods through loading, inheritance, etc.
More custom variable reference: http://codeigniter.org.cn/user_guide/libraries/config.html

Hide index.php and load external files:

其实不管是在用CI还是ZF都有同样一个问题,就是路径的问题。前期,我在用ZF做CMS时,我在.htaccess文件中设置了如遇到js,css,img等资源文件都不重定向。但今天在用CI时,却忘记了,弄了半天都没搞好,登陆CI的中国官方网,终于在论坛高手的帮助下把问题觖决了,在这里把它贴出来,供大家分享。

首先,隐藏url中的index.php文件,这样访问其它目录的时候就不会有http://www.xxx.com/index.php/xxx的样式出现,面是直接http://www.xxx.com/xxx形式,在根目录.htaccess文件里设置(作用是隐藏index.php,有时index.php可能不在根目录,则htaccess须移到index.php所在目录),如下:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)
#这里排除了images、js、css目录及index.php、robots.txt文件
RewriteRule ^(.*)$ index.php/$1 [L]
Copy after login

这里JS,CSS,IMG等资源文件夹与SYSTEM文件夹放在同一级下,独立放置的好处是不用受htaccess的限制,因为htaccess文件写明Deny from all,即拒绝访问。打开application/config/config.php改写配置:

$config['base_url'] = "http://127.0.0.1/";
$config['index_page'] = "index.php";
Copy after login

如果

$config['base_url'] = http://127.0.0.1;
Copy after login

后面没加'/',则在model_rewrite最后一行应写RewriteRule ^(.*)$ /index.php/$1 [L],在index.php前加一个'/'。然后在JS文件夹中建立ajax.js文件,我在VIEW层中的文件为index.html。这样我要引入JS时,可以用CI自带的BASE_URL来设置,如下:

在controllers里相关控制网页里添加(在其它load之前):

$this->load->helper('url');
Copy after login

在views表现的index.html里:

复制代码 代码如下:

注:这里url是网站相对URL(好处是可以更改根目录后相对地址不用改变)

这里js文件夹没有重定向,所以可以正常访问,而如果是受限制的页面则比较麻烦了。

好了,CI中引入外部的JS与CSS就这么简单。

注别的说明:“ RewriteCond $1 !^(index\.php|images|js|css|robots\.txt) ”这里代码的意思是:任意你想访问的资源都不被重定向时,都可写在这里。有时,网站没有加载CSS,JS(它的路径都是正确的)时,都是被重定向了,这要注意。

具体可查看CI的中国官论坛 http://codeigniter.org.cn/user_guide/helpers/url_helper.html,URL辅助函数一节,
http://codeigniter.org.cn/user_guide/general/urls.html,url设置,
http://codeigniter.org.cn/forums/thread-4-1-2.html,Hex关于隐藏index.php的说明,但他在model_rewrite用了index\\.php,我觉得用双反斜杠有误。

(另外:特别谢谢CI中国官论坛上的Hex 与visvoy )

数据间的传输:

1、将数据从控制器传入视图

由于控制器controllers在ci中扮演交通警察的角色,其是一个大类,而视图view作为controller类中的一个函数中的函数,所以view可以使用controller中的属性。所以可以这样写:

Controller类Test

class Test extends CI_Controller {
 public static $test2=''; //定义一个属性
 public function __construct(){
 parent::__construct();
 self::$test2 = $this->load->view('new','',true); //给$test2这个属性赋值
 }
 public function index() {
 $this->load->helper('url');
 $this->load->view('anchor');
 }
}
Copy after login

View.php

<?php
echo Test::$test2; //直接使用类中的值
?>
Copy after login

这种直接使用controllers类中的值的方法虽然可行,却不是ci所提倡的。一般来说在controller中使用$this->load->view()的时候可以通过参数传值给view视图:

function index()
{
 $data[&#39;css&#39;] = $this->css;
 $data[&#39;base&#39;] = $this->base;
 $data[&#39;mytitle&#39;] = &#39;Welcome to this site&#39;;
 $data[&#39;mytext&#39;] = "Hello, $name, now we&#39;re getting dynamic!";
 $this->load->view(&#39;testview&#39;, $data); //$data通过参数传递到view
}
Copy after login

这里,把需要传递的数值加入至$data数组,ci在核心类中给自动使用extract()函数把数组“解压”出来,成为一个个变量。所以在view中可以直接这样使用变量:

echo $css;
Copy after login

2、模型与视图的交互

在ci中模型总是用以处理数据,模型中数据处理也是通过controller中转到view,所以最好不要试图模型直接与视图联系。手册中有这样一个例子:

class Blog_controller extends CI_Controller {
 function blog() {
 $this->load->model(&#39;Blog&#39;); //载入模型
 $data[&#39;query&#39;] = $this->Blog->get_last_ten_entries(); //使用模型中的方法,将返回值存入$data数组
 $this->load->view(&#39;blog&#39;, $data); //像上例一样,通过参数传给视图view
 }
}
Copy after login

相关推荐:

CI框架(CodeIgniter)实现的数据库增删改查操作总结

CI框架(CodeIgniter)实现的导入、导出数据操作

The above is the detailed content of How CodeIgniter records error logs. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

Colorful motherboards enjoy high popularity and market share in the Chinese domestic market, but some users of Colorful motherboards still don’t know how to enter the bios for settings? In response to this situation, the editor has specially brought you two methods to enter the colorful motherboard bios. Come and try it! Method 1: Use the U disk startup shortcut key to directly enter the U disk installation system. The shortcut key for the Colorful motherboard to start the U disk with one click is ESC or F11. First, use Black Shark Installation Master to create a Black Shark U disk boot disk, and then turn on the computer. When you see the startup screen, continuously press the ESC or F11 key on the keyboard to enter a window for sequential selection of startup items. Move the cursor to the place where "USB" is displayed, and then

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

Summary of methods to obtain administrator rights in Win11 Summary of methods to obtain administrator rights in Win11 Mar 09, 2024 am 08:45 AM

A summary of how to obtain Win11 administrator rights. In the Windows 11 operating system, administrator rights are one of the very important permissions that allow users to perform various operations on the system. Sometimes, we may need to obtain administrator rights to complete some operations, such as installing software, modifying system settings, etc. The following summarizes some methods for obtaining Win11 administrator rights, I hope it can help you. 1. Use shortcut keys. In Windows 11 system, you can quickly open the command prompt through shortcut keys.

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Detailed explanation of Oracle version query method Detailed explanation of Oracle version query method Mar 07, 2024 pm 09:21 PM

Detailed explanation of Oracle version query method Oracle is one of the most popular relational database management systems in the world. It provides rich functions and powerful performance and is widely used in enterprises. In the process of database management and development, it is very important to understand the version of the Oracle database. This article will introduce in detail how to query the version information of the Oracle database and give specific code examples. Query the database version of the SQL statement in the Oracle database by executing a simple SQL statement

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

In today's society, mobile phones have become an indispensable part of our lives. As an important tool for our daily communication, work, and life, WeChat is often used. However, it may be necessary to separate two WeChat accounts when handling different transactions, which requires the mobile phone to support logging in to two WeChat accounts at the same time. As a well-known domestic brand, Huawei mobile phones are used by many people. So what is the method to open two WeChat accounts on Huawei mobile phones? Let’s reveal the secret of this method. First of all, you need to use two WeChat accounts at the same time on your Huawei mobile phone. The easiest way is to

See all articles