During development, in order to facilitate us to debug the program and record information, we will write the necessary information into a file, which is the log file. The yii framework provides us with a good method of writing logs. Let's take a look at it together. Take a look.
#How does the yii framework write logs?
1. To use log with Yii, you first need to modify the configuration file:
'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], 'logVars' => ['_GET'], ], ], ],
targets parameter explanation:
● class specifies the use of files to save logs
● Levels specifies saving error and warning logs
● logVars specifies saving get parameters
If logVars is not specified, GET POST COOKIE will be saved SESSION SERVER and other parameters, which will affect our viewing, we can modify it to
'logVars' => ['*'], // 只记录message
2, use the following:
Yii::error($message); Yii::warning($message);
Write the log to Different files
Method 1: First assign the log file address where the log needs to be recorded, and then write the log
Yii::$app->log->targets[0]->logFile = Yii::getAlias('@runtime').DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'app2.log'; Yii::warning($message);
Method 2 (recommended): Modify the configuration file main.php
'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], 'logVars' => ['*'], //'categories' => ['application'], //'logFile' => '@runtime/logs/app.log', ], [ 'class' => 'yii\log\FileTarget', 'categories' => ['pay'], 'levels' => ['error', 'warning'], 'logVars' => ['*'], 'logFile' => '@runtime/logs/pay.log', ], [ 'class' => 'yii\log\FileTarget', 'categories' => ['order'], 'levels' => ['error', 'warning'], 'logVars' => ['*'], 'logFile' => '@runtime/logs/order.log', ], ], ],
Use:
Yii::warning($message,'pay')
The message here will be recorded in pay.log, and of course it will also be recorded in the default In
in app.log, you can uncomment this code: //'categories' => ['application'],, so that it will only be recorded in the respective logs. .
But this will also cause some error information not to be recorded in app.log.
The above is the detailed content of How to write logs in Yii framework?. For more information, please follow other related articles on the PHP Chinese website!

Yii is still competitive in modern development. 1) High performance: adopts lazy loading and caching mechanisms. 2) Security: Built-in CSRF and SQL injection protection. 3) Extensibility: Component-based design is easy to expand and customize.

The Yii community provides rich support and resources. 1. Visit the official website and GitHub to get the documentation and code. 2. Use official forums and StackOverflow to solve technical problems. 3. Report bugs and make suggestions through GitHubIssues. 4. Use documents and tutorials to learn the Yii framework.

Yii is a high-performance PHP framework designed for fast development and efficient code generation. Its core features include: MVC architecture: Yii adopts MVC architecture to help developers separate application logic and make the code easier to maintain and expand. Componentization and code generation: Through componentization and code generation, Yii reduces the repetitive work of developers and improves development efficiency. Performance Optimization: Yii uses latency loading and caching technologies to ensure efficient operation under high loads and provides powerful ORM capabilities to simplify database operations.

Yii is a high-performance framework based on PHP, suitable for rapid development of web applications. 1) It adopts MVC architecture and component design to simplify the development process. 2) Yii provides rich functions, such as ActiveRecord, RESTfulAPI, etc., which supports high concurrency and expansion. 3) Using Gii tools can quickly generate CRUD code and improve development efficiency. 4) During debugging, you can check configuration files, use debugging tools and view logs. 5) Performance optimization suggestions include using cache, optimizing database queries and maintaining code readability.

YiiremainspopularbutislessfavoredthanLaravel,withabout14kGitHubstars.ItexcelsinperformanceandActiveRecord,buthasasteeperlearningcurveandasmallerecosystem.It'sidealfordevelopersprioritizingefficiencyoveravastecosystem.

Yii is a high-performance PHP framework that is unique in its componentized architecture, powerful ORM and excellent security. 1. The component-based architecture allows developers to flexibly assemble functions. 2. Powerful ORM simplifies data operation. 3. Built-in multiple security functions to ensure application security.

Yii framework adopts an MVC architecture and enhances its flexibility and scalability through components, modules, etc. 1) The MVC mode divides the application logic into model, view and controller. 2) Yii's MVC implementation uses action refinement request processing. 3) Yii supports modular development and improves code organization and management. 4) Use cache and database query optimization to improve performance.

Strategies to improve Yii2.0 application performance include: 1. Database query optimization, using QueryBuilder and ActiveRecord to select specific fields and limit result sets; 2. Caching strategy, rational use of data, query and page cache; 3. Code-level optimization, reducing object creation and using efficient algorithms. Through these methods, the performance of Yii2.0 applications can be significantly improved.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool