Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'upstream/master' into module-class-map-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 20 changed files with 570 additions and 149 deletions.
4 changes: 2 additions & 2 deletions src/Filter/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function __construct($regex)
}
ErrorHandler::start(E_WARNING);
$result = preg_match($regex, '');
ErrorHandler::stop();
$error = ErrorHandler::stop();
if ($result === false) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid regular expression "%s"',
$regex
));
), 0, $error);
}
$this->regex = $regex;
}
Expand Down
114 changes: 114 additions & 0 deletions src/Formatter/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace Zend\Log\Formatter;

use DateTime;
use Traversable;
use Zend\Log\Exception;

/**
* @category Zend
* @package Zend_Log
* @subpackage Formatter
*/
class Base implements FormatterInterface
{
/**
* Format specifier for DateTime objects in event data (default: ISO 8601)
*
* @see http://php.net/manual/en/function.date.php
* @var string
*/
protected $dateTimeFormat = self::DEFAULT_DATETIME_FORMAT;

/**
* Class constructor
*
* @see http://php.net/manual/en/function.date.php
* @param null|string $dateTimeFormat Format for DateTime objects
*/
public function __construct($dateTimeFormat = null)
{
if (null !== $dateTimeFormat) {
$this->dateTimeFormat = $dateTimeFormat;
}
}

/**
* Formats data to be written by the writer.
*
* @param array $event event data
* @return array
*/
public function format($event)
{
foreach ($event as $key => $value) {
// Keep extra as an array
if ('extra' === $key) {
$event[$key] = self::format($value);
} else {
$event[$key] = $this->normalize($value);
}
}

return $event;
}

/**
* Normalize all non-scalar data types (except null) in a string value
*
* @param mixed $value
* @return mixed
*/
protected function normalize($value)
{
if (is_scalar($value) || null === $value) {
return $value;
}

if ($value instanceof DateTime) {
$value = $value->format($this->getDateTimeFormat());
} elseif (is_array($value) || $value instanceof Traversable) {
if ($value instanceof Traversable) {
$value = iterator_to_array($value);
}
foreach ($value as $key => $subvalue) {
$value[$key] = $this->normalize($subvalue);
}
$value = json_encode($value);
} elseif (is_object($value) && !method_exists($value,'__toString')) {
$value = sprintf('object(%s) %s', get_class($value), json_encode($value));
} elseif (is_resource($value)) {
$value = sprintf('resource(%s)', get_resource_type($value));
} elseif (!is_object($value)) {
$value = gettype($value);
}

return (string) $value;
}

/**
* {@inheritDoc}
*/
public function getDateTimeFormat()
{
return $this->dateTimeFormat;
}

/**
* {@inheritDoc}
*/
public function setDateTimeFormat($dateTimeFormat)
{
$this->dateTimeFormat = (string) $dateTimeFormat;
return $this;
}
}
78 changes: 78 additions & 0 deletions src/Formatter/Db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace Zend\Log\Formatter;

use DateTime;
use Zend\Log\Exception;

/**
* @category Zend
* @package Zend_Log
* @subpackage Formatter
*/
class Db implements FormatterInterface
{
/**
* Format specifier for DateTime objects in event data (default: ISO 8601)
*
* @see http://php.net/manual/en/function.date.php
* @var string
*/
protected $dateTimeFormat = self::DEFAULT_DATETIME_FORMAT;

/**
* Class constructor
*
* @see http://php.net/manual/en/function.date.php
* @param null|string $dateTimeFormat Format specifier for DateTime objects in event data
*/
public function __construct($dateTimeFormat = null)
{
if (null !== $dateTimeFormat) {
$this->setDateTimeFormat($dateTimeFormat);
}
}

/**
* Formats data to be written by the writer.
*
* @param array $event event data
* @return array
*/
public function format($event)
{
$format = $this->getDateTimeFormat();
array_walk_recursive($event, function (&$value) use ($format) {
if ($value instanceof DateTime) {
$value = $value->format($format);
}
});

return $event;
}

/**
* {@inheritDoc}
*/
public function getDateTimeFormat()
{
return $this->dateTimeFormat;
}

/**
* {@inheritDoc}
*/
public function setDateTimeFormat($dateTimeFormat)
{
$this->dateTimeFormat = (string) $dateTimeFormat;
return $this;
}
}
57 changes: 14 additions & 43 deletions src/Formatter/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* @package Zend_Log
* @subpackage Formatter
*/
class Simple implements FormatterInterface
class Simple extends Base
{
const DEFAULT_FORMAT = '%timestamp% %priorityName% (%priority%): %message% %info%';
const DEFAULT_FORMAT = '%timestamp% %priorityName% (%priority%): %message% %extra%';

/**
* Format specifier for log messages
Expand All @@ -29,14 +29,6 @@ class Simple implements FormatterInterface
*/
protected $format;

/**
* Format specifier for DateTime objects in event data (default: ISO 8601)
*
* @see http://php.net/manual/en/function.date.php
* @var string
*/
protected $dateTimeFormat = self::DEFAULT_DATETIME_FORMAT;

/**
* Class constructor
*
Expand All @@ -53,9 +45,7 @@ public function __construct($format = null, $dateTimeFormat = null)

$this->format = isset($format) ? $format : static::DEFAULT_FORMAT;

if (isset($dateTimeFormat)) {
$this->dateTimeFormat = $dateTimeFormat;
}
parent::__construct($dateTimeFormat);
}

/**
Expand All @@ -68,41 +58,22 @@ public function format($event)
{
$output = $this->format;

if (!isset($event['info'])) {
$event['info'] = '';
}

if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTime) {
$event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat());
}

$event = parent::format($event);
foreach ($event as $name => $value) {
if ((is_object($value) && !method_exists($value,'__toString'))
|| is_array($value)
) {
$value = gettype($value);
if ('extra' == $name && count($value)) {
$value = $this->normalize($value);
} elseif ('extra' == $name) {
// Don't print an empty array
$value = '';
}

$output = str_replace("%$name%", $value, $output);
}

if (isset($event['extra']) && empty($event['extra'])
&& false !== strpos($this->format, '%extra%')
) {
$output = rtrim($output, ' ');
}
return $output;
}

/**
* {@inheritDoc}
*/
public function getDateTimeFormat()
{
return $this->dateTimeFormat;
}

/**
* {@inheritDoc}
*/
public function setDateTimeFormat($dateTimeFormat)
{
$this->dateTimeFormat = (string) $dateTimeFormat;
return $this;
}
}
2 changes: 2 additions & 0 deletions src/Formatter/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public function format($event)
) {
if ($key == "message") {
$value = htmlspecialchars($value, ENT_COMPAT, $enc);
} elseif ($key == "extra" && empty($value)) {
continue;
}
$elt->appendChild(new DOMElement($key, (string)$value));
}
Expand Down
3 changes: 1 addition & 2 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ public function addWriter($writer, $priority = 1, array $options = null)
is_object($writer) ? get_class($writer) : gettype($writer)
));
}

$this->writers->insert($writer, $priority);

return $this;
Expand Down Expand Up @@ -426,7 +425,7 @@ public static function registerExceptionHandler(Logger $logger)
throw new Exception\InvalidArgumentException('Invalid Logger specified');
}

set_exception_handler(function ($exception) use ($logger){
set_exception_handler(function ($exception) use ($logger) {
$extra = array(
'file' => $exception->getFile(),
'line' => $exception->getLine(),
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function setFormatter(Formatter $formatter)
}

/**
* Perform shutdown activites such as closing open resources
* Perform shutdown activities such as closing open resources
*
* @return void
*/
Expand Down
22 changes: 10 additions & 12 deletions src/Writer/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Zend\Db\Adapter\Adapter;
use Zend\Log\Exception;
use Zend\Log\Formatter;
use Zend\Log\Formatter\Db as DbFormatter;

/**
* @category Zend
Expand Down Expand Up @@ -62,7 +63,7 @@ class Db extends AbstractWriter
* @return Db
* @throw Exception\InvalidArgumentException
*/
public function __construct($db, $tableName, array $columnMap = null, $separator = null)
public function __construct($db, $tableName = null, array $columnMap = null, $separator = null)
{
if ($db instanceof Traversable) {
$db = iterator_to_array($db);
Expand All @@ -79,25 +80,20 @@ public function __construct($db, $tableName, array $columnMap = null, $separator
throw new Exception\InvalidArgumentException('You must pass a valid Zend\Db\Adapter\Adapter');
}

$tableName = (string) $tableName;
if ('' === $tableName){
throw new Exception\InvalidArgumentException('You must specify a table name. Either directly in the constructor, or via options');
}

$this->db = $db;
$this->tableName = $tableName;
$this->columnMap = $columnMap;

if (!empty($separator)) {
$this->separator = $separator;
}
}

/**
* Formatting is not possible on this writer
*
* @param Formatter\FormatterInterface $formatter
* @return void
* @throws Exception\InvalidArgumentException
*/
public function setFormatter(Formatter\FormatterInterface $formatter)
{
throw new Exception\InvalidArgumentException(get_class() . ' does not support formatting');
$this->setFormatter(new DbFormatter());
}

/**
Expand All @@ -123,6 +119,8 @@ protected function doWrite(array $event)
throw new Exception\RuntimeException('Database adapter is null');
}

$event = $this->formatter->format($event);

// Transform the event array into fields
if (null === $this->columnMap) {
$dataToInsert = $this->eventIntoColumn($event);
Expand Down
Loading

0 comments on commit 91ed9d0

Please sign in to comment.