Category Archives: Zend Log

Using Zend Log in Codeigniter

If you are developing a complex script then LOG helps you to keep trace the function execution flow and thus helps fixing out bug.

Here is an implementation of using Zend Log in Codeigniter framework:
1. Copy Zend Log Library:
i. Download ZendFramework-1.11 from here: http://framework.zend.com/downloads/latest
ii. Extract it. Inside it there is a “Log” folder inside “library/Zend” directory.
iii. Create a folder inside “application/libraries” of your codeigniter and copy “Log” folder (from ii.) into it.
iv. Copy “Exception.php” and “Log.php” from extracted “library/Zend”  directory to Codeigniter’s “application/libraries/Zend” directory.

2. Create Library:
Create Zend.php inside application/libraries and paste following codes:

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

class CI_Zend {
	private $writer;
	private $logger;

	public function __construct() {
		ini_set('include_path',ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
	}

	public function log_init($logfile) {
		require_once 'Zend/Log.php';
		require_once 'Zend/Log/Writer/Stream.php';

		$this->writer = new Zend_Log_Writer_Stream($logfile);
		$this->logger = new Zend_Log($this->writer);
	}

	public function info_log($msg) {
		$this->logger->log($msg, Zend_Log::INFO);
	}

	public function err_log($msg) {
		$this->logger->log($msg, Zend_Log::ERR);
	}
}

3. Create controller myzend.php:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Myzend extends CI_Controller {

    public function __construct() {
	
        parent::__construct();
    }

    public function index() {
	
        $this->load->library('zend');
        $this->zend->log_init('application.log');

       $this->zend->info_log('This is Info message', Zend_Log::INFO);
       $this->zend->err_log('This is Error message', Zend_Log::ERR);
    }
}

Thats it.

Ref:
Log Your PHP Application With Zend_Log (Part 2)
Integrate Zend library in code Igniter

Advertisements