Skip to content

Commit

Permalink
Merge 79e9a3b into 5c52c92
Browse files Browse the repository at this point in the history
  • Loading branch information
keitakn committed Jul 4, 2018
2 parents 5c52c92 + 79e9a3b commit 65920a9
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 63 deletions.
11 changes: 11 additions & 0 deletions src/PhpJsonLogger/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Nekonomokochan\PhpJsonLogger;

/**
* Class InvalidArgumentException
*
* @package Nekonomokochan\PhpJsonLogger
*/
class InvalidArgumentException extends \InvalidArgumentException
{
}
97 changes: 68 additions & 29 deletions src/PhpJsonLogger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*
* @package Nekonomokochan\PhpJsonLogger
*/
class Logger
class Logger extends \Monolog\Logger
{
use ErrorsContextFormatter;

use MonologInstanceCreator;
use MonologCreator;

/**
* @var string
Expand All @@ -30,11 +30,6 @@ class Logger
*/
private $logLevel;

/**
* @var \Monolog\Logger
*/
private $monologInstance;

/**
* @var string
*/
Expand All @@ -61,16 +56,22 @@ public function __construct(LoggerBuilder $builder)
$this->logLevel = $builder->getLogLevel();
$this->maxFiles = $builder->getMaxFiles();

$this->monologInstance = $this->createMonologInstance($this->traceId, $builder);
$constructParams = $this->createConstructParams($this->traceId, $builder);

parent::__construct(
$constructParams['channel'],
$constructParams['handlers'],
$constructParams['processors']
);
}

/**
* @param $message
* @param $context
*/
public function debug($message, $context = [])
public function debug($message, array $context = [])
{
$this->monologInstance->addDebug($message, $context);
$this->addDebug($message, $context);
}

/**
Expand All @@ -79,7 +80,7 @@ public function debug($message, $context = [])
*/
public function info($message, array $context = [])
{
$this->monologInstance->addInfo($message, $context);
$this->addInfo($message, $context);
}

/**
Expand All @@ -88,7 +89,7 @@ public function info($message, array $context = [])
*/
public function notice($message, array $context = [])
{
$this->monologInstance->addNotice($message, $context);
$this->addNotice($message, $context);
}

/**
Expand All @@ -97,43 +98,67 @@ public function notice($message, array $context = [])
*/
public function warning($message, array $context = [])
{
$this->monologInstance->addWarning($message, $context);
$this->addWarning($message, $context);
}

/**
* @param \Throwable $e
* @param array $context
*/
public function error(\Throwable $e, array $context = [])
public function error($e, array $context = [])
{
$this->monologInstance->addError(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
if ($this->isErrorObject($e) === false) {
throw new \InvalidArgumentException(
$this->generateInvalidArgumentMessage(__METHOD__)
);
}

$this->addError(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
}

/**
* @param \Throwable $e
* @param array $context
*/
public function critical(\Throwable $e, array $context = [])
public function critical($e, array $context = [])
{
$this->monologInstance->addCritical(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
if ($this->isErrorObject($e) === false) {
throw new \InvalidArgumentException(
$this->generateInvalidArgumentMessage(__METHOD__)
);
}

$this->addCritical(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
}

/**
* @param \Throwable $e
* @param array $context
*/
public function alert(\Throwable $e, array $context = [])
public function alert($e, array $context = [])
{
$this->monologInstance->addAlert(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
if ($this->isErrorObject($e) === false) {
throw new \InvalidArgumentException(
$this->generateInvalidArgumentMessage(__METHOD__)
);
}

$this->addAlert(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
}

/**
* @param \Throwable $e
* @param array $context
*/
public function emergency(\Throwable $e, array $context = [])
public function emergency($e, array $context = [])
{
$this->monologInstance->addEmergency(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
if ($this->isErrorObject($e) === false) {
throw new \InvalidArgumentException(
$this->generateInvalidArgumentMessage(__METHOD__)
);
}

$this->addEmergency(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
}

/**
Expand All @@ -160,14 +185,6 @@ public function getLogLevel(): int
return $this->logLevel;
}

/**
* @return \Monolog\Logger
*/
public function getMonologInstance()
{
return $this->monologInstance;
}

/**
* @return string
*/
Expand All @@ -193,4 +210,26 @@ private function generateTraceIdIfNeeded()
$this->traceId = Uuid::uuid4()->toString();
}
}

/**
* @param $value
* @return bool
*/
private function isErrorObject($value): bool
{
if ($value instanceof \Exception || $value instanceof \Error) {
return true;
}

return false;
}

/**
* @param string $method
* @return string
*/
private function generateInvalidArgumentMessage(string $method)
{
return 'Please give the exception class to the ' . $method;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
use Monolog\Processor\WebProcessor;

/**
* Trait MonologInstanceCreator
* Trait MonologCreator
*
* @package Nekonomokochan\PhpJsonLogger
*/
trait MonologInstanceCreator
trait MonologCreator
{
/**
* @param string $traceId
* @param LoggerBuilder $loggerBuilder
* @return \Monolog\Logger
* @return array
*/
public function createMonologInstance(string $traceId, LoggerBuilder $loggerBuilder)
public function createConstructParams(string $traceId, LoggerBuilder $loggerBuilder): array
{
$formatter = new JsonFormatter();

Expand Down Expand Up @@ -66,10 +66,10 @@ public function createMonologInstance(string $traceId, LoggerBuilder $loggerBuil
array_push($processors, $webProcessor);
}

return new \Monolog\Logger(
$loggerBuilder->getChannel(),
$handlers,
$processors
);
return [
'channel' => $loggerBuilder->getChannel(),
'handlers' => $handlers,
'processors' => $processors
];
}
}
27 changes: 24 additions & 3 deletions tests/Logger/AlertTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Nekonomokochan\Tests\Logger;

use Nekonomokochan\PhpJsonLogger\InvalidArgumentException;
use Nekonomokochan\PhpJsonLogger\LoggerBuilder;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -65,7 +66,7 @@ public function outputAlertLog()
'channel' => 'PhpJsonLogger',
'trace_id' => $logger->getTraceId(),
'file' => __FILE__,
'line' => 53,
'line' => 54,
'context' => $context,
'remote_ip_address' => '127.0.0.1',
'server_ip_address' => '127.0.0.1',
Expand All @@ -77,12 +78,32 @@ public function outputAlertLog()
'message' => 'TestCritical',
'code' => 500,
'file' => __FILE__,
'line' => 44,
'line' => 45,
'trace' => $resultArray['errors']['trace'],
],
];

$this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName());
$this->assertSame('PhpJsonLogger', $logger->getChannel());
$this->assertSame($expectedLog, $resultArray);
}

/**
* @test
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Please give the exception class to the Nekonomokochan\PhpJsonLogger\Logger::alert
*/
public function invalidArgumentException()
{
$message = '';

$context = [
'name' => 'keitakn',
'email' => 'dummy@email.com',
];

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setFileName($this->outputFileBaseName);
$logger = $loggerBuilder->build();
$logger->alert($message, $context);
}
}
27 changes: 24 additions & 3 deletions tests/Logger/CriticalTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Nekonomokochan\Tests\Logger;

use Nekonomokochan\PhpJsonLogger\InvalidArgumentException;
use Nekonomokochan\PhpJsonLogger\LoggerBuilder;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -65,7 +66,7 @@ public function outputCriticalLog()
'channel' => 'PhpJsonLogger',
'trace_id' => $logger->getTraceId(),
'file' => __FILE__,
'line' => 53,
'line' => 54,
'context' => $context,
'remote_ip_address' => '127.0.0.1',
'server_ip_address' => '127.0.0.1',
Expand All @@ -77,12 +78,32 @@ public function outputCriticalLog()
'message' => 'TestCritical',
'code' => 500,
'file' => __FILE__,
'line' => 44,
'line' => 45,
'trace' => $resultArray['errors']['trace'],
],
];

$this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName());
$this->assertSame('PhpJsonLogger', $logger->getChannel());
$this->assertSame($expectedLog, $resultArray);
}

/**
* @test
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Please give the exception class to the Nekonomokochan\PhpJsonLogger\Logger::critical
*/
public function invalidArgumentException()
{
$message = '';

$context = [
'name' => 'keitakn',
'email' => 'dummy@email.com',
];

$loggerBuilder = new LoggerBuilder();
$loggerBuilder->setFileName($this->outputFileBaseName);
$logger = $loggerBuilder->build();
$logger->critical($message, $context);
}
}
2 changes: 1 addition & 1 deletion tests/Logger/DebugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function outputDebugLog()
'process_time' => $resultArray['process_time'],
];

$this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName());
$this->assertSame('PhpJsonLogger', $logger->getChannel());
$this->assertSame($expectedLog, $resultArray);
}
}

0 comments on commit 65920a9

Please sign in to comment.