summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJordi Boggiano <j.boggiano@seld.be>2016-05-26 17:39:41 +0100
committerJordi Boggiano <j.boggiano@seld.be>2016-05-26 18:04:51 +0100
commit76a91c672213a17cb0de82f20e7a28c6f014eacf (patch)
treea44608bb9044b38b41c2945bda0d8025749c0911
parent912d813c7345ffe97ddcf1469bd3223fdf27d538 (diff)
downloadmonolog-76a91c672213a17cb0de82f20e7a28c6f014eacf.zip
monolog-76a91c672213a17cb0de82f20e7a28c6f014eacf.tar.gz
monolog-76a91c672213a17cb0de82f20e7a28c6f014eacf.tar.bz2
Set default date format to have a timezone, fixes #196
-rw-r--r--phpunit.xml.dist2
-rw-r--r--src/Monolog/DateTimeImmutable.php10
-rw-r--r--src/Monolog/Formatter/NormalizerFormatter.php10
-rw-r--r--tests/Monolog/Formatter/ScalarFormatterTest.php6
-rw-r--r--tests/Monolog/Handler/DoctrineCouchDBHandlerTest.php2
-rw-r--r--tests/Monolog/Handler/LogEntriesHandlerTest.php4
-rw-r--r--tests/Monolog/Handler/MailHandlerTest.php2
-rw-r--r--tests/bootstrap.php5
8 files changed, 27 insertions, 14 deletions
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 20d82b6..54da281 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
-<phpunit bootstrap="vendor/autoload.php" colors="true">
+<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Monolog Test Suite">
<directory>tests/Monolog/</directory>
diff --git a/src/Monolog/DateTimeImmutable.php b/src/Monolog/DateTimeImmutable.php
index 6187c5a..f1536f4 100644
--- a/src/Monolog/DateTimeImmutable.php
+++ b/src/Monolog/DateTimeImmutable.php
@@ -34,10 +34,7 @@ class DateTimeImmutable extends \DateTimeImmutable implements \JsonSerializable
$this->useMicroseconds = $useMicroseconds;
}
- /**
- * @return string
- */
- public function jsonSerialize()
+ public function jsonSerialize(): string
{
if ($this->useMicroseconds) {
return $this->format('Y-m-d\TH:i:s.uP');
@@ -45,4 +42,9 @@ class DateTimeImmutable extends \DateTimeImmutable implements \JsonSerializable
return $this->format('Y-m-d\TH:i:sP');
}
+
+ public function __toString(): string
+ {
+ return $this->jsonSerialize();
+ }
}
diff --git a/src/Monolog/Formatter/NormalizerFormatter.php b/src/Monolog/Formatter/NormalizerFormatter.php
index 1b2e99c..d73ac65 100644
--- a/src/Monolog/Formatter/NormalizerFormatter.php
+++ b/src/Monolog/Formatter/NormalizerFormatter.php
@@ -12,6 +12,7 @@
namespace Monolog\Formatter;
use Throwable;
+use Monolog\DateTimeImmutable;
/**
* Normalizes incoming records to remove objects/resources so it's easier to dump to various targets
@@ -20,7 +21,7 @@ use Throwable;
*/
class NormalizerFormatter implements FormatterInterface
{
- const SIMPLE_DATE = "Y-m-d H:i:s";
+ const SIMPLE_DATE = "Y-m-d\TH:i:sP";
protected $dateFormat;
@@ -29,7 +30,7 @@ class NormalizerFormatter implements FormatterInterface
*/
public function __construct($dateFormat = null)
{
- $this->dateFormat = $dateFormat ?: static::SIMPLE_DATE;
+ $this->dateFormat = $dateFormat;
if (!function_exists('json_encode')) {
throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s NormalizerFormatter');
}
@@ -86,7 +87,10 @@ class NormalizerFormatter implements FormatterInterface
}
if ($data instanceof \DateTimeInterface) {
- return $data->format($this->dateFormat);
+ if ($data instanceof DateTimeImmutable) {
+ return (string) $data;
+ }
+ return $data->format($this->dateFormat ?: static::SIMPLE_DATE);
}
if (is_object($data)) {
diff --git a/tests/Monolog/Formatter/ScalarFormatterTest.php b/tests/Monolog/Formatter/ScalarFormatterTest.php
index eb237ae..96d92b5 100644
--- a/tests/Monolog/Formatter/ScalarFormatterTest.php
+++ b/tests/Monolog/Formatter/ScalarFormatterTest.php
@@ -11,6 +11,8 @@
namespace Monolog\Formatter;
+use Monolog\DateTimeImmutable;
+
class ScalarFormatterTest extends \PHPUnit_Framework_TestCase
{
private $formatter;
@@ -49,7 +51,7 @@ class ScalarFormatterTest extends \PHPUnit_Framework_TestCase
'baz' => false,
'bam' => array(1, 2, 3),
'bat' => array('foo' => 'bar'),
- 'bap' => \DateTimeImmutable::createFromFormat(\DateTime::ISO8601, '1970-01-01T00:00:00+0000'),
+ 'bap' => $dt = new DateTimeImmutable(true),
'ban' => $exception,
));
@@ -59,7 +61,7 @@ class ScalarFormatterTest extends \PHPUnit_Framework_TestCase
'baz' => false,
'bam' => $this->encodeJson(array(1, 2, 3)),
'bat' => $this->encodeJson(array('foo' => 'bar')),
- 'bap' => '1970-01-01 00:00:00',
+ 'bap' => (string) $dt,
'ban' => $this->encodeJson(array(
'class' => get_class($exception),
'message' => $exception->getMessage(),
diff --git a/tests/Monolog/Handler/DoctrineCouchDBHandlerTest.php b/tests/Monolog/Handler/DoctrineCouchDBHandlerTest.php
index 5d69a7c..3383381 100644
--- a/tests/Monolog/Handler/DoctrineCouchDBHandlerTest.php
+++ b/tests/Monolog/Handler/DoctrineCouchDBHandlerTest.php
@@ -38,7 +38,7 @@ class DoctrineCouchDBHandlerTest extends TestCase
'level' => Logger::WARNING,
'level_name' => 'WARNING',
'channel' => 'test',
- 'datetime' => $record['datetime']->format('Y-m-d H:i:s'),
+ 'datetime' => (string) $record['datetime'],
'extra' => array(),
);
diff --git a/tests/Monolog/Handler/LogEntriesHandlerTest.php b/tests/Monolog/Handler/LogEntriesHandlerTest.php
index f9c8fd4..9db4957 100644
--- a/tests/Monolog/Handler/LogEntriesHandlerTest.php
+++ b/tests/Monolog/Handler/LogEntriesHandlerTest.php
@@ -37,7 +37,7 @@ class LogEntriesHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
- $this->assertRegexp('/testToken \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] test.CRITICAL: Critical write test/', $content);
+ $this->assertRegexp('/testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] test.CRITICAL: Critical write test/', $content);
}
public function testWriteBatchContent()
@@ -53,7 +53,7 @@ class LogEntriesHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
- $this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] .* \[\] \[\]\n){3}/', $content);
+ $this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] .* \[\] \[\]\n){3}/', $content);
}
private function createHandler()
diff --git a/tests/Monolog/Handler/MailHandlerTest.php b/tests/Monolog/Handler/MailHandlerTest.php
index 8e73a78..03ee177 100644
--- a/tests/Monolog/Handler/MailHandlerTest.php
+++ b/tests/Monolog/Handler/MailHandlerTest.php
@@ -64,7 +64,7 @@ class MailHandlerTest extends TestCase
$record = $this->getRecord();
$records = array($record);
- $records[0]['formatted'] = '['.$record['datetime']->format('Y-m-d H:i:s').'] test.WARNING: test [] []'."\n";
+ $records[0]['formatted'] = '['.$record['datetime'].'] test.WARNING: test [] []'."\n";
$handler->expects($this->once())
->method('send')
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
new file mode 100644
index 0000000..0ee6932
--- /dev/null
+++ b/tests/bootstrap.php
@@ -0,0 +1,5 @@
+<?php
+
+date_default_timezone_set('UTC');
+
+require __DIR__.'/../vendor/autoload.php';