运行phing脚本时,它将把内容打印到控制台。这些消息要么是系统消息(例如BUILD STARTED),要么是您已在build.xml文件中放入的回显消息。所有这些输出均由记录器文件控制和创建。默认记录器称为(毫不奇怪)DefaultLogger,它将用作默认记录器。可以使用几种不同类型的记录器,所有这些记录器都可以在PEAR \ phing目录的侦听器文件夹中找到。
要选择要使用的其他记录器脚本,只需在phing调用中使用-logger标志。要指定DefaultLogger,请使用以下命令:
phing -logger phing.listener.DefaultLogger
若要指定XML记录器,该记录器将创建有关当前工作目录中发生的网络钓鱼事件的XML文档,请使用XmlLogger记录器。
phing -logger phing.listener.XmlLogger
那么,如果我们只想在构建文件中打印出回显消息,该怎么办?我们没有可以使用的记录器,因此可以简单地创建自己的解决方案。以下代码块包含一个记录器,该记录器将仅输出echo命令以及发生的任何错误以及完成构建所花费的时间。创建一个名为的文件EchoLogger.php,并将其放入PEAR \ phing \ listener目录。该文件中的许多注释来自DefaultLogger,我刚刚更改了不同的部分或不适用的部分。
<?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); } }
现在,您可以使用以下命令来调用新的记录器。
phing -logger phing.listener.EchoLogger
仅打印回显命令以及运行脚本所花费的时间。如果遇到任何错误,将以正常方式打印。