Using Different Loggers In Phing

17th November 2009 - 7 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.

  1. <?php
  2. require_once 'phing/BuildListener.php';
  3. include_once 'phing/BuildEvent.php';
  4.  
  5. /**
  6.  * Writes all echo messages generated during a build event to the console.
  7.  *
  8.  * This is the only output generated by the logger with two exceptions. These
  9.  * are the total time taken for the build to run and any errors that occurred
  10.  * during the build.
  11.  *
  12.  * @author Philip Norton
  13.  * @see BuildEvent
  14.  * @package phing.listener
  15.  */
  16. class EchoLogger implements BuildListener {
  17.  
  18. /**
  19.   * Size of the left column in output. The default char width is 12.
  20.   * @var int
  21.   */
  22. const LEFT_COLUMN_SIZE = 12;
  23.  
  24. /**
  25.   * The message output level that should be used. The default is
  26.   * PROJECT_MSG_VERBOSE.
  27.   * @var int
  28.   */
  29. protected $msgOutputLevel = PROJECT_MSG_ERR;
  30.  
  31. /**
  32.   * Time that the build started
  33.   * @var int
  34.   */
  35. protected $startTime;
  36.  
  37. /**
  38.   * Char that should be used to seperate lines. Default is the system
  39.   * property <em>line.seperator</em>.
  40.   * @var string
  41.   */
  42. protected $lSep;
  43.  
  44. /**
  45.   * Construct a new default logger.
  46.   */
  47. public function __construct() {
  48. $this->lSep = Phing::getProperty("line.separator");
  49. }
  50.  
  51. /**
  52.   * Set the msgOutputLevel this logger is to respond to.
  53.   *
  54.   * Only messages with a message level lower than or equal to the given
  55.   * level are output to the log.
  56.   *
  57.   * <p> Constants for the message levels are in Project.php. The order of
  58.   * the levels, from least to most verbose, is:
  59.   *
  60.   * <ul>
  61.   * <li>PROJECT_MSG_ERR</li>
  62.   * <li>PROJECT_MSG_WARN</li>
  63.   * <li>PROJECT_MSG_INFO</li>
  64.   * <li>PROJECT_MSG_VERBOSE</li>
  65.   * <li>PROJECT_MSG_DEBUG</li>
  66.   * </ul>
  67.   *
  68.   * The default message level for DefaultLogger is PROJECT_MSG_ERR.
  69.   *
  70.   * @param integer the logging level for the logger.
  71.   * @access public
  72.   */
  73. function setMessageOutputLevel($level) {
  74. $this->msgOutputLevel = (int) $level;
  75. }
  76.  
  77. /**
  78.   * Sets the start-time when the build started. Used for calculating
  79.   * the build-time.
  80.   *
  81.   * @param object The BuildEvent
  82.   * @access public
  83.   */
  84. function buildStarted(BuildEvent $event) {
  85. $this->startTime = Phing::currentTimeMillis();
  86. }
  87.  
  88. /**
  89.   * Prints whether the build failed, and any errors that occured during
  90.   the build. Also outputs the total build-time on completion.
  91.   *
  92.   * @param object The BuildEvent
  93.   * @access public
  94.   * @see BuildEvent::getException()
  95.   */
  96. function buildFinished(BuildEvent $event) {
  97. $error = $event->getException();
  98. if ($error !== null) {
  99. print($this->lSep . "BUILD FAILED" . $this->lSep);
  100. if (PROJECT_MSG_VERBOSE <= $this->msgOutputLevel || !($error instanceof BuildException)) {
  101. print($error->__toString().$this->lSep);
  102. } else {
  103. print($error->getMessage());
  104. }
  105. }
  106. print($this->lSep . "Total time: " .$this->_formatTime(Phing::currentTimeMillis() - $this->startTime) . $this->lSep);
  107. }
  108.  
  109. /**
  110.   * Fired when a target has started. We don't need specific action on this
  111.   * event. So the methods are empty.
  112.   *
  113.   * @param object The BuildEvent
  114.   * @access public
  115.   * @see BuildEvent::getTarget()
  116.   */
  117. function targetStarted(BuildEvent $event) {}
  118.  
  119. /**
  120.   * Fired when a target has finished. We don't need specific action on this
  121.   * event. So the methods are empty.
  122.   *
  123.   * @param object The BuildEvent
  124.   * @access public
  125.   * @see BuildEvent::getException()
  126.   */
  127. function targetFinished(BuildEvent $event) {}
  128.  
  129. /**
  130.   * Fired when a task is started. We don't need specific action on this
  131.   * event. So the methods are empty.
  132.   *
  133.   * @param object The BuildEvent
  134.   * @access public
  135.   * @see BuildEvent::getTask()
  136.   */
  137. function taskStarted(BuildEvent $event) {}
  138.  
  139. /**
  140.   * Fired when a task has finished. We don't need specific action on this
  141.   * event. So the methods are empty.
  142.   *
  143.   * @param object The BuildEvent
  144.   * @access public
  145.   * @see BuildEvent::getException()
  146.   */
  147. function taskFinished(BuildEvent $event) {}
  148.  
  149. /**
  150.   * Print any echo messages to the stdout.
  151.   *
  152.   * @param object The BuildEvent
  153.   * @access public
  154.   * @see BuildEvent::getMessage()
  155.   */
  156. function messageLogged(BuildEvent $event) {
  157. if ($event->getPriority() <= $this->msgOutputLevel) {
  158. $msg = "";
  159. if ($event->getTask() !== null) {
  160. $name = $event->getTask();
  161. $name = $name->getTaskName();
  162. if ($name == 'echo') {
  163. $msg = "[$name] ";
  164. $msg .= $event->getMessage();
  165. $this->printMessage($msg, $event->getPriority());
  166. }
  167. }
  168. }
  169. }
  170.  
  171. /**
  172.   * Formats a time micro integer to human readable format.
  173.   *
  174.   * @param integer The time stamp
  175.   * @access private
  176.   */
  177. function _formatTime($micros) {
  178. $seconds = $micros;
  179. $minutes = $seconds / 60;
  180. if ($minutes > 1) {
  181. return sprintf("%1.0f minute%s %0.2f second%s",
  182. $minutes, ($minutes === 1 ? " " : "s "),
  183. $seconds - floor($seconds/60) * 60, ($seconds%60 === 1 ? "" : "s"));
  184. } else {
  185. return sprintf("%0.4f second%s", $seconds, ($seconds%60 === 1 ? "" : "s"));
  186. }
  187. }
  188.  
  189. /**
  190.   * Prints a message to console.
  191.   *
  192.   * @param string $message The message to print.
  193.   * Should not be null.
  194.   * @param int $priority The priority of the message.
  195.   * (Ignored in this implementation.)
  196.   * @return void
  197.   */
  198. protected function printMessage($message, $priority) {
  199. print($message . $this->lSep);
  200. }
  201. }

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.

Add new comment

The content of this field is kept private and will not be shown publicly.