diff --git a/library/log4php/layouts/LoggerLayoutTTCC.php b/library/log4php/layouts/LoggerLayoutTTCC.php new file mode 100644 --- /dev/null +++ b/library/log4php/layouts/LoggerLayoutTTCC.php @@ -0,0 +1,201 @@ +time, thread, category and nested + * diagnostic context information, hence the name. + * + *

Each of the four fields can be individually enabled or + * disabled. The time format depends on the DateFormat used.

+ * + *

If no dateFormat is specified it defaults to '%c'. + * See php {@link PHP_MANUAL#date} function for details.

+ * + * Configurable parameters for this layout are: + * - {@link $threadPrinting} (true|false) enable/disable pid reporting. + * - {@link $categoryPrefixing} (true|false) enable/disable logger category reporting. + * - {@link $contextPrinting} (true|false) enable/disable NDC reporting. + * - {@link $microSecondsPrinting} (true|false) enable/disable micro seconds reporting in timestamp. + * - {@link $dateFormat} (string) set date format. See php {@link PHP_MANUAL#date} function for details. + * + * An example how to use this layout: + * + * {@example ../../examples/php/layout_ttcc.php 19}
+ * + * {@example ../../examples/resources/layout_ttcc.properties 18}
+ * + * The above would print:
+ * 02:28 [13714] INFO root - Hello World! + * + * @version $Revision: 1302503 $ + * @package log4php + * @subpackage layouts + * + * @deprecated LoggerLayout TTCC is deprecated and will be removed in a future release. Please use + * LoggerLayoutPattern instead. + */ +class LoggerLayoutTTCC extends LoggerLayout { + + // Internal representation of options + protected $threadPrinting = true; + protected $categoryPrefixing = true; + protected $contextPrinting = true; + protected $microSecondsPrinting = true; + + /** + * @var string date format. See {@link PHP_MANUAL#strftime} for details + */ + protected $dateFormat = '%c'; + + /** + * Constructor + * + * @param string date format + * @see dateFormat + */ + public function __construct($dateFormat = '') { + $this->warn("LoggerLayout TTCC is deprecated and will be removed in a future release. Please use LoggerLayoutPattern instead."); + if (!empty($dateFormat)) { + $this->dateFormat = $dateFormat; + } + return; + } + + /** + * The ThreadPrinting option specifies whether the name of the + * current thread is part of log output or not. This is true by default. + */ + public function setThreadPrinting($threadPrinting) { + $this->setBoolean('threadPrinting', $threadPrinting); + } + + /** + * @return boolean Returns value of the ThreadPrinting option. + */ + public function getThreadPrinting() { + return $this->threadPrinting; + } + + /** + * The CategoryPrefixing option specifies whether {@link Category} + * name is part of log output or not. This is true by default. + */ + public function setCategoryPrefixing($categoryPrefixing) { + $this->setBoolean('categoryPrefixing', $categoryPrefixing); + } + + /** + * @return boolean Returns value of the CategoryPrefixing option. + */ + public function getCategoryPrefixing() { + return $this->categoryPrefixing; + } + + /** + * The ContextPrinting option specifies log output will include + * the nested context information belonging to the current thread. + * This is true by default. + */ + public function setContextPrinting($contextPrinting) { + $this->setBoolean('contextPrinting', $contextPrinting); + } + + /** + * @return boolean Returns value of the ContextPrinting option. + */ + public function getContextPrinting() { + return $this->contextPrinting; + } + + /** + * The MicroSecondsPrinting option specifies if microseconds infos + * should be printed at the end of timestamp. + * This is true by default. + */ + public function setMicroSecondsPrinting($microSecondsPrinting) { + $this->setBoolean('microSecondsPrinting', $microSecondsPrinting); + } + + /** + * @return boolean Returns value of the MicroSecondsPrinting option. + */ + public function getMicroSecondsPrinting() { + return $this->microSecondsPrinting; + } + + + public function setDateFormat($dateFormat) { + $this->setString('dateFormat', $dateFormat); + } + + /** + * @return string + */ + public function getDateFormat() { + return $this->dateFormat; + } + + /** + * In addition to the level of the statement and message, the + * returned string includes time, thread, category. + *

Time, thread, category are printed depending on options. + * + * @param LoggerLoggingEvent $event + * @return string + */ + public function format(LoggerLoggingEvent $event) { + $timeStamp = (float)$event->getTimeStamp(); + $format = strftime($this->dateFormat, (int)$timeStamp); + + if ($this->microSecondsPrinting) { + $usecs = floor(($timeStamp - (int)$timeStamp) * 1000); + $format .= sprintf(',%03d', $usecs); + } + + $format .= ' '; + + if ($this->threadPrinting) { + $format .= '[' . getmypid() . '] '; + } + + $level = $event->getLevel(); + $format .= $level . ' '; + + if ($this->categoryPrefixing) { + $format .= $event->getLoggerName() . ' '; + } + + if ($this->contextPrinting) { + $ndc = $event->getNDC(); + if ($ndc != null) { + $format .= $ndc . ' '; + } + } + + $format .= '- ' . $event->getRenderedMessage(); + $format .= PHP_EOL; + + return $format; + } + + public function ignoresThrowable() { + return true; + } +}