Recently I had the pleasure of getting back to some Java Programming and was lamenting about the Java Log4J library used for application logging and how it would be great to have such a facility in PHP. That got me searching for a similar library for PHP. Being a Zend Framework users already, I was excited to see that Zend has a logging facility that seems to be modeled after Log4J. While the Log4J Library is a very capable and functional, the Zend_Log facility is much more limited. But for the kinds of application logging I needed, it is just right.
Zend_Log has a factory that you can use to instantiate a single Zend_Log object. For grins, I decided to write my own singleton class for doing essentially the same. The following class sets up three Zend_Log writers to write to 1) web browser, 2) a log file 3) email under certain circumstances like when the application determines that a Zend_Log::EMERG condition has bee detected.
<?php
/*
* Author: Eric F. Palmer
* Email: epalmer [at] richmond [dot] edu
* Organization: University of Richmond
* Date: July 11, 2010
* Description: Singleton Class for instantiating a Zend Framwork Zend_Log Logging facility
*
*
* Copyright: 2010 by Eric F. Palmer, University of Richmond
* License: LGPL http://www.gnu.org/copyleft/lesser.html
*
* If you find this useful send me an email please. Thanks EFP
*/
class Logger {
private static $logger = null;
private static $initialized = false;
private function __construct() {
// make constructor private so that no one can instantiate this class
}
private static function initialize()
{
if (self::$initialized) {
return;
}
// else set up logger
// set up a writer for writing to html output, bold the output and put a html break at the end of the line
$logger = new Zend_Log();
// log to http output/console
$writer = new Zend_Log_Writer_Stream('php://output');
$format = '<strong>%timestamp% %priorityName% (%priority%): %message% </strong><br/>' . PHP_EOL;
$formatter = new Zend_Log_Formatter_Simple($format);
$writer->setFormatter($formatter);
$logger->addWriter($writer);
// set up a 2nd writer that logs to a file with a simple format
/* if you run the test program from the web you will have to set up the log folder to
* have permissions so that your web server can write to the files
*/
$writer = new Zend_Log_Writer_Stream('/yourPathGoesHere/testZendLogger.log');
$format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
$formatter = new Zend_Log_Formatter_Simple($format);
$writer->setFormatter($formatter);
$logger->addWriter($writer);
// set up a 3rd writer that sends an email when an EMERG log message is created
$mail = new Zend_Mail();
if (!$mail) {
$logger("Unable to instantiate Zend_Mail object", Zend_Log::INFO);
}
// put your email address in the form and to methods belo
$mail->setFrom('yourEmail@domain.com')->addTo('yourEmail@domain.com');
$writer = new Zend_Log_Writer_Mail($mail);
$writer->setSubjectPrependText('A Serious Error Has Occurred in testZendLog.php: ');
$writer->addFilter(Zend_Log::EMERG);
$logger->addWriter($writer);
self::$logger = $logger;
self::$initialized = true;
}
public static function getLogger()
{
// call this method to return a single instance object for the loger.
self::initialize();
return self::$logger;
}
}
After you have edited the above to suit your environment, you can run the follow program to test it.
<?php
// change this path statement to point to your instance of Zend Framework
set_include_path('/WWW/includes/zend/Framework/library');
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
require_once './Logger.php';
// get instance of a Zend Logger Object configured on the Logger.php file
// using a static facotry like call
$logger = Logger::getLogger();
// test the logger - does it echo out a log instance
$logger->log('Logger has been created', Zend_Log::INFO);
// assume some serious error has been enocunted, now send an email using the Zend_Log facility
$logger->log('A serious error has occured', Zend_Log::EMERG);
$logger->log('done', Zend_Log::INFO);
When you run this you should see the following in your browser:
2010-07-12T11:36:00-04:00 INFO (6): Logger has been created
2010-07-12T11:36:00-04:00 EMERG (0): A serious error has occured
2010-07-12T11:36:00-04:00 INFO (6): done
You will also have the same messages in your logfile and you should receive 1 email.
Enjoy,
Eric
I’m in the process of building a Centos 5.3 Desktop Virtual Machine using VMWARE Fusion 3.0. This VM will be used as a PHP/Oracle/Exist-db (xml database) development VM. What I wanted was the ability to add menu items to the gnome Application menu that
- open a terminal window
- run a command
- leave the window open
For example this can be used for starting up apache using apachectl start. Using Google, the closest solution I found was in this forum thread Open the terminal, have it run a command and then stay open. I wanted this to work in a gnome-terminal instead of an xterm window. Building on the success outlined in the forum thread I was able to make this work.This example assumes that only one Centos user would have access to this menu item. But this technique can be modified to make this work with all users.
Create a file in your home directory and call it run-cmd with the following contents:
#!/bin/sh $@ /bin/bash
So lets assume that this file was placed in /home/eric
Using chmod or the file browser, turn on the execute bit for /home/eric/run-cmd
Using the gnome menu editor (right click on the Applications menu item and select Edit Menus
Now create a new menu item, give it a name and select a desired icon. In the Command field enter the following:
gnome-terminal -x /home/eric/run-cmd ls -al
Save your work by selecting close. That is all there is to it. Now you can substitute your favorite bash commands or a shell script for ls-al and get the desired behavior. For example:
gnome-terminal -x /home/eric/run-cmd apachectl start
One issue remains: the actual command supplied does not display in the window. Only the output from the command.
I hope you find this useful. If so please leave a comment.
Moving Files to a WebDAV Mounted Share – Why?
I use a Content Management System (CMS) called Cascade from Hannon Hill. We have 40,000+ pages of content in Cascade. One of Cascade’s strengths is the ability to quickly generate XML files and store then from what they call a data definition. These XML files can then be transformed, using XSLT into other XML files, HTML files and files of other formats. But at times we want to just save the XML files out of Cascade (publish them) and use them in other applications or transform them in ways not supported by Cascade. Recently I came across an XML database system called eXist-db that makes its XML collections visible as a WebDAV share. This got me thinking about using eXist-db as a XML file server/cache system that also will be used to transform the files into many different forms for presentation on a dynamic web site I’m working on.
Unfortunately Cascade does not support WebDAV file publishing but instead supports FTP and SFTP file transfers. So I started researching how to SFTP files into eXist-db. This post details what I have learned so far about connecting to a WebDAV from a MAC. The ultimate goal is to run eXist-db on a Red Hat Linux system (RHEL 5.x) with the WebDAV share mounted as a filesystem using davfs2. This will allow us to sftp from Cascade to the davfs2 mounted filesystem and have davfs2 automatically put the files into the eXist-db collection. I’m still working on getting davfs2 set up on a test server. Here is what I have learned so far about doing WebDAV on a MAC. Well not really, its just a small portion of what I have learned.
Connecting to WebDAV from a MAC OS X System
This is a simple howto post showing two ways to connect to, or mount, a WebDAV server from a MAC OS X system. This has been tested on 10.5.8 OS X and should work on later versions.
For this example I’ll show how to connect to a local eXist-db XML DB Server instance running directly on the same MAC. This instance has a WebDAV address of http://localhost:8080/exist/webdav/db/
I’ve also tested these methods connecting to remote WebDAV servers.
Connect Using Finder
In the Finder choose Go then Connect to Server…. and enter the WebDAV server address then click on the Connect button.
That’s all there is to it. The Finder will bring up a file browser window and you can manipulate the files in the WebDAV share now as if there were in a local folder.
Connect Using Transmit Panic.com
I’ve just started using Transmit and so far I really like this application. While the focus of this post is on WebDAV, Transmit looks like a full featured file transfer utility. It has Amazon S3 support, Edit remote files in any application, iDisk synchronization and more. It even will let you shuttle files from one server to another. Transmit comes with a free 15 day trial and is $29.95 $US. So go get a copy and fire it up and follow along.
First just click on the Connect button above the right pane
The click on the “+” sign at the botom left of the right pane. Fill in the WebDAV server connection details as shown and click the Save button.
Then double click the connection name and start working with your WebDAV mounted share.
Why Use a Transmit and Not Finder?
Using the Finder is really easy and may provide all the features you need to interact with a WebDAV share. But at times you may want to work in an environment that is more full featured. Since a WebDAV share is typically a remote share, connectivity issues can disrupt your sessions with the server. Since Transmit is a full featured file transfer application, there are features that can help you move large quanities of files in and out of a WebDAV share reliably.
One I really like is the ability to syncronize a Mac folder with a Transmit Mounted WebDAV share. Recently I had to upload more than 1200 XML files (> 375 MB) to an eXist-db instance over WebDAV. During the transfer, the network connection went down. Not wanting to review every file entry to make sure that all files were transmited, I just used the Transmit synchronize function to ensure all of the files in my mac XML folder were transferred to the WebDAV share.
Please let me know if you know of other 3rd party applications, you like, that support WebDAV mounted shares.




