-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from Nekland/feature/logger
Logger feature
- Loading branch information
Showing
6 changed files
with
188 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* This file is a part of Woketo package. | ||
* | ||
* (c) Nekland <[email protected]> | ||
* | ||
* For the full license, take a look to the LICENSE file | ||
* on the root directory of this project | ||
*/ | ||
|
||
namespace Nekland\Woketo\Utils; | ||
|
||
|
||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\LogLevel; | ||
|
||
class SimpleLogger extends AbstractLogger | ||
{ | ||
/** | ||
* Modify the log behavior | ||
* @var bool | ||
*/ | ||
private $debug; | ||
|
||
public function __construct($debug = false) | ||
{ | ||
$this->debug = $debug; | ||
} | ||
|
||
/** | ||
* Log | ||
* | ||
* @param mixed $level | ||
* @param string $message | ||
* @param array $context | ||
*/ | ||
public function log($level, $message, array $context = array()) | ||
{ | ||
// Doesn't log everything in not debug context | ||
if ($this->debug || \in_array($level, [LogLevel::CRITICAL, LogLevel::ERROR])) { | ||
echo '[' . date('Y-m-d H:i:s') . '][' . $level . '] ' . $message ."\n"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* This file is a part of Woketo package. | ||
* | ||
* (c) Nekland <[email protected]> | ||
* | ||
* For the full license, take a look to the LICENSE file | ||
* on the root directory of this project | ||
*/ | ||
|
||
namespace Test\Woketo\Utils; | ||
|
||
|
||
use Nekland\Woketo\Utils\SimpleLogger; | ||
|
||
class SimpleLoggerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testItEchoesLog() | ||
{ | ||
$logger = new SimpleLogger(); | ||
|
||
\ob_start(); | ||
$logger->info('Just some information.'); | ||
$logger->critical('God, something went wrong !'); | ||
|
||
$res = \ob_get_clean(); | ||
|
||
$this->assertContains('God, something went wrong !', $res); | ||
$this->assertNotContains('Just some information.', $res); | ||
} | ||
|
||
public function testItEchoesEverythingInDebugMode() | ||
{ | ||
$logger = new SimpleLogger(true); | ||
|
||
\ob_start(); | ||
$logger->critical('Oops!'); | ||
$logger->debug('Normal log'); | ||
$res = \ob_get_clean(); | ||
|
||
$this->assertContains('Oops!', $res); | ||
$this->assertContains('Normal log', $res); | ||
} | ||
} |