


PHP development framework Yii Framework tutorial (3) Add logs to the application
In the process of developing applications, debugging is also a very important link. In addition to the real-time debugging supported by IDE (such as VS.PHP or the debugging function supported by IDE), adding appropriate debugging information to the web application is also very useful. Method, anyone who has developed Java or .Net applications is familiar with log4XX. Yii Framework also provides a similar Log function, Yii::log, which appears as a built-in component of CWebApplication. It can be configured through a configuration file (the configuration in Yii is usually protected/config/main.php).
The previous article Yii Framework Development Concise Tutorial (2) Yii Web Application Basics briefly describes the application components.
In addition to the Log component, Yii predefines a series of core application components to provide functions used in common web applications. For example, the request component is used to parse user requests and provide information such as URLs, cookies, etc. By configuring the properties of these core components, we can modify Yii's default behavior in almost all aspects.
Below we list the core components predefined by CWebApplication.
assetManager: CAssetManager - manages the release of private resource files.
authManager: CAuthManager - Manages role-based access control (RBAC).
cache: CCache - Provides data caching function. Note that you must specify the actual class (eg CMemCache, CDbCache). Otherwise, NULL will be returned when you access this component.
clientScript: CClientScript - Manages client-side scripts (javascripts and CSS).
coreMessages: CPhpMessageSource - Provides translation of core messages used by the Yii framework.
db: CDbConnection - Provides database connection. Note that to use this component you must configure its connectionString property.
errorHandler: CErrorHandler - Handles uncaught PHP errors and exceptions.
format: CFormatter - formatted numerical display. This feature is available starting with version 1.1.0.
messages: CPhpMessageSource - Provides message translations used in Yii applications.
request: CHttpRequest - Provides information about the user's request.
securityManager: CSecurityManager - Provides security-related services, such as hashing, encryption.
session: CHttpSession - Provides session-related functions.
statePersister: CStatePersister - Provides global state persistence methods.
urlManager: CUrlManager - Provides URL parsing and creation related functions
user: CWebUser - Provides identification information of the current user.
themeManager: CThemeManager - manages themes. These components will be introduced step by step in subsequent tutorials. The basic method of using the Log function is given below: Here we modify the Yii Framework Development Concise Tutorial (1) The first application is Hello World. Add a log to it. 1. Create the configuration file protected/config/main.php. If you want to write the log to a file, you can use the following configuration:
// This is the main Web application configuration. Any writable// CWebApplication properties can be configured here.return array( // preloading 'log' component'preload'=>array('log'), // application components'components'=>array( 'log'=>array('class'=>'CLogRouter','routes'=>array(array('class'=>'CFileLogRoute','levels'=>'info,error, warning',), ),),), );
CLogRouter The information recorded through Yii::log or Yii::trace is stored in memory. We usually need to display them in a browser window, or save them to some persistent storage such as files or emails. This is called information routing.
Generally when using the Log function, you need to preload the Log component, which is configured through preload. Preload allows the application to load some modules in advance. Tip: By default, application components are created as needed . This means that a component will only be created if it is accessed. Therefore, the overall performance of the system will not be degraded due to the configuration of many components. Some application components (such as CLogRouter) are created whether they are used or not. In this case, we list the IDs of these components in the application's configuration file: preload.
2. Modify the entry script index.php and configure the main application instance to use the newly created configuration file.
$yii='C:/yiiframework/yii.php'; // remove the following line when in production mode defined('YII_DEBUG') or define('YII_DEBUG',true); $config=dirname(__FILE__).'/protected/config/main.php'; require_once($yii); Yii::createWebApplication($config)->run();3. 创建protected/runtime
Because the default directory where log files are written is protected/runtime, the runtime directory must be created in advance, otherwise Yii will display the log directly on the web page.
4. In this way, you can use Yii::log or Yii::trace to add logs to the application.
The difference is that the latter only records information when the application is running in debug mode.
Yii::log($message, $level, $category); Yii::trace($message, $category);
When recording information, we need to specify its category and level. The category is a string in a format similar to a path alias. For example, if a message is recorded in CController, we can use system.web.CController as the classification. The information level should be one of the following values:
trace: This is the level used in Yii::trace. It is used to track the execution flow of a program during development.
info: This is used to record ordinary information.
profile: This is the performance overview (profile). More detailed instructions will follow shortly.
warning: This is used for warning information.
error: This is used for fatal error messages.
Yii's logs are classified and filtered by Level and Category. As we mentioned, multiple levels or categories should be connected with commas.
由于 信息分类是类似 xxx.yyy.zzz 格式的,我们可以将其视为一个分类层级。 具体地,我们说 xxx 是 xxx.yyy 的父级,而 xxx.yyy 又是 xxx.yyy.zzz 的父级。 这样我们就可以使用 xxx.* 表示分类 xxx 及其所有的子级和孙级分类。
有了以 上知识,我们修改SiteController的actionIndex,添加一行日志。
public function actionIndex() { Yii::log("action","info","site.action"); $this->render("index"); }
5. 运行应用,可以看到Yii在protected/runtime 创建了一个application.log
其内容如下:
2012/12/11 21:23:38 [info] [site.action] action
注: 日志存放的位置和文件名都可以通过配置来修改,一般情 况使用缺省值就可以了,和log4X 类似,应用的日志可以通过路由同时写到多个目的地(文件,Email等)这些多可以通过配置 来实现,具体可以参见日志记录。
以上就是PHP开发框架Yii Framework教程(3) 为应用添加日志的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query
