PHP debugging function and logging function sharing, debugging logging _ PHP Tutorial

Source: Internet
Author: User
PHP debugging functions and logging functions are shared and debugging logs are recorded. PHP debugging functions and logging functions are shared. debugging logs are used to record the development process of website programs, and Operation logs are also required in the release phase, ease of problem discovery and event restoration PHP debugging functions and logging functions sharing, debugging logging

Debugging is often required during Website program development, and running logs need to be recorded during the release phase to facilitate fault discovery and event restoration. This requires debugging and logging.

The following are the functions used for debugging and the functions used to record errors.

It is easy to use and automatically generates log files by date:

The code is as follows:


// When debugging, multiple parameters can be:
Sysdebug ("hello ");
Sysdebug ("hello", "tiger is coming now ");

// The error records are the same:
Syserror ("error ");
Syserror ("error", "unfortunately tiger is dead", "we are sad ");

Php debugging and logging functions are as follows:

The code is as follows:


/**
* Record debugging information
*/
Function sysdebug ($ msg ){
If (defined ("DEBUG_MODE ")){
// TODO detection debugging switch, which is not printed upon release
$ Params = func_get_args ();
$ Traces = debug_backtrace ();
$ Trace = array_pop ($ traces );
Sysrecord ($ params, $ trace, 'debug ');
}
}

/**
* Record error information
*/
Function syserror ($ msg ){
$ Params = func_get_args ();
$ Traces = debug_backtrace ();
$ Trace = array_pop ($ traces );
Sysrecord ($ params, $ trace, 'error ');
}

/**
* Write a file
* @ Ignore
*/
Function sysfile ($ filename, $ msg, $ mode = null ){
$ Path = dirname ($ filename );
If (! File_exists ($ path )){
Mkdir ($ path, 0666, true );
}
$ Flag = LOCK_EX;
If ($ mode ){
Switch ($ mode ){
Case "add ":
$ Flag = FILE_APPEND | LOCK_EX;
Break;
Case "":
$ Flag = FILE_APPEND | LOCK_EX;
Break;
Default:
Break;
}
}
File_put_contents ($ filename, $ msg, $ flag );
}

/**
* Record Information
* @ Ignore
*/
Function sysrecord ($ params, $ trace, $ level ){
$ Path = dirname (_ FILE _). "/logs /";
// Modify the TODO log storage directory.

$ File = $ trace ['file'];
$ Func = $ trace ['function'];
If ($ func = "sys $ level "){
$ Func = '';
}
$ Filename = $ path. "$ level/". date ("Y-m-d"). '. log ';
$ Msg = "[". date ("m-d H: I: s "). "] File :\"". basename ($ file ). "\" Func :\"". $ func. "\" Msg :". json_encode ($ params ). "\ r \ n ";
Sysfile ($ filename, $ msg, 'ADD ');
}

Debugging is often required during the development process of the Ghost website program, and running logs need to be recorded during the release phase to facilitate fault discovery and event restoration...

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.