Skip to content

Commit

Permalink
Fix logger that wasn't able to log \Throwable
Browse files Browse the repository at this point in the history
  • Loading branch information
phpbg committed Feb 27, 2019
1 parent 49836a9 commit 91d7ad3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Logger/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ private function format(string $level, string $message, array $context): string

/**
* Format an exception
* @param \Exception $e
* @param \Throwable $e
* @param string $newLine Optionnal new line char
* @return string
*/
protected function formatException(\Exception $e, $newLine = PHP_EOL)
protected function formatException(\Throwable $e, $newLine = PHP_EOL): string
{
$message = '';

Expand All @@ -130,11 +130,11 @@ protected function formatException(\Exception $e, $newLine = PHP_EOL)
/**
* Format an exception as string
*
* @param \Exception $e
* @param \Throwable $e
* @param string $newLine
* @return string
*/
protected function _formatException(\Exception $e, string $newLine = PHP_EOL): string
protected function _formatException(\Throwable $e, string $newLine = PHP_EOL): string
{
return get_class($e) . ': ' . $e->getMessage() . $newLine . '#> ' . $e->getFile() . '(' . $e->getLine() . ')' . $newLine . $e->getTraceAsString();
}
Expand Down
65 changes: 65 additions & 0 deletions tests/Logger/ConsoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* MIT License
*
* Copyright (c) 2018 Samuel CHEMLA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace PhpBg\MiniHttpd\Tests\Logger;

use PhpBg\MiniHttpd\Logger\Console;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;

class ConsoleTest extends TestCase
{
public function testLogException() {
$output = fopen('php://memory', 'w+');
$logger = new Console(LogLevel::DEBUG, $output);

$logger->error("foo", ['exception' => new \Exception("bar")]);

rewind($output);
$this->assertNotEmpty(stream_get_contents($output));
}

public function testLogExceptionWithPrevious() {
$output = fopen('php://memory', 'w+');
$logger = new Console(LogLevel::DEBUG, $output);

$logger->error("foo", ['exception' => new \Exception("bar", 0, new \Exception("baz"))]);

rewind($output);
$this->assertNotEmpty(stream_get_contents($output));
}

public function testLogError() {
$output = fopen('php://memory', 'w+');
$logger = new Console(LogLevel::DEBUG, $output);

$logger->error("foo", ['exception' => new \Error("bar")]);

rewind($output);
$this->assertNotEmpty(stream_get_contents($output));
}

}

0 comments on commit 91d7ad3

Please sign in to comment.