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!

TobuildrobustwebapplicationswithYii,mastertheseskills:1)MVCarchitectureforstructuringapplications,2)ActiveRecordforefficientdatabaseoperations,3)WidgetsystemforreusableUIcomponents,4)Validationandsecuritymeasures,5)Cachingforperformanceoptimization,a

TobecomeasuccessfulYiideveloper,youneed:1)PHPmastery,2)understandingofMVCarchitecture,3)Yiiframeworkproficiency,4)databasemanagementskills,5)front-endknowledge,6)APIdevelopmentexpertise,7)testinganddebuggingcapabilities,8)versioncontrolproficiency,9)

ThemostcommonerrorsinYiiframeworkare"UnknownProperty","InvalidConfiguration","ClassNotFound",and"ValidationErrors".1."UnknownProperty"errorsoccurwhenaccessingnon-existentproperties;ensurepropertiesexi

The key skills that European Yii developers need to possess include: 1. Yii framework proficiency, 2. PHP proficiency, 3. Database management, 4. Front-end skills, 5. RESTful API development, 6. Version control system, 7. Testing and debugging, 8. Security knowledge, 9. Agile methodology, 10. Soft skills, 11. Localization and internationalization, 12. Continuous learning, these skills make developers stand out in the European market.

Yes,theYiicommunityisstillactiveandvibrant.1)TheofficialYiiforumremainsaresourcefordiscussionsandsupport.2)TheGitHubrepositoryshowsregularcommitsandpullrequests,indicatingongoingdevelopment.3)StackOverflowcontinuestohostYii-relatedquestionsandhigh-qu

Migratingalaravel Projecttoyiiishallingbutachieffable WITHIEFLEFLANT.1) Mapoutlaravel component likeroutes, Controllers, Andmodels.2) Translatelaravel's SartisancommandeloequentTooyii's giiandetiverecordeba

Soft skills are crucial to Yii developers because they facilitate team communication and collaboration. 1) Effective communication ensures that the project is progressing smoothly, such as through clear API documentation and regular meetings. 2) Collaborate to enhance team interaction through Yii's tools such as Gii to improve development efficiency.

Laravel'sMVCarchitectureoffersenhancedcodeorganization,improvedmaintainability,andarobustseparationofconcerns.1)Itkeepscodeorganized,makingnavigationandteamworkeasier.2)Itcompartmentalizestheapplication,simplifyingtroubleshootingandmaintenance.3)Itse


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
