Category Archives: PHP - Page 2

Create a logger in Zend Framework using application.ini

I am here going to show how I usually setup a logger in my Zend applications. First we add the logger settings into the application.ini:

[production]
...
;Setup logger (file)
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/logs/basic.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 7
...
  • WriterName is the type of writer you want. I choose “Stream” here for logging to file
  • WriterParams is the parameters which are sent to the writer. Since i use stream i set the path to the logfile and set the “mode” to “a” (append to file)
  • FilterName is the name of the filter type. I use “Priority” so I can use loglevels
  • FilterParams is set to 7 (Debug) so that all loglevels are logged

Now we will make the logger easily accessible by putting a handle in the Zend register like this.
Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
...
    protected function _initLogger() {
        $this->bootstrap("log");
        $logger = $this->getResource("log");
        Zend_Registry::set("logger", $logger);
    }
...
}

Now, to use the logger in your application just do like this:


//Get the logger handle from the register
$logger = Zend_Registry::get('logger');

//Use the logger
$logger->log("message", 3);//Second argument is the loglevel (0-7)

Valid loglevels (straight from the Zend code:Log.php):

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

Tested on Mac OSX 10.6.8 and Zend Framework 1.10.8

Zend Bootstrap “Circular resource dependency detected” error

So you have also got it :)? This is one of the more strange errors in the Zend Framework if you ask me. One way to get it is to give the init function the “wrong” name in the bootstrap class:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
    protected function _initAutoload() {
        $loader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'Application',
        'basePath' => APPLICATION_PATH
        ));

        return $loader;
    }

    protected function _initDbAdapter() {
        $this->bootstrap('db');
        $db = $this->getResource('db');
        Zend_Registry::set('db' , $db);
    }

    protected function _initConfig() {
        Zend_Registry::set('config', $this->getOptions());
    }  
    
    protected function _initTranslate() {

        $this->bootstrap("translate");
        $translator = $this->getResource("translate");
        Zend_Registry::set('translator', $translator);
    }
 
}

You see something wrong here? I didn’t 🙂 But there is a problem with ‘initTranslate()’ function. The last part of the name is ‘Translate’ which is the same as the

$this->bootstrap("translate"); 

call. If the bootstrap call is ‘translate’ you can not name the init function ‘Translate’ (case does not matter) so you have to do this:

protected function _initTranslator() { // not '_initTranslate'

        $this->bootstrap("translate");
        $translator = $this->getResource("translate");
        Zend_Registry::set('translator', $translator);
    }

Now the name of the function and the bootstrap call are different from each other which should make the error go away

Hope this saves someone some time. Tested in Zend Framework 1.10

PHP check what modules are installed (cli)

I move around a lot between servers in my line of work so a quick way to see what modules are installed is very valuable. You can of course use the built in phpinfo() function but that requires a browser and web server. A quicker way for me is to use the command line interface.

Simply input on the command line:

php -m

and all the installed modules will be displayed.

Example output:

[PHP Modules]
bz2
calendar
ctype
curl
date
dbase
exif
ftp
gettext
gmp
hash
iconv
libxml
mime_magic
mqseries
openssl
pcntl
pcre
posix
pspell
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlrpczlib