Zend Framework 101: Zend_Log
Creating Your First Logger
To begin, I'll show you how to create a basic file-system logger. To achieve this we must instantiate the Zend_Log class, then add a writer to this object. We will use a writer that writes log messages to the file system.
The class we use to write log messages to the file system is Zend_Log_Writer_Stream. This class accepts the path where the log messages should be written to as the first argument to the constructor.
Listing 1 shows an example of creating the logger and adding the writer to it. You can either add the writer when instantiating Zend_Log (such as new Zend_Log(new Zend_Log_Writer_Stream(...))), or as shown in the following listing.
require_once('Zend/Log.php'); require_once('Zend/Log/Writer/Stream.php'); $logger = new Zend_Log(); $writer = new Zend_Log_Writer_Stream('sample.log'); $logger->addWriter($writer); $logger->log('sample message!', Zend_Log::INFO);
The above listing concludes by calling the log() method on the Zend_Log object. This records a message to the log. Listing 2 shows an example of how the log file appears after this code is run.
$ cat sample.log
2009-04-11T14:47:20+09:00 INFO (6): sample message!In the next section I'll cover the recording of events in greater detail.




