Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
17th November 2009 - 9 minutes read time
When you run a phing script it will print things out to the console. These messages are either system messages (eg. BUILD STARTED) or echo messages that you have put into your build.xml file. All of this output is controlled and created by a logger file. The default logger is called (unsurprisingly) DefaultLogger and will be used as a default. There are a few different types of logger available, all of which can be found in the listener folder in your PEAR\phing directory.
To select a different logger script to be used just use the -logger flag in your phing call. To specify the DefaultLogger use the following:
phing -logger phing.listener.DefaultLogger
To specify an XML logger that will create an XML document of the phing events that occurred in the current working directory use the XmlLogger logger.
phing -logger phing.listener.XmlLogger
So what happens if we want to print out only the echo messages in our build file? There isn't a logger we can use to do this so the solution it to simply make our own. The following code block contains a logger that will only print out echo commands along with any errors that occurred and the time taken for the build to complete. Create a file called EchoLogger.php and put it into the PEAR\phing\listener directory. Many of the comments in this file are from DefaultLogger, I have just changed the parts that were different or not applicable.
<?php
require_once 'phing/BuildListener.php';
include_once 'phing/BuildEvent.php';
/**
* Writes all echo messages generated during a build event to the console.
*
* This is the only output generated by the logger with two exceptions. These
* are the total time taken for the build to run and any errors that occurred
* during the build.
*
* @author Philip Norton
* @see BuildEvent
* @package phing.listener
*/
class EchoLogger implements BuildListener {
/**
* Size of the left column in output. The default char width is 12.
* @var int
*/
const LEFT_COLUMN_SIZE = 12;
/**
* The message output level that should be used. The default is
* PROJECT_MSG_VERBOSE.
* @var int
*/
protected $msgOutputLevel = PROJECT_MSG_ERR;
/**
* Time that the build started
* @var int
*/
protected $startTime;
/**
* Char that should be used to seperate lines. Default is the system
* property <em>line.seperator</em>.
* @var string
*/
protected $lSep;
/**
* Construct a new default logger.
*/
public function __construct() {
$this->lSep = Phing::getProperty("line.separator");
}
/**
* Set the msgOutputLevel this logger is to respond to.
*
* Only messages with a message level lower than or equal to the given
* level are output to the log.
*
* <p> Constants for the message levels are in Project.php. The order of
* the levels, from least to most verbose, is:
*
* <ul>
* <li>PROJECT_MSG_ERR</li>
* <li>PROJECT_MSG_WARN</li>
* <li>PROJECT_MSG_INFO</li>
* <li>PROJECT_MSG_VERBOSE</li>
* <li>PROJECT_MSG_DEBUG</li>
* </ul>
*
* The default message level for DefaultLogger is PROJECT_MSG_ERR.
*
* @param integer the logging level for the logger.
* @access public
*/
function setMessageOutputLevel($level) {
$this->msgOutputLevel = (int) $level;
}
/**
* Sets the start-time when the build started. Used for calculating
* the build-time.
*
* @param object The BuildEvent
* @access public
*/
function buildStarted(BuildEvent $event) {
$this->startTime = Phing::currentTimeMillis();
}
/**
* Prints whether the build failed, and any errors that occured during
the build. Also outputs the total build-time on completion.
*
* @param object The BuildEvent
* @access public
* @see BuildEvent::getException()
*/
function buildFinished(BuildEvent $event) {
$error = $event->getException();
if ($error !== null) {
print($this->lSep . "BUILD FAILED" . $this->lSep);
if (PROJECT_MSG_VERBOSE <= $this->msgOutputLevel || !($error instanceof BuildException)) {
print($error->__toString().$this->lSep);
} else {
print($error->getMessage());
}
}
print($this->lSep . "Total time: " .$this->_formatTime(Phing::currentTimeMillis() - $this->startTime) . $this->lSep);
}
/**
* Fired when a target has started. We don't need specific action on this
* event. So the methods are empty.
*
* @param object The BuildEvent
* @access public
* @see BuildEvent::getTarget()
*/
function targetStarted(BuildEvent $event) {}
/**
* Fired when a target has finished. We don't need specific action on this
* event. So the methods are empty.
*
* @param object The BuildEvent
* @access public
* @see BuildEvent::getException()
*/
function targetFinished(BuildEvent $event) {}
/**
* Fired when a task is started. We don't need specific action on this
* event. So the methods are empty.
*
* @param object The BuildEvent
* @access public
* @see BuildEvent::getTask()
*/
function taskStarted(BuildEvent $event) {}
/**
* Fired when a task has finished. We don't need specific action on this
* event. So the methods are empty.
*
* @param object The BuildEvent
* @access public
* @see BuildEvent::getException()
*/
function taskFinished(BuildEvent $event) {}
/**
* Print any echo messages to the stdout.
*
* @param object The BuildEvent
* @access public
* @see BuildEvent::getMessage()
*/
function messageLogged(BuildEvent $event) {
if ($event->getPriority() <= $this->msgOutputLevel) {
$msg = "";
if ($event->getTask() !== null) {
$name = $event->getTask();
$name = $name->getTaskName();
if ($name == 'echo') {
$msg = "[$name] ";
$msg .= $event->getMessage();
$this->printMessage($msg, $event->getPriority());
}
}
}
}
/**
* Formats a time micro integer to human readable format.
*
* @param integer The time stamp
* @access private
*/
function _formatTime($micros) {
$seconds = $micros;
$minutes = $seconds / 60;
if ($minutes > 1) {
return sprintf("%1.0f minute%s %0.2f second%s",
$minutes, ($minutes === 1 ? " " : "s "),
$seconds - floor($seconds/60) * 60, ($seconds%60 === 1 ? "" : "s"));
} else {
return sprintf("%0.4f second%s", $seconds, ($seconds%60 === 1 ? "" : "s"));
}
}
/**
* Prints a message to console.
*
* @param string $message The message to print.
* Should not be null.
* @param int $priority The priority of the message.
* (Ignored in this implementation.)
* @return void
*/
protected function printMessage($message, $priority) {
print($message . $this->lSep);
}
}
You can now call your new logger by using the following command.
phing -logger phing.listener.EchoLogger
Only echo commands will be printed out, along with the time the script took to run. If any errors are encountered these will be printed in the normal way.
Running complex tasks in Phing can mean running out of memory, especially when altering or changing lots of files. I was recently working on a image sorting Phing project that sorted images based on EXIF information.
Providing a Phing build file along with a project is a good way of allowing automation of certain aspects of the project. The only trouble is that users won't know what's in the build file unless they open it or just run it.
Phing has a few different tasks and elements that allow you to select paths of code execution depending on what you need to happen in a build file. These are limited to loops and if statements, but a lot of functionality can be covered with just a couple of lines of XML.
The other day I was experimenting with Git hooks. These are scripts that you can execute before certain actions are run in Git. For example, you might want to ensure that forced updates are not run, ensuring respository files have the correct permissions after merging, or that the files have ASCII standard names before being committed.
I use Phing for a lot of different tasks, it helps me to automate things that I would otherwise mess up if left to my own devices. Prime candidates for Phing scripts are things that I don't do that much and forget how to do them, or that have a number of complex steps.
Checking Syntax Errors In PHP And JavaScript Using Phing
Running a simple syntax check over your files is a good way to save time. This can be when testing code but best practice is to not to even commit code that contains syntax errors.
Add new comment