Zend有提供一個專門紀錄Log的物件:Zend_Log,以下為Zend_Log定義的Log類型

1EMERG   = 0;  // Emergency: system is unusable
2ALERT   = 1;  // Alert: action must be taken immediately
3CRIT    = 2;  // Critical: critical conditions
4ERR     = 3;  // Error: error conditions
5WARN    = 4;  // Warning: warning conditions
6NOTICE  = 5;  // Notice: normal but significant condition
7INFO    = 6;  // Informational: informational messages
8DEBUG   = 7;  // Debug: debug messages

Zend_log寫入檔案

1//預設是使用Append mode開啟檔案
2$writer = new Zend_Log_Writer_Stream(dirname(__DIR__) . '/log/logfile');
3$logger = new Zend_Log($writer);
4//寫入 info log
5$logger->info('Informational message');
6//寫入 err log
7$logger->err('Error');
8//使用參數決定log型態
9$logger->log('Info~~~ message', Zend_Log::INFO);

fopen檔案,修改檔案開啟權限

1//可以使用fopen開啟檔案
2$file = fopen(dirname(__DIR__) . '/log/logfile2','a+',false);
3$writer = new Zend_Log_Writer_Stream($file);
4$logger = new Zend_Log($writer);
5$logger->info('Informational message');

將Log寫入資料庫

1//Log寫入資料庫
2$db = Zend_Db::factory('Pdo_Mysql', array(
3    'host'     => 'localhost',
4    'username' => 'johnson',
5    'password' => '123',
6    'dbname'   => 'log'
7));
8$db -> query('set names UTF8');
9 
10//將資料對應到欄位(Zend_Log本身有提供一些event item,例如priority、message)
11$columnMapping = array('lvl' => 'priority', 'msg' => 'message', 'date' => 'log_date');
12$writer = new Zend_Log_Writer_Db($db, 'log_table', $columnMapping);
13 
14$logger = new Zend_Log($writer);
15//自訂資料庫date的欄位
16$logger->setEventItem('log_date',date('Y-m-d'));
17$logger->info('Informational message');