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 );}