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 . "
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 = "