array_shift($args) ); if (count($args)) { $options['elementMap'] = array_shift($args); } if (count($args)) { $options['encoding'] = array_shift($args); } if (count($args)) { $options['dateTimeFormat'] = array_shift($args); } } if (!array_key_exists('rootElement', $options)) { $options['rootElement'] = 'logEntry'; } if (!array_key_exists('encoding', $options)) { $options['encoding'] = 'UTF-8'; } $this->rootElement = $options['rootElement']; $this->setEncoding($options['encoding']); if (array_key_exists('elementMap', $options)) { $this->elementMap = $options['elementMap']; } if (array_key_exists('dateTimeFormat', $options)) { $this->setDateTimeFormat($options['dateTimeFormat']); } } /** * Get encoding * * @return string */ public function getEncoding() { return $this->encoding; } /** * Set encoding * * @param string $value * @return Xml */ public function setEncoding($value) { $this->encoding = (string) $value; return $this; } /** * Set Escaper instance * * @param Escaper $escaper * @return Xml */ public function setEscaper(Escaper $escaper) { $this->escaper = $escaper; return $this; } /** * Get Escaper instance * * Lazy-loads an instance with the current encoding if none registered. * * @return Escaper */ public function getEscaper() { if (null === $this->escaper) { $this->setEscaper(new Escaper($this->getEncoding())); } return $this->escaper; } /** * Formats data into a single line to be written by the writer. * * @param array $event event data * @return string formatted line to write to the log */ public function format($event) { if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTime) { $event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat()); } if ($this->elementMap === null) { $dataToInsert = $event; } else { $dataToInsert = array(); foreach ($this->elementMap as $elementName => $fieldKey) { $dataToInsert[$elementName] = $event[$fieldKey]; } } $enc = $this->getEncoding(); $escaper = $this->getEscaper(); $dom = new DOMDocument('1.0', $enc); $elt = $dom->appendChild(new DOMElement($this->rootElement)); foreach ($dataToInsert as $key => $value) { if (empty($value) || is_scalar($value) || (is_object($value) && method_exists($value, '__toString')) ) { if ($key == "message") { $value = $escaper->escapeHtml($value); } elseif ($key == "extra" && empty($value)) { continue; } $elt->appendChild(new DOMElement($key, (string) $value)); } } $xml = $dom->saveXML(); $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml); return $xml . PHP_EOL; } /** * {@inheritDoc} */ public function getDateTimeFormat() { return $this->dateTimeFormat; } /** * {@inheritDoc} */ public function setDateTimeFormat($dateTimeFormat) { $this->dateTimeFormat = (string) $dateTimeFormat; return $this; } }