Lately, I’ve found that I need to create a more robust logging system for both audits and debugging. I found a great logging primer from DevShed that offers some good insight into going about making a more robust system. Other than the theory, you shouldn’t really take much more away from it since, as one comment points out, the implementation is poor aka globals are bad. Caveat: my implementation probably won’t be all that great either but I hope to avoid making beginner’s mistakes.
That being said, the reason this particular article is centered on Zend is because I’m pretty much in love with Zend right now. The main reason for writing this article is that, while the Zend documentation on Zend_Log is thorough and easy to understand when you read it, I hate back and forth between the sections of Zend_Log to make sure I understand what I’m needing to do.
I’m assuming you have at least gone through the Zend Quickstart before reading on. For my filesystem and db configuration, I use Zend_Config to load up settings such as the logfile name and log database adapter. I could’ve set the path for the file but I want a certain degree of flexibility.
At this point, I have the logger configuration from zend config and the db adapter so let’s move on to some code examples. This is partially taken from my in-progress class.
making a full filepath:
$filepath=(isset($options['FilePath'])) ? $options['FilePath'] : INC_PATH."/".$this->_systemConfig->system->logfile; $writer = new Zend_Log_Writer_Stream($filepath);
the filepath is used with the Zend_Log_Writer. Next we set a format for the writer and then instantiate Zend_Log with the writer
// specify format
Note: $format is a text string that indicates how each log entry will appear.
The important things to remember here is that Zend_Log creates an associative array with all the basic useful log information. This array can be accessed in a variety of ways. For my purposes, knowing what is accessible by the stream and database writers are important.
If you have read the documentation thoroughly, you will have found that in section 30.1.6 Zend documents this array expressly. Unfortunately, unlike some most of their other documentation, this useful bit of information is almost secreted away. I’ve scoured the docs a lot to find the keys created by Log so here they are as a quick reference.
These little gems are pretty much the keys to the Kingdom of Zend_Log.
If you want to reference them for formatting in a stream, you can do something like:
$format = (isset($options['LogFormat'])) ? $options['LogFormat'] : '%timestamp% ' .PHP_EOL.'%priorityName% (%priority%)' .PHP_EOL.'%message%' . PHP_EOL.PHP_EOL .str_repeat("=",100). PHP_EOL;
or if you need to use them for your db, you can use set column mapping like so:
$columnMapping = array('lvl' => 'priority','Priority'=>'priorityName', 'msg' => 'message'); $writer = new Zend_Log_Writer_Db($db, 'log_table_name', $columnMapping);
That should cover the basics.
Pro-tip discussion: For auditing, you may want to make separate log files for access,create,and changes made to your app/system. Error logging should log everything into files, but a separate debug copy would probably out in the development environment. I usually do a
tail -f on debug/log file. It seems obvious to, me that any logging/exception system you make should have a simple outside error handling function to alert you of that logging broke. I believe this can be done by setting the error handler in the constructor. Haven’t played with this yet. It seems to me that any robust logging system you implement will run into a chicken vs the egg type problem