Index: src/main/php/appenders/LoggerAppenderAdodb.php =================================================================== --- src/main/php/appenders/LoggerAppenderAdodb.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderAdodb.php (working copy) @@ -146,7 +146,7 @@ $this->canAppend = true; } - function append(LoggerLoggingEvent $event) { + function append(LoggerBaseLoggingEvent $event) { if ($this->canAppend) { $query = $this->layout->format($event); $this->db->Execute($query); Index: src/main/php/appenders/LoggerAppenderConsole.php =================================================================== --- src/main/php/appenders/LoggerAppenderConsole.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderConsole.php (working copy) @@ -103,7 +103,7 @@ } } - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { if (is_resource($this->fp) && $this->layout !== null) { fwrite($this->fp, $this->layout->format($event)); } Index: src/main/php/appenders/LoggerAppenderEcho.php =================================================================== --- src/main/php/appenders/LoggerAppenderEcho.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderEcho.php (working copy) @@ -73,7 +73,7 @@ $this->closed = true; } - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { if($this->layout !== null) { if($this->firstAppend) { echo $this->layout->getHeader(); Index: src/main/php/appenders/LoggerAppenderFile.php =================================================================== --- src/main/php/appenders/LoggerAppenderFile.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderFile.php (working copy) @@ -105,7 +105,7 @@ } } - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { if($this->fp and $this->layout !== null) { if(flock($this->fp, LOCK_EX)) { fwrite($this->fp, $this->layout->format($event)); Index: src/main/php/appenders/LoggerAppenderMail.php =================================================================== --- src/main/php/appenders/LoggerAppenderMail.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderMail.php (working copy) @@ -123,7 +123,7 @@ $this->dry = $dry; } - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { if($this->layout !== null) { $this->body .= $this->layout->format($event); } Index: src/main/php/appenders/LoggerAppenderMailEvent.php =================================================================== --- src/main/php/appenders/LoggerAppenderMailEvent.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderMailEvent.php (working copy) @@ -143,7 +143,7 @@ $this->dry = $dry; } - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { $smtpHost = $this->smtpHost; $prevSmtpHost = ini_get('SMTP'); if(!empty($smtpHost)) { Index: src/main/php/appenders/LoggerAppenderNull.php =================================================================== --- src/main/php/appenders/LoggerAppenderNull.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderNull.php (working copy) @@ -54,7 +54,7 @@ * * @param LoggerLoggingEvent $event */ - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { } } Index: src/main/php/appenders/LoggerAppenderPDO.php =================================================================== --- src/main/php/appenders/LoggerAppenderPDO.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderPDO.php (working copy) @@ -174,7 +174,7 @@ * * @throws LoggerException If the pattern conversion or the INSERT statement fails. */ - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { // TODO: Can't activateOptions() simply throw an Exception if it encounters problems? if ( ! $this->canAppend) return; Index: src/main/php/appenders/LoggerAppenderPhp.php =================================================================== --- src/main/php/appenders/LoggerAppenderPhp.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderPhp.php (working copy) @@ -57,7 +57,7 @@ $this->closed = true; } - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { if($this->layout !== null) { $level = $event->getLevel(); if($level->isGreaterOrEqual(LoggerLevel::getLevelError())) { Index: src/main/php/appenders/LoggerAppenderRollingFile.php =================================================================== --- src/main/php/appenders/LoggerAppenderRollingFile.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderRollingFile.php (working copy) @@ -221,7 +221,7 @@ /** * @param LoggerLoggingEvent $event */ - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { parent::append($event); if(ftell($this->fp) > $this->getMaximumFileSize()) { $this->rollOver(); Index: src/main/php/appenders/LoggerAppenderSocket.php =================================================================== --- src/main/php/appenders/LoggerAppenderSocket.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderSocket.php (working copy) @@ -230,7 +230,7 @@ $this->useXml = LoggerOptionConverter::toBoolean($flag, $this->getUseXml()); } - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { if($this->sp || $this->dry) { if($this->getLocationInfo()) { $event->getLocationInformation(); Index: src/main/php/appenders/LoggerAppenderSyslog.php =================================================================== --- src/main/php/appenders/LoggerAppenderSyslog.php (revision 950916) +++ src/main/php/appenders/LoggerAppenderSyslog.php (working copy) @@ -172,7 +172,7 @@ } } - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { if($this->_option == NULL){ $this->_option = LOG_PID | LOG_CONS; } Index: src/main/php/layouts/LoggerLayoutHtml.php =================================================================== --- src/main/php/layouts/LoggerLayoutHtml.php (revision 950916) +++ src/main/php/layouts/LoggerLayoutHtml.php (working copy) @@ -129,7 +129,7 @@ * @param LoggerLoggingEvent $event * @return string */ - public function format(LoggerLoggingEvent $event) { + public function format(LoggerBaseLoggingEvent $event) { $sbuf = PHP_EOL . "" . PHP_EOL; $sbuf .= ""; Index: src/main/php/layouts/LoggerLayoutPattern.php =================================================================== --- src/main/php/layouts/LoggerLayoutPattern.php (revision 950916) +++ src/main/php/layouts/LoggerLayoutPattern.php (working copy) @@ -182,7 +182,7 @@ * @param LoggerLoggingEvent $event * @return string */ - public function format(LoggerLoggingEvent $event) { + public function format(LoggerBaseLoggingEvent $event) { $sbuf = ''; $c = $this->head; while ($c !== null) { @@ -202,7 +202,7 @@ * * @return array(string) An array of the converted elements i.e. timestamp, message, filename etc. */ - public function formatToArray(LoggerLoggingEvent $event) { + public function formatToArray(LoggerBaseLoggingEvent $event) { $results = array(); $c = $this->head; while ($c !== null) { Index: src/main/php/layouts/LoggerLayoutSimple.php =================================================================== --- src/main/php/layouts/LoggerLayoutSimple.php (revision 950916) +++ src/main/php/layouts/LoggerLayoutSimple.php (working copy) @@ -54,7 +54,7 @@ * @param LoggerLoggingEvent $event * @return string */ - public function format(LoggerLoggingEvent $event) { + public function format(LoggerBaseLoggingEvent $event) { $level = $event->getLevel(); return $level->toString() . ' - ' . $event->getRenderedMessage(). PHP_EOL; } Index: src/main/php/layouts/LoggerLayoutTTCC.php =================================================================== --- src/main/php/layouts/LoggerLayoutTTCC.php (revision 950916) +++ src/main/php/layouts/LoggerLayoutTTCC.php (working copy) @@ -171,10 +171,10 @@ * returned string includes time, thread, category. *

Time, thread, category are printed depending on options. * - * @param LoggerLoggingEvent $event + * @param LoggerBaseLoggingEvent $event * @return string */ - public function format(LoggerLoggingEvent $event) { + public function format(LoggerBaseLoggingEvent $event) { $timeStamp = (float)$event->getTimeStamp(); $format = strftime($this->dateFormat, (int)$timeStamp); Index: src/main/php/layouts/LoggerLayoutXml.php =================================================================== --- src/main/php/layouts/LoggerLayoutXml.php (revision 950916) +++ src/main/php/layouts/LoggerLayoutXml.php (working copy) @@ -117,7 +117,7 @@ * @param LoggerLoggingEvent $event * @return string */ - public function format(LoggerLoggingEvent $event) { + public function format(LoggerBaseLoggingEvent $event) { $loggerName = $event->getLoggerName(); $timeStamp = number_format((float)($event->getTimeStamp() * 1000), 0, '', ''); $thread = $event->getThreadName(); Index: src/main/php/Logger.php =================================================================== --- src/main/php/Logger.php (revision 950916) +++ src/main/php/Logger.php (working copy) @@ -111,7 +111,9 @@ 'LoggerRendererException' => '/renderers/LoggerRendererException.php', 'LoggerLocationInfo' => '/LoggerLocationInfo.php', 'LoggerThrowableInformation' => '/LoggerThrowableInformation.php', + 'LoggerBaseLoggingEvent' => '/LoggerLoggingEvent.php', 'LoggerLoggingEvent' => '/LoggerLoggingEvent.php', + 'LoggerLoggingFormatEvent' => '/LoggerLoggingEvent.php', 'LoggerFilter' => '/LoggerFilter.php', 'LoggerFilterDenyAll' => '/filters/LoggerFilterDenyAll.php', 'LoggerFilterLevelMatch' => '/filters/LoggerFilterLevelMatch.php', @@ -268,8 +270,80 @@ public function fatal($message, $caller = null) { $this->logLevel($message, LoggerLevel::getLevelFatal(), $caller); } - + /** + * Logs a formatted message with TRACE level + * + * @param string $format Message format + * @param mixed $args Message arguments + */ + public function traceFormat($format, $args = null) { + $args = func_get_args(); + array_shift($args); + $this->logLevelFormat(LoggerLevel::getLevelTrace(), $format, $args); + } + + /** + * Logs a formatted message with DEBUG level + * + * @param string $format Message format + * @param mixed $args Message arguments + */ + public function debugFormat($format, $args = null) { + $args = func_get_args(); + array_shift($args); + $this->logLevelFormat(LoggerLevel::getLevelDebug(), $format, $args); + } + + /** + * Logs a formatted message with INFO level + * + * @param string $format Message format + * @param mixed $args Message arguments + */ + public function infoFormat($format, $args = null) { + $args = func_get_args(); + array_shift($args); + $this->logLevelFormat(LoggerLevel::getLevelInfo(), $format, $args); + } + + /** + * Logs a formatted message with WARN level + * + * @param string $format Message format + * @param mixed $args Message arguments + */ + public function warnFormat($format, $args = null) { + $args = func_get_args(); + array_shift($args); + $this->logLevelFormat(LoggerLevel::getLevelWarn(), $format, $args); + } + + /** + * Logs a formatted message with ERROR level + * + * @param string $format Message format + * @param mixed $args Message arguments + */ + public function errorFormat($format, $args = null) { + $args = func_get_args(); + array_shift($args); + $this->logLevelFormat(LoggerLevel::getLevelError(), $format, $args); + } + + /** + * Logs a formatted message with FATAL level + * + * @param string $format Message format + * @param mixed $args Message arguments + */ + public function fatalFormat($format, $args = null) { + $args = func_get_args(); + array_shift($args); + $this->logLevelFormat(LoggerLevel::getLevelFatal(), $format, $args); + } + + /** * This method creates a new logging event and logs the event without further checks. * * It should not be called directly. Use {@link info()}, {@link debug()}, {@link warn()}, @@ -288,9 +362,13 @@ } $this->callAppenders(new LoggerLoggingEvent($fqcn, $this, $level, $message, null, $throwable)); - } + } + + public function forcedLogFormat($fqcn, $level, $format, $args) + { + $this->callAppenders(new LoggerLoggingFormatEvent($fqcn, $this, $level, $format, $args, null)); + } - /** * Check whether this category is enabled for the DEBUG Level. * @return boolean @@ -347,7 +425,13 @@ if($level->isGreaterOrEqual($this->getEffectiveLevel())) { $this->forcedLog($this->fqcn, $caller, $level, $message); } - } + } + + private function logLevelFormat($level, $format, $args = null) { + if($level->isGreaterOrEqual($this->getEffectiveLevel())) { + $this->forcedLogFormat($this->fqcn, $level, $format, $args); + } + } /* Factory methods */ Index: src/main/php/LoggerAppender.php =================================================================== --- src/main/php/LoggerAppender.php (revision 950916) +++ src/main/php/LoggerAppender.php (working copy) @@ -114,7 +114,7 @@ * @see LoggerAppender::doAppend() * @param LoggerLoggingEvent $event */ - public function doAppend(LoggerLoggingEvent $event) { + public function doAppend(LoggerBaseLoggingEvent $event) { if($this->closed) { return; } @@ -249,7 +249,7 @@ * @see doAppend() * @abstract */ - abstract protected function append(LoggerLoggingEvent $event); + abstract protected function append(LoggerBaseLoggingEvent $event); /** * Release any resources allocated. Index: src/main/php/LoggerLayout.php =================================================================== --- src/main/php/LoggerLayout.php (revision 950916) +++ src/main/php/LoggerLayout.php (working copy) @@ -40,7 +40,7 @@ * @param LoggerLoggingEvent * @return string */ - public function format(LoggerLoggingEvent $event) { + public function format(LoggerBaseLoggingEvent $event) { return $event->getRenderedMessage(); } Index: src/main/php/LoggerLoggingEvent.php =================================================================== --- src/main/php/LoggerLoggingEvent.php (revision 950916) +++ src/main/php/LoggerLoggingEvent.php (working copy) @@ -19,24 +19,23 @@ */ /** - * The internal representation of logging event. + * Base class for logging event. * - * @version $Revision$ * @package log4php */ -class LoggerLoggingEvent { +abstract class LoggerBaseLoggingEvent { private static $startTime; /** * @var string Fully Qualified Class Name of the calling category class. */ - private $fqcn; + protected $fqcn; /** * @var Logger reference */ - private $logger = null; + protected $logger = null; /** * The category (logger) name. @@ -45,7 +44,7 @@ * Use the {@link getLoggerName()} method instead. * @deprecated */ - private $categoryName; + protected $categoryName; /** * Level of logging event. @@ -60,7 +59,7 @@ /** * @var string The nested diagnostic context (NDC) of logging event. */ - private $ndc; + protected $ndc; /** * Have we tried to do an NDC lookup? If we did, there is no need @@ -69,7 +68,7 @@ * (incorrect) NDC. See also writeObject method. * @var boolean */ - private $ndcLookupRequired = true; + protected $ndcLookupRequired = true; /** * Have we tried to do an MDC lookup? If we did, there is no need @@ -77,26 +76,21 @@ * serialized. See also the getMDC and getMDCCopy methods. * @var boolean */ - private $mdcCopyLookupRequired = true; + protected $mdcCopyLookupRequired = true; /** - * @var mixed The application supplied message of logging event. - */ - private $message; - - /** * The application supplied message rendered through the log4php * objet rendering mechanism. At present renderedMessage == message. * @var string */ - private $renderedMessage = null; + protected $renderedMessage = null; /** * The name of thread in which this logging event was generated. * log4php saves here the process id via {@link PHP_MANUAL#getmypid getmypid()} * @var mixed */ - private $threadName = null; + protected $threadName = null; /** * The number of seconds elapsed from 1/1/1970 until logging event @@ -108,14 +102,14 @@ /** * @var LoggerLocationInfo Location information for the caller. */ - private $locationInfo = null; + protected $locationInfo = null; /** * @var LoggerThrowableInformation log4php internal representation of throwable */ - private $throwableInfo = null; + protected $throwableInfo = null; - /** + /** * Instantiate a LoggingEvent from the supplied parameters. * *

Except {@link $timeStamp} all the other fields of @@ -124,11 +118,10 @@ * @param string $fqcn name of the caller class. * @param mixed $logger The {@link Logger} category of this event or the logger name. * @param LoggerLevel $priority The level of this event. - * @param mixed $message The message of this event. * @param integer $timeStamp the timestamp of this logging event. * @param Exception $throwable The throwable associated with logging event */ - public function __construct($fqcn, $logger, $priority, $message, $timeStamp = null, $throwable = null) { + public function __construct($fqcn, $logger, $priority, $timeStamp = null, $throwable = null) { $this->fqcn = $fqcn; if($logger instanceof Logger) { $this->logger = $logger; @@ -137,7 +130,6 @@ $this->categoryName = strval($logger); } $this->level = $priority; - $this->message = $message; if($timeStamp !== null && is_float($timeStamp)) { $this->timeStamp = $timeStamp; } else { @@ -158,7 +150,7 @@ * Returns the full qualified classname. * TODO: PHP does contain namespaces in 5.3. Those should be returned too, */ - public function getFullQualifiedClassname() { + public function getFullQualifiedClassname() { return $this->fqcn; } @@ -233,23 +225,6 @@ } /** - * Return the message for this logging event. - * - *

Before serialization, the returned object is the message - * passed by the user to generate the logging event. After - * serialization, the returned value equals the String form of the - * message possibly after object rendering. - * @return mixed - */ - public function getMessage() { - if($this->message !== null) { - return $this->message; - } else { - return $this->getRenderedMessage(); - } - } - - /** * This method returns the NDC for this event. It will return the * correct content even if the event was generated in a different * thread or even on a different machine. The {@link LoggerNDC::get()} method @@ -274,25 +249,10 @@ } /** - * Render message. + * Renders output message. * @return string */ - public function getRenderedMessage() { - if($this->renderedMessage === null and $this->message !== null) { - if(is_string($this->message)) { - $this->renderedMessage = $this->message; - } else { - // $this->logger might be null or an instance of Logger or RootLogger - // But in contrast to log4j, in log4php there is only have one LoggerHierarchy so there is - // no need figure out which one is $this->logger part of. - // TODO: Logger::getHierarchy() is marked @deprecated! - $repository = Logger::getHierarchy(); - $rendererMap = $repository->getRendererMap(); - $this->renderedMessage= $rendererMap->findAndRender($this->message); - } - } - return $this->renderedMessage; - } + public abstract function getRenderedMessage(); /** * Returns the time when the application started, in seconds @@ -365,6 +325,93 @@ 'level', 'ndc', 'ndcLookupRequired', + 'renderedMessage', + 'threadName', + 'timeStamp', + 'locationInfo', + ); + } +} + +/** + * The internal representation of logging event. + * + * @version $Revision$ + * @package log4php + */ +class LoggerLoggingEvent extends LoggerBaseLoggingEvent { + /** + * @var mixed The application supplied message of logging event. + */ + private $message; + + /** + * Instantiate a LoggingEvent from the supplied parameters. + * + *

Except {@link $timeStamp} all the other fields of + * LoggerLoggingEvent are filled when actually needed. + * + * @param string $fqcn name of the caller class. + * @param mixed $logger The {@link Logger} category of this event or the logger name. + * @param LoggerLevel $priority The level of this event. + * @param mixed $message The message of this event. + * @param integer $timeStamp the timestamp of this logging event. + * @param Exception $throwable The throwable associated with logging event + */ + public function __construct($fqcn, $logger, $priority, $message, $timeStamp = null, $throwable = null) { + parent::__construct($fqcn, $logger, $priority, $timeStamp, $throwable); + $this->message = $message; + } + + + /** + * Return the message for this logging event. + * + *

Before serialization, the returned object is the message + * passed by the user to generate the logging event. After + * serialization, the returned value equals the String form of the + * message possibly after object rendering. + * @return mixed + */ + public function getMessage() { + if($this->message !== null) { + return $this->message; + } else { + return $this->getRenderedMessage(); + } + } + + /** + * Render message. + * @return string + */ + public function getRenderedMessage() { + if($this->renderedMessage === null and $this->message !== null) { + if(is_string($this->message)) { + $this->renderedMessage = $this->message; + } else { + // $this->logger might be null or an instance of Logger or RootLogger + // But in contrast to log4j, in log4php there is only have one LoggerHierarchy so there is + // no need figure out which one is $this->logger part of. + // TODO: Logger::getHierarchy() is marked @deprecated! + $repository = Logger::getHierarchy(); + $rendererMap = $repository->getRendererMap(); + $this->renderedMessage= $rendererMap->findAndRender($this->message); + } + } + return $this->renderedMessage; + } + + /** + * Avoid serialization of the {@link $logger} object + */ + public function __sleep() { + return array( + 'fqcn', + 'categoryName', + 'level', + 'ndc', + 'ndcLookupRequired', 'message', 'renderedMessage', 'threadName', @@ -372,7 +419,92 @@ 'locationInfo', ); } +} +/** + * Representation of formatted logging event + */ +class LoggerLoggingFormatEvent extends LoggerBaseLoggingEvent +{ + /** + * sprintf-like format string + * @var string + */ + private $format; + + /** + * Array of arguments to format + * @var array + */ + private $args; + + /** + * Instantiate a LoggingEvent from the supplied parameters. + * + *

Except {@link $timeStamp} all the other fields of + * LoggerLoggingEvent are filled when actually needed. + * + * @param string $fqcn name of the caller class. + * @param mixed $logger The {@link Logger} category of this event or the logger name. + * @param LoggerLevel $priority The level of this event. + * @param string $format Format string for this event. + * @param array $args Arguments to format. + * @param integer $timeStamp the timestamp of this logging event. + */ + public function __construct($fqcn, $logger, $priority, $format, $args = null, $timeStamp = null) { + parent::__construct($fqcn, $logger, $priority, $timeStamp, null); + + $this->format = $format; + $this->args = $args; + } + + /** + * Renders output message. + * @return string + */ + public function getRenderedMessage() { + if($this->renderedMessage === null and $this->format !== null) { + if(empty($this->args)) { + $this->renderedMessage = $this->format; + } else { + // $this->logger might be null or an instance of Logger or RootLogger + // But in contrast to log4j, in log4php there is only have one LoggerHierarchy so there is + // no need figure out which one is $this->logger part of. + // TODO: Logger::getHierarchy() is marked @deprecated! + $repository = Logger::getHierarchy(); + $rendererMap = $repository->getRendererMap(); + + $sprintf_args = array($this->format); + foreach ($this->args as $arg) + { + $sprintf_args[] = $rendererMap->findAndRender($arg); + } + + $this->renderedMessage = call_user_func_array('sprintf', $sprintf_args); + } + } + return $this->renderedMessage; + } + + /** + * Avoid serialization of the {@link $logger} object + */ + public function __sleep() { + return array( + 'fqcn', + 'categoryName', + 'level', + 'ndc', + 'ndcLookupRequired', + 'format', + 'args', + 'renderedMessage', + 'threadName', + 'timeStamp', + 'locationInfo', + ); + } + } -LoggerLoggingEvent::getStartTime(); +LoggerBaseLoggingEvent::getStartTime(); Index: src/test/php/layouts/LoggerLayoutXmlTest.php =================================================================== --- src/test/php/layouts/LoggerLayoutXmlTest.php (revision 950916) +++ src/test/php/layouts/LoggerLayoutXmlTest.php (working copy) @@ -34,7 +34,7 @@ $e = "getThreadName(). "\" timestamp=\"".number_format((float)($event->getTimeStamp() * 1000), 0, '', '')."\">".PHP_EOL. "".PHP_EOL. - "".PHP_EOL. "".PHP_EOL . PHP_EOL; @@ -50,7 +50,7 @@ $e = "getThreadName(). "\" timestamp=\"".number_format((float)($event->getTimeStamp() * 1000), 0, '', '')."\">".PHP_EOL. "".PHP_EOL. - "".PHP_EOL. "".PHP_EOL . PHP_EOL; @@ -69,7 +69,7 @@ $e = "getThreadName(). "\" timestamp=\"".number_format((float)($event->getTimeStamp() * 1000), 0, '', '')."\">".PHP_EOL. "".PHP_EOL. - "".PHP_EOL. "".PHP_EOL . PHP_EOL; Index: src/test/php/LoggerLoggingEventTest.php =================================================================== --- src/test/php/LoggerLoggingEventTest.php (revision 950916) +++ src/test/php/LoggerLoggingEventTest.php (working copy) @@ -27,7 +27,7 @@ protected $requiresLayout = true; - public function append(LoggerLoggingEvent $event) { + public function append(LoggerBaseLoggingEvent $event) { $this->layout->format($event); } @@ -39,7 +39,7 @@ return; } - public function format(LoggerLoggingEvent $event) { + public function format(LoggerBaseLoggingEvent $event) { LoggerLoggingEventTest::$locationInfo = $event->getLocationInformation(); LoggerLoggingEventTest::$throwableInfo = $event->getThrowableInformation(); } Index: src/test/php/LoggerTest.php =================================================================== --- src/test/php/LoggerTest.php (revision 950916) +++ src/test/php/LoggerTest.php (working copy) @@ -56,6 +56,7 @@ $logger = Logger::getLogger('mylogger'); ob_start(); + $logger->trace('this is a trace'); $logger->info('this is an info'); $logger->warn('this is a warning'); $logger->error('this is an error'); @@ -65,7 +66,8 @@ $v = ob_get_contents(); ob_end_clean(); - $e = 'INFO - this is an info'.PHP_EOL; + $e = 'TRACE - this is a trace'.PHP_EOL; + $e .= 'INFO - this is an info'.PHP_EOL; $e .= 'WARN - this is a warning'.PHP_EOL; $e .= 'ERROR - this is an error'.PHP_EOL; $e .= 'DEBUG - this is a debug message'.PHP_EOL; @@ -122,6 +124,49 @@ Logger::configure('log4php.properties'); self::assertEquals('LoggerConfiguratorIni', Logger::getConfigurationClass()); self::assertEquals('log4php.properties', Logger::getConfigurationFile()); - } -} + + public function testFormattedLog() { + Logger::configure('LoggerTest.properties'); + $logger = Logger::getLogger('mylogger'); + + ob_start(); + $logger->traceFormat("None"); + $logger->debugFormat("One: %s", 1); + $logger->infoFormat("One: %s, Two: %s", 1, 2); + $logger->warnFormat("One: %s, Two: %s, Three: %s", 1, 2, 3); + $logger->errorFormat("One: %s, Two: %s, Three: %s, Four: %s", 1, 2, 3, 4); + $logger->fatalFormat("One: %s, Two: %s, Three: %s, Four: %s, Five: %s", 1, 2, 3, 4, 5); + + $v = ob_get_contents(); + ob_end_clean(); + + $e = 'TRACE - None'.PHP_EOL; + $e .= 'DEBUG - One: 1'.PHP_EOL; + $e .= 'INFO - One: 1, Two: 2'.PHP_EOL; + $e .= 'WARN - One: 1, Two: 2, Three: 3'.PHP_EOL; + $e .= 'ERROR - One: 1, Two: 2, Three: 3, Four: 4'.PHP_EOL; + $e .= 'FATAL - One: 1, Two: 2, Three: 3, Four: 4, Five: 5'.PHP_EOL; + + self::assertEquals($v, $e); + } + + public function testFormattingErrors() { + Logger::configure('LoggerTest.properties'); + $logger = Logger::getLogger('mylogger'); + + $success = false; + try + { + $logger->debugFormat("Format for three values: %s, %s, %s", 1, "abc"); + } + catch (PHPUnit_Framework_Error_Warning $e) + { + if ($e->getMessage() === "sprintf(): Too few arguments") + $success = true; + } + + if (!$success) + self::fail('Expected PHPUnit_Framework_Error_Warning with message "sprintf(): Too few arguments"'); + } +} \ No newline at end of file Index: src/test/php/LoggerTest.properties =================================================================== --- src/test/php/LoggerTest.properties (revision 950916) +++ src/test/php/LoggerTest.properties (working copy) @@ -17,5 +17,5 @@ log4php.appender.loggertestdefault.layout = LoggerLayoutSimple log4php.additivity.mylogger= "false" -log4php.logger.mylogger = DEBUG, loggertestdefault +log4php.logger.mylogger = ALL, loggertestdefault log4php.rootLogger = ERROR, loggertestdefault Index: src/test/php/renderers/LoggerRendererMapTest.php =================================================================== --- src/test/php/renderers/LoggerRendererMapTest.php (revision 950916) +++ src/test/php/renderers/LoggerRendererMapTest.php (working copy) @@ -80,5 +80,13 @@ ob_end_clean(); self::assertEquals("ERROR - test1,test2,test3" . PHP_EOL, $v); + + ob_start(); + $logger->errorFormat("Fruit3: %s", new Fruit3()); + $v = ob_get_contents(); + ob_end_clean(); + + self::assertEquals("ERROR - Fruit3: test1,test2,test3" . PHP_EOL, $v); + } } Index: src/test/php/renderers/test4.properties =================================================================== --- src/test/php/renderers/test4.properties (revision 950916) +++ src/test/php/renderers/test4.properties (working copy) @@ -16,6 +16,7 @@ log4php.renderer.Fruit3=FruitRenderer3 log4php.appender.default = LoggerAppenderEcho +log4php.appender.default.HtmlLineBreaks = false log4php.appender.default.layout = LoggerLayoutSimple log4php.appender.default.threshold = WARN