PHP Debug Log method

Source: Internet
Author: User
Tags php debug

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.