diff --git a/library/log4php/pattern/LoggerPatternConverter.php b/library/log4php/pattern/LoggerPatternConverter.php new file mode 100644 --- /dev/null +++ b/library/log4php/pattern/LoggerPatternConverter.php @@ -0,0 +1,128 @@ +Conversion specifiers in a conversion patterns are parsed to + * individual PatternConverters. Each of which is responsible for + * converting a logging event in a converter specific manner.

+ * + * @version $Revision: 1326626 $ + * @package log4php + * @subpackage helpers + * @since 0.3 + */ +abstract class LoggerPatternConverter { + + /** + * Next converter in the converter chain. + * @var LoggerPatternConverter + */ + public $next = null; + + /** + * Formatting information, parsed from pattern modifiers. + * @var LoggerFormattingInfo + */ + protected $formattingInfo; + + /** + * Converter-specific formatting options. + * @var array + */ + protected $option; + + /** + * Constructor + * @param LoggerFormattingInfo $formattingInfo + * @param array $option + */ + public function __construct(LoggerFormattingInfo $formattingInfo = null, $option = null) { + $this->formattingInfo = $formattingInfo; + $this->option = $option; + $this->activateOptions(); + } + + /** + * Called in constructor. Converters which need to process the options + * can override this method. + */ + public function activateOptions() { + } + + /** + * Converts the logging event to the desired format. Derived pattern + * converters must implement this method. + * + * @param LoggerLoggingEvent $event + */ + abstract public function convert(LoggerLoggingEvent $event); + + /** + * Converts the event and formats it according to setting in the + * Formatting information object. + * + * @param string &$sbuf string buffer to write to + * @param LoggerLoggingEvent $event Event to be formatted. + */ + public function format(&$sbuf, $event) { + $string = $this->convert($event); + + if (!isset($this->formattingInfo)) { + $sbuf .= $string; + return; + } + + $fi = $this->formattingInfo; + + // Empty string + if ($string === '' || is_null($string)) { + if ($fi->min > 0) { + $sbuf .= str_repeat(' ', $fi->min); + } + return; + } + + $len = strlen($string); + + // Trim the string if needed + if ($len > $fi->max) { + if ($fi->trimLeft) { + $sbuf .= substr($string, $len - $fi->max, $fi->max); + } else { + $sbuf .= substr($string, 0, $fi->max); + } + } // Add padding if needed + else if ($len < $fi->min) { + if ($fi->padLeft) { + $sbuf .= str_repeat(' ', $fi->min - $len); + $sbuf .= $string; + } else { + $sbuf .= $string; + $sbuf .= str_repeat(' ', $fi->min - $len); + } + } // No action needed + else { + $sbuf .= $string; + } + } +}