使用dedecms的朋友可能会自己dedecms会记录程序执行错误文件和注入sql到一个日志文件中去,下面我来介绍在php中的一个error_log()将错误信息写入日志文件实例,希望此方法对大家有帮助.
error_log() 是发送错误信息到某个地方的一个函数,在程序编程中比较常见,尤其是在程序调试阶段.
本文将用实例讲解一下error_log()这个函数的用法,以及一些需要注意的问题,代码如下:
- <?php
-
- $str='这是条错误信息。';
-
- error_log($str,3,'errors.log');
-
- ?>
上述是最常用的error_log()例子,它的作用是把一条信息写入errors.log这个文件里,这个文件如果不存在则自动创建,在这个例子中,我们看到有一个参数“3”,注意这个数字“3”不能更改也不能去掉.
下面列举一下使用error_log()这个函数的过程中可能出现的问题:
(1)程序报错提示:Warning: error_log() [function.error-log]: failed to open stream: Permission denied in ...on line ...
上述错误的出现,是因为文件没有写权限,开启该目录的文件写权限即可.
(3)写入到log文件中的信息不能换行
使用error_log()写入log文件,会发现文字是没有换行的,可以对以上代码做如下改进:
- <?php
-
- $str="这是条错误信息。rn";
-
- error_log($str,3,'errors.log');
-
- ?>
注意$str,用的是双引号,php单引号和双引号的区别,还在字符串结尾加上了rn,这跟第一个实例那个写法是不同的,下面我再分享两个自定义记录出错日志实例,代码如下:
- <?php
- function exceptionHandler(){
- error_reporting(E_ALL ^ E_NOTICE);
- date_default_timezone_set('Etc/GMT-8');
-
- ini_set('display_errors',0);
- ini_set('error_log','D:\'.date('Y-m-d').'_weblog.txt');
- ini_set('log_errors',1);
- ini_set('ignore_repeated_errors',1);
-
- $user_defined_err = error_get_last();
- if($user_defined_err['type'] > 0)
- {
- switch($user_defined_err['type']){
- case 1:
- $user_defined_errType = '致命的运行时错误(E_ERROR)';
- break;
- case 2:
- $user_defined_errType = '非致命的运行时错误(E_WARNING)';
- break;
- case 4:
- $user_defined_errType = '编译时语法解析错误(E_PARSE)';
- break;
- case 8:
- $user_defined_errType = '运行时提示(E_NOTICE)';
- break;
- case 16:
- $user_defined_errType = 'PHP内部错误(E_CORE_ERROR)';
- break;
- case 32:
- $user_defined_errType = 'PHP内部警告(E_CORE_WARNING)';
- break;
- case 64:
- $user_defined_errType = 'Zend脚本引擎内部错误(E_COMPILE_ERROR)';
- break;
- case 128:
- $user_defined_errType = 'Zend脚本引擎内部警告(E_COMPILE_WARNING)';
- break;
- case 256:
- $user_defined_errType = '用户自定义错误(E_USER_ERROR)';
- break;
- case 512:
- $user_defined_errType = '用户自定义警告(E_USER_WARNING)';
- break;
- case 1024:
- $user_defined_errType = '用户自定义提示(E_USER_NOTICE)';
- break;
- case 2048:
- $user_defined_errType = '代码提示(E_STRICT)';
- break;
- case 4096:
- $user_defined_errType = '可以捕获的致命错误(E_RECOVERABLE_ERROR)';
- break;
- case 8191:
- $user_defined_errType = '所有错误警告(E_ALL)';
- break;
- default:
- $user_defined_errType = '未知类型';
- break;
- }
- $msg = sprintf('%s %s %s %s %s',date("Y-m-d H:i:s"),$user_defined_errType,$user_defined_err['message'],$user_defined_err['file'],$user_defined_err['line']);
- error_log($msg,0);
- }
- }
-
- register_shutdown_function('exceptionHandler');
- ?>
调用方法:
- <meta charset="utf-8">
- <?php
-
- include('error.class.php');
- echo $_COOKIE['aaaaadfa'];
- echo $_SESSION['aaaaadfa'];
- ?>
例2,日志记录类,代码如下:
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class Logs {
- private $_filepath;
- private $_filename;
- private $_filehandle;
-
-
-
-
-
-
-
- public function Logs($dir = null, $filename = null) {
-
- $this->_filepath = emptyempty ( $dir ) ? '' : $dir;
-
-
- $this->_filename = emptyempty ( $filename ) ? date ( 'Y-m-d', time () ) . '.log' : $filename;
-
-
- $path = $this->_createPath ( $this->_filepath, $this->_filename );
-
- if (! $this->_isExist ( $path )) {
-
- if (! emptyempty ( $this->_filepath )) {
-
- if (! $this->_createDir ( $this->_filepath )) {
- die ( "创建目录失败!" );
- }
- }
-
- if (! $this->_createLogFile ( $path )) {
- die ( "创建文件失败!" );
- }
- }
-
-
- $path = $this->_createPath ( $this->_filepath, $this->_filename );
-
- $this->_filehandle = fopen ( $path, "a+" );
- }
-
-
-
-
-
-
- public function setLog($log) {
-
- $str = "";
- if (is_array ( $log )) {
- foreach ( $log as $k => $v ) {
- $str .= $k . " : " . $v . "n";
- }
- } else {
- $str = $log . "n";
- }
-
-
- if (! fwrite ( $this->_filehandle, $str )) {
- die ( "写入日志失败" );
- }
- }
-
-
-
-
-
-
- private function _isExist($path) {
- return file_exists ( $path );
- }
-
-
-
-
-
-
- private function _createDir($dir) {
- return is_dir ( $dir ) or ($this->_createDir ( dirname ( $dir ) ) and mkdir ( $dir, 0777 ));
- }
-
-
-
-
-
-
- private function _createLogFile($path) {
- $handle = fopen ( $path, "w" );
- fclose ( $handle );
- return $this->_isExist ( $path );
- }
-
-
-
-
-
-
- private function _createPath($dir, $filename) {
- if (emptyempty ( $dir )) {
- return $filename;
- } else {
- return $dir . "/" . $filename;
- }
- }
-
-
-
-
-
-
- function __destruct() {
-
- fclose ( $this->_filehandle );
- }
- }
- ?>
修正:
1,不用每写一条日志就进行次文件的IO的操作,在对象声明时打开文件句柄
2,支持数组类型的日志记录
3,可使用默认路径和默认文件,为当前目录下的YYYY-MM-DD.log文件
总结:个人更喜欢系统自带的函数,如果系统自带的日志记录函数满足不了我们就可以使用下面自定义函数. |