A simple logging function-PHP source code

Source: Internet
Author: User
Php code for a simple logging function

/*** Record a log and try to write the log in the following three ways. *-Write logs to the file specified by the current parameter. *-Try to write content to the error_log specified in php. ini. *-If the system log fails to write content to the system log, false is returned. ** You do not need to specify the logFile and dateFormat parameters for each call * the system automatically remembers the last specified content. ** After PHP5.0, make sure that the correct zone has been set. Otherwise, an error may be thrown. * Example: * @ code php * // The first call, initialize the log, and write the first message. * Logg ('init... ', LOG_INFO,'/usr/log.txt ', 'Y-m-D'); * // write logs * logg ('log msg', LOG_INFO ); * @ endcode ** @ param string $ message log content * @ param int $ type log type, refer to the syslog function parameter * @ param string $ logFile log file * @ param string $ dateFormat log time format * @ return bool whether the log is successfully written * @ staticvar array $ types parameter $ type. * @ Staticvar string $ file stores the last pass of the $ logFile parameter. * @ Staticvar string $ format saves the last content passed by the $ dateFormat parameter. * @ Link http://blog.830725.com/post/13.html */Function logg ($ message, $ type, $ logFile = null, $ dateFormat = null) {static $ types = array (LOG_EMERG => 'emerg ', LOG_ALERT => 'alert ', LOG_CRIT => 'critical', LOG_ERR => 'error', LOG_WARNING => 'warning', // in windows, the following three values are the same: LOG_NOTICE => 'notice ', LOG_DEBUG => 'debug', LOG_INFO => 'info'); static $ file = null; static $ format = 'Y-m-d H: I: s'; if (! Is_null ($ logFile) {$ file = $ logFile;} if (! Is_null ($ dateFormat) {$ format = $ dateFormat;}/* format the message */$ type = isset ($ types [$ type])? $ Type: LOG_INFO; $ msg = date ($ format ). '['. $ types [$ type]. ']'. $ message. PHP_EOL; if (error_log ($ msg, 3, $ file) {return true;} if (error_log ($ msg, 0) {return true ;} return syslog ($ type, $ message );}

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.