Zend_Log
是一个通用日志组件,它支持多个日志后端,格式化发送给日志的消息,过滤被纪录的消息。这些功能被划分为如下的对象:
- Zend_Log_Writer_Stream,相当于写文件流的铅笔。
- Zend_Log_Writer_Mail,将日志信息以邮件的型式,记录发送。
- Zend_Log_Writer_Db,将日志信息以数据库的型式记录。
- Zend_Log_Filter_Message,以正则的型式阻塞信息。
- Zend_Log_Filter_Priority,阻塞日志等级。
- Zend_Log_Formatter_Simple,比较常用的类,下面我们会讲到。
- Zend_Log_Formatter_Xml,已XML的型式记录Log信息。
上面说了这么多的理论,来的实惠的了,上菜!顺便说下,在真正使用Zend_Log的时候,应该将它封装成Singleton,并通过application.ini来配置。以上纯属个人理解,有什么错误的地方还请指正:)。言归正传,下面我将分为Writer的三个实例,来分别上菜。
第一道菜:Zend_Log_Writer_Stream
require_once 'Zend/Log.php'; require_once 'Zend/Log/Writer/Stream.php'; require_once 'Zend/Log/Formatter/Simple.php'; //Zend_Log实例 $log = new Zend_Log(); //设置EventItem $log->setEventItem('pid',getmypid()); $log->setEventItem('ip',getIP()); //获取文本文件路径 //已日期定义文件名称 $file = 路径 .'/log/log-' . date('y-m-d') . '.log'; $stream = @fopen($file,'a',false); //Zend_Log_Writer_Stream实例 $writer = new Zend_Log_Writer_Stream($stream); //格式化输出Log信息 //%timestamp%为站位符,显示输出 $format = '[%timestamp%][%priorityName%-%priority%]-[PID:%pid%][ip:%ip%]' . PHP_EOL . '%message%' . PHP_EOL; $formatter = new Zend_Log_Formatter_Simple($format); $writer->setFormatter($formatter); //添加writer $log->addWriter($writer); //添加过滤器,相当于Zend_Log_Filter_Priority $log->addFilter(3); //写日志 $log->log('Hello Dante.What/'s going on?',3); exit; /** * 获得IP地址 * @return ip **/ function getIP() { $realip = NULL; if (isset($_SERVER)) { if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } elseif (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv('HTTP_X_FORWARDED_FOR')) { $realip = getenv( 'HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_CLIENT_IP')) { $realip = getenv('HTTP_CLIENT_IP'); } else { $realip = getenv('REMOTE_ADDR'); } } return $realip; }
上面的代码有两点我需要说下:
第二道菜:Zend_Log_Writer_Db
数据库表以MYSQL为例:
-- -- 表的结构 `logs` -- DROP TABLE IF EXISTS `logs`; CREATE TABLE IF NOT EXISTS `logs` ( `logID` bigint(20) NOT NULL auto_increment, `logPid` int(10) NOT NULL, `logType` tinyint(3) NOT NULL, `logName` varchar(20) collate utf8_unicode_ci NOT NULL, `logIp` varchar(30) collate utf8_unicode_ci default '127.0.0.1', `logTime` timestamp NULL default NULL, `logMsg` text collate utf8_unicode_ci, PRIMARY KEY (`logID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=8 ;
表的column可根据自己的需要来定义,以上代码仅供参考。
require_once 'Zend/Db.php'; require_once 'Zend/Log.php'; require_once 'Zend/Log/Writer/Db.php'; //Zend_Log实例 $log = new Zend_Log(); //设置EventItem $log->setEventItem('pid',getmypid()); $log->setEventItem('ip',getIP()); //通过Zend_Db工厂创建DB $dbAdapter = Zend_Db::factory('PDO_MYSQL',array( 'host' => 'localhost' ,'dbname' => 'Lap' ,'username' => 'root' ,'password' => '1986630187' ,'charset' => 'UTF8' )); //数据库字段配置 $columns = array( 'logPid' => 'pid' ,'logType' => 'priority' ,'logName' => 'priorityName' ,'logIp' => 'ip' ,'logTime' => 'timestamp' ,'logMsg' => 'message' ); //Zend_Log_Writer_Db实例,logs为表名 $writerDb = new Zend_Log_Writer_Db($dbAdapter,'logs',$columns); //添加wirter $log->addWriter($writerDb); $log->log('Hello Dante.What/'s going on?',3); exit; /** * 获得IP地址 * @return ip **/ function getIP() { $realip = NULL; if (isset($_SERVER)) { if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } elseif (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv('HTTP_X_FORWARDED_FOR')) { $realip = getenv( 'HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_CLIENT_IP')) { $realip = getenv('HTTP_CLIENT_IP'); } else { $realip = getenv('REMOTE_ADDR'); } } return $realip; }
第三道菜:Zend_Log_Writer_Email
require_once 'Zend/Log.php'; require_once 'Zend/Log/Writer/Mail.php'; require_once 'Zend/Mail.php'; //Zend_Mail实例 $mail = new Zend_Mail(); //发件人 $mail->setFrom('[email protected]', 'Some Sender'); //邮寄到 $mail->addTo('[email protected]', 'Some Recipient'); //主题 $mail->setSubject('TestSubject'); $log = new Zend_Log(); $writer = new Zend_Log_Writer_Mail($mail); $log->addWriter($writer); $log->log('Hello Dante.What/'s going on?',3);
三道菜终于上齐了,希望味道还可以。刚才有个小插曲,我写一半的时候为了看效果发表了一下,突然发现刚才写的东西全没了,也没有发表成功,崩溃了都。不过还好还是硬着头皮重写了一遍。