Php custom error logs and php custom logs

Source: Internet
Author: User
Tags parse error

Php custom error logs and php custom logs

If the project needs to promptly process the defined error log, you need to modify the output mode of the custom error log (writing logs, sending emails, and sending text messages)

1. register_shutdown_function (array ('phperror ', 'shutdown _ function'); // defines the function to be executed after the PHP program is executed.

A function can be used to execute a function after the program is executed. Its function can be used to perform subsequent operations after the program is executed. The program may have execution timeout or forced shutdown, but the default prompt is unfriendly in this case. If you use the register_shutdown_function () function to catch exceptions, you can provide more friendly error display methods and perform subsequent operations on some features, such as temporary data cleaning after execution, including temporary files.
The call conditions can be understood as follows:
1. When the page is forcibly stopped by the user
2. When the program code runs out of time
3. When PHP code execution is complete, there are exceptions, errors, and warnings in code execution.


2. set_error_handler (array ('phpererror', 'error _ handler'); // sets a user-defined error handling function

Use the set_error_handler () function to set a custom error handler, and then trigger the error (through trigger_error ()):


3. set_exception_handler (array ('phperror ', 'appexception'); // custom exception handling

Defines the data format thrown by an exception.

Class phperror {

// Custom error Output Method
Public static function error_handler ($ errno, $ errstr, $ errfile, $ errline ){
$ Errtype = self: parse_errortype ($ errno );
$ Ip = $ _ SERVER ['remote _ ADDR ']; // obtain the Client IP address.
// Custom error message format
$ Msg = date ('Y-m-d H: I: s '). "[$ ip] [$ errno] [-] [$ errtype] [application] {$ errstr} in {$ errfile }:{ $ errline }";
// Customize the path of the log file
$ LogPath = 'logs/app. log ';
// Write operations, pay attention to file size control, etc.
File_put_contents ($ logPath, $ msg, FILE_APPEND );
}

// Error output method in system running
Public static function shutdown_function (){
$ Lasterror = error_get_last (); // shutdown can only catch the last error, and trace cannot get
$ Errtype = self: parse_errortype ($ lasterror ['type']);
$ Ip = $ _ SERVER ['remote _ ADDR ']; // obtain the Client IP address.
// Custom error message format
$ Msg = date ('Y-m-d H: I: s '). "[$ ip] [{$ lasterror ['type']}] [-] [$ errtype] [application] {$ lasterror ['message']} in {$ file}: {$ lasterror ['line']} ";
// Customize the path of the log file
$ LogPath = 'logs/app. log ';
// Write operations, pay attention to file size control, etc.
File_put_contents ($ logPath, $ msg, FILE_APPEND );
}

// Custom exception output

Public static function appException ($ exception ){
Echo "exception:", $ exception-> getMessage (), "/n ";
}
Private static function parse_errortype ($ type ){
Switch ($ type ){
Case E_ERROR: // 1
Return 'fatal error ';
Case E_WARNING: // 2
Return 'warning ';
Case E_PARSE: // 4
Return 'parse error ';
Case E_NOTICE: // 8
Return 'notice ';
Case E_CORE_ERROR: // 16
Return 'core error ';
Case E_CORE_WARNING: // 32
Return 'core warning ';
Case E_COMPILE_ERROR: // 64
Return 'compile error ';
Case E_COMPILE_WARNING: // 128.
Return 'compile warning ';
Case E_USER_ERROR: // 256
Return 'user error ';
Case E_USER_WARNING: // 512.
Return 'user warning ';
Case E_USER_NOTICE: // 1024.
Return 'user notice ';
Case E_STRICT: // 2048 //
Return 'strict notice ';
Case E_RECOVERABLE_ERROR: // 4096
Return 'recoverable error ';
Case E_DEPRECATED: // 8192
Return 'describeed ';
Case E_USER_DEPRECATED: /// 16384
Return 'user deprecated ';
}
Return $ type;
}

}

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.