1. Demand
When our project is on-line, but there is a bug, debugging becomes extremely important. But sometimes you need to print variables to find bugs. This is where we need to log variables. I have found a lot of PHP code on the Internet, but I always feel that it is not ideal. I hope the following can solve the problem of everyone.
2. Code
/** * Write log, for testing, can be turned off * @param data mixed*/function_logs ($data,$file= ' Logs_ '){ $year=Date("Y"); $month=Date("M"); $dir= './test_logs/'.$year. ‘/‘ .$month. ‘/‘; if(!Is_dir($dir)) { mkdir($dir, 0755,true); } $file=$dir.$file.Date(' y-m-d '). txt; @file_put_contents($file, ‘----------------‘ .Date(' H:i:s '). ‘--------------------‘.Php_eol.Var_export($data,true).Php_eol,file_append);}
Comments:
File_put_contents This method is not necessary, PHP is very common.
Php_eol is a newline character because there are different line breaks in each system. UNIX series with \nwindows series with \ r \ n mac with \ r
Var_export This method is relatively small but useful here. Just like the Var_dump method. Just the format of the output is in PHP code format. outputs or returns a string representation of a variable. You can return the representation of a variable by setting the second argument of the function to TRUE
.
PHP Debug Log method