Yii2 log processing and yii2 log processing
Recently, a new PHP project was developed, which finally broke away from the claw of a framework (previously tortured) and chose the Yii2 framework, which is like a storm in the world. Logs are essential for Running code of each project. during development, you step on the Yii2 log management pitfalls. You have seen many online configurations of Yii2 logs, summary of Yii2's log processing today.
1. First, let's take a look at the log Configuration:
1 return [2 'tracelevel '=> YII_DEBUG? 3: 0, 3 'targets' => [// you can configure multiple log 4 [5' class' => 'yii \ log \ FileTarget ', // Yii2 log processing class 6 'levels' => ['error', 'warning', 'info'], // set the log record level to 7'categories '=> ['user'], // custom log classification 8' maxFileSize' => 1024*20, // set the file size, in the unit of k, 9 'logfile' => '@ runtime /.. /logs/user '. date ('ymmd'), // custom file path (generally, project logs are routed to other paths on the server. You need to modify the permission of the corresponding directory ~) 10 'logvars' => ['_ Post'], // capture request parameter 11 'filemode' => 0775, // set log file permissions 12' maxLogFiles' => 100, // maximum number of file names for the same file: 13 'rotatebycopy' => false, // whether to copy rotate14 'prefix' => function () {// custom callback Method for log format 15 if (Yii: $ app === null) {16 return ''; 17} 18 $ request = Yii :: $ app-> getRequest (); 19 $ ip = $ request instanceof Request? $ Request-> getUserIP (): '-'; 20 $ controller = Yii ::$ app-> controller-> id; 21 $ action = Yii :: $ app-> controller-> action-> id; 22 return "[$ ip] [$ controller-$ action]"; 23}, 24], 25];
2. Logging
Yii: trace (): records a message to track how a piece of code runs. This is mainly used during development.
Yii: info (): record a message to convey some useful information.
Yii: warning (): records a warning message to indicate exceptions that have occurred.
Yii: error (): records a fatal error, which should be checked as soon as possible.
Eg: Yii: info ('the log content', 'user ');
The second parameter can be a custom log category, which corresponds to the categories field in the configuration file.
3. Log component call
Log configuration is loaded to the Application Object Yii ::$ app in component mode through web. php (basic template web. php advanced template main. php.
4. log splitting
./Vendor/yiisoft/yii2/log/FileTarget. php
1 class FileTarget extends Target 2 {3 public $ logFile; 4 // If the rotation switch is enabled, when the log file size is greater than the file size set by maxFileSize, the log is automatically split into 5 public $ enableRotation = true; 6 public $ maxFileSize = 10240; // in KB 7 // how many files can be split by the same file name 8 public $ maxLogFiles = 5; 9 public $ fileMode; // Log File Permission 10 public $ dirMode = 0775; 11/** 12 * @ var bool Whether to rotate log files by copy and truncate in contrast to rotation by13 * renaming fi Les. ults to 'true' to be more compatible with log tailers and is windows14 * systems which do not play well with rename on open files. rotation by renaming however is15 * a bit faster.16 * 17 * The problem with windows systems where the [rename ()] (http://www.php.net/manual/en/function.rename.php) 18 * function does not work with files that are opened by some process is described in a19 * [comme Nt by Martin Pelletier] (http://www.php.net/manual/en/function.rename.php#102274) in20 * the PHP documentation. by setting rotateByCopy to 'true' you can work21 * around this problem.22 */23 public $ rotateByCopy = true; 24 25/** 26 * Rotates log files.27 */28 protected function rotateFiles () 29 {30 $ file = $ this-> logFile; 31 for ($ I = $ this-> maxLogFiles; $ I> = 0; -- $ I) {32 // $ I = 0 is the original Log file33 $ rotateFile = $ file. ($ I === 0? '':'. '. $ I); 34 if (is_file ($ rotateFile )) {35 // suppress errors because it's possible multiple processes enter into this section36 if ($ I ====$ this-> maxLogFiles) {37 @ unlink ($ rotateFile ); 38} else {39 if ($ this-> rotateByCopy) {40 @ copy ($ rotateFile, $ file. '. '. ($ I + 1); 41 if ($ fp = @ fopen ($ rotateFile, 'A') {42 @ ftruncate ($ fp, 0 ); 43 @ fclose ($ fp); 44} 45 if ($ this-> fileMode! = Null) {46 @ chmod ($ file. '. '. ($ I + 1), $ this-> fileMode); 47} 48} else {49 // use the rename method in linux: 50 @ rename ($ rotateFile, $ file. '. '. ($ I + 1); 51} 52} 53} 54} 55} 56}
5. Log prefix
Prefix: If no configuration is available, the default call is./vendor/yiisoft/yii2/log/Target. php.
1 public function getMessagePrefix($message) 2 { 3 if ($this->prefix !== null) { 4 return call_user_func($this->prefix, $message); 5 } 6 7 if (Yii::$app === null) { 8 return ''; 9 }10 11 $request = Yii::$app->getRequest();12 $ip = $request instanceof Request ? $request->getUserIP() : '-';13 14 /* @var $user \yii\web\User */15 $user = Yii::$app->has('user', true) ? Yii::$app->get('user') : null;16 if ($user && ($identity = $user->getIdentity(false))) {17 $userID = $identity->getId();18 } else {19 $userID = '-';20 }21 22 /* @var $session \yii\web\Session */23 $session = Yii::$app->has('session', true) ? Yii::$app->get('session') : null;24 $sessionID = $session && $session->getIsActive() ? $session->getId() : '-';25 26 return "[$ip][$userID][$sessionID]";27 }
If you want to customize the log format prefix, you can configure the callback function (note: if a specific class is used in the callback, you need to use the "use" keyword to introduce the class at the beginning of the file)