From ec60ac0100c58e855e226c8344f7ef14cedfe68e Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 10:33:08 +0900 Subject: [PATCH 01/15] resolve #20 From 2f67900948937fb0fb43db59a469c0db60bcbfe4 Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 10:36:58 +0900 Subject: [PATCH 02/15] modify #20 move to LoggerTest because For easy division --- tests/{ => Logger}/LoggerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{ => Logger}/LoggerTest.php (99%) diff --git a/tests/LoggerTest.php b/tests/Logger/LoggerTest.php similarity index 99% rename from tests/LoggerTest.php rename to tests/Logger/LoggerTest.php index ee7303c..95a1474 100644 --- a/tests/LoggerTest.php +++ b/tests/Logger/LoggerTest.php @@ -1,5 +1,5 @@ Date: Thu, 7 Jun 2018 11:01:50 +0900 Subject: [PATCH 03/15] modify #20 separated the test of the "info" method into another class --- tests/Logger/InfoTest.php | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/Logger/InfoTest.php diff --git a/tests/Logger/InfoTest.php b/tests/Logger/InfoTest.php new file mode 100644 index 0000000..4fe2f23 --- /dev/null +++ b/tests/Logger/InfoTest.php @@ -0,0 +1,68 @@ + 'Test', + 'price' => 4000, + 'list' => [1, 2, 3], + 'user' => [ + 'id' => 100, + 'name' => 'keitakn', + ], + ]; + + $loggerBuilder = new LoggerBuilder(); + $logger = $loggerBuilder->build(); + $logger->info('🐱', $context); + + $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultArray = json_decode($resultJson, true); + + echo "\n ---- Output Log Begin ---- \n"; + echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo "\n ---- Output Log End ---- \n"; + + $expectedLog = [ + 'log_level' => 'INFO', + 'message' => '🐱', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 45, + 'context' => $context, + 'remote_ip_address' => '127.0.0.1', + 'user_agent' => 'unknown', + 'datetime' => $resultArray['datetime'], + 'timezone' => date_default_timezone_get(), + 'process_time' => $resultArray['process_time'], + ]; + + $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); + $this->assertSame($expectedLog, $resultArray); + } +} From 2dbbd4ef2e7b6dffb1a457ce4276f1a54d7b6d6d Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 11:07:58 +0900 Subject: [PATCH 04/15] fixed #20 mistake fixed --- tests/Logger/InfoTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Logger/InfoTest.php b/tests/Logger/InfoTest.php index 4fe2f23..eef219e 100644 --- a/tests/Logger/InfoTest.php +++ b/tests/Logger/InfoTest.php @@ -53,7 +53,7 @@ public function outputInfoLog() 'message' => '🐱', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 45, + 'line' => 42, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', From 6271adcf73557024d084edb282fdda0a6d7d8f77 Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 11:08:20 +0900 Subject: [PATCH 05/15] modify #20 separated the test of the "debug" method into another class --- tests/Logger/DebugTest.php | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/Logger/DebugTest.php diff --git a/tests/Logger/DebugTest.php b/tests/Logger/DebugTest.php new file mode 100644 index 0000000..1a8c012 --- /dev/null +++ b/tests/Logger/DebugTest.php @@ -0,0 +1,63 @@ + 'Test', + ]; + + $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); + $logger = $loggerBuilder->build(); + $logger->debug('🐶', $context); + + $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultArray = json_decode($resultJson, true); + + echo "\n ---- Output Log Begin ---- \n"; + echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo "\n ---- Output Log End ---- \n"; + + $expectedLog = [ + 'log_level' => 'DEBUG', + 'message' => '🐶', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 37, + 'context' => $context, + 'remote_ip_address' => '127.0.0.1', + 'user_agent' => 'unknown', + 'datetime' => $resultArray['datetime'], + 'timezone' => date_default_timezone_get(), + 'process_time' => $resultArray['process_time'], + ]; + + $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); + $this->assertSame($expectedLog, $resultArray); + } +} From 52f11f7d277886279cd7af527c25cd779814ee9a Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 11:44:44 +0900 Subject: [PATCH 06/15] modify #20 separated the test of the "notice" method into another class --- tests/Logger/NoticeTest.php | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/Logger/NoticeTest.php diff --git a/tests/Logger/NoticeTest.php b/tests/Logger/NoticeTest.php new file mode 100644 index 0000000..598e9d2 --- /dev/null +++ b/tests/Logger/NoticeTest.php @@ -0,0 +1,64 @@ + 'Test', + ]; + + $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); + $logger = $loggerBuilder->build(); + $logger->notice('🐶', $context); + + $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultArray = json_decode($resultJson, true); + + echo "\n ---- Output Log Begin ---- \n"; + echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo "\n ---- Output Log End ---- \n"; + + $expectedLog = [ + 'log_level' => 'NOTICE', + 'message' => '🐶', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 38, + 'context' => $context, + 'remote_ip_address' => '127.0.0.1', + 'user_agent' => 'unknown', + 'datetime' => $resultArray['datetime'], + 'timezone' => date_default_timezone_get(), + 'process_time' => $resultArray['process_time'], + ]; + + $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); + $this->assertSame($expectedLog, $resultArray); + } +} From bfc3b926f64fe9767ec9c1dca8c0d64be3173605 Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 11:50:43 +0900 Subject: [PATCH 07/15] modify #20 separated the test of the "warning" method into another class --- tests/Logger/WarningTest.php | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/Logger/WarningTest.php diff --git a/tests/Logger/WarningTest.php b/tests/Logger/WarningTest.php new file mode 100644 index 0000000..b11dc24 --- /dev/null +++ b/tests/Logger/WarningTest.php @@ -0,0 +1,64 @@ + 'Test', + ]; + + $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); + $logger = $loggerBuilder->build(); + $logger->warning('🐶', $context); + + $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultArray = json_decode($resultJson, true); + + echo "\n ---- Output Log Begin ---- \n"; + echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo "\n ---- Output Log End ---- \n"; + + $expectedLog = [ + 'log_level' => 'WARNING', + 'message' => '🐶', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 38, + 'context' => $context, + 'remote_ip_address' => '127.0.0.1', + 'user_agent' => 'unknown', + 'datetime' => $resultArray['datetime'], + 'timezone' => date_default_timezone_get(), + 'process_time' => $resultArray['process_time'], + ]; + + $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); + $this->assertSame($expectedLog, $resultArray); + } +} From 39c1645243344e62f62b20dd3f0d94c0d344fa93 Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 11:55:46 +0900 Subject: [PATCH 08/15] add #20 separated the test of the "critical" method into another class --- tests/Logger/CriticalTest.php | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/Logger/CriticalTest.php diff --git a/tests/Logger/CriticalTest.php b/tests/Logger/CriticalTest.php new file mode 100644 index 0000000..81be4d0 --- /dev/null +++ b/tests/Logger/CriticalTest.php @@ -0,0 +1,72 @@ + 'keitakn', + 'email' => 'dummy@email.com', + ]; + + $loggerBuilder = new LoggerBuilder(); + $logger = $loggerBuilder->build(); + $logger->critical($exception, $context); + + $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultArray = json_decode($resultJson, true); + + echo "\n ---- Output Log Begin ---- \n"; + echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo "\n ---- Output Log End ---- \n"; + + $expectedLog = [ + 'log_level' => 'CRITICAL', + 'message' => 'ErrorException', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 39, + 'context' => $context, + 'remote_ip_address' => '127.0.0.1', + 'user_agent' => 'unknown', + 'datetime' => $resultArray['datetime'], + 'timezone' => date_default_timezone_get(), + 'process_time' => $resultArray['process_time'], + 'errors' => [ + 'message' => 'TestCritical', + 'code' => 500, + 'file' => __FILE__, + 'line' => 31, + 'trace' => $resultArray['errors']['trace'], + ], + ]; + + $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); + $this->assertSame($expectedLog, $resultArray); + } +} From c701222590ee9d233a8cebd9b7a5690fe5db21e2 Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 12:02:02 +0900 Subject: [PATCH 09/15] modify #20 separated the test of the "alert" method into another class --- tests/Logger/AlertTest.php | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/Logger/AlertTest.php diff --git a/tests/Logger/AlertTest.php b/tests/Logger/AlertTest.php new file mode 100644 index 0000000..1970600 --- /dev/null +++ b/tests/Logger/AlertTest.php @@ -0,0 +1,72 @@ + 'keitakn', + 'email' => 'dummy@email.com', + ]; + + $loggerBuilder = new LoggerBuilder(); + $logger = $loggerBuilder->build(); + $logger->alert($exception, $context); + + $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultArray = json_decode($resultJson, true); + + echo "\n ---- Output Log Begin ---- \n"; + echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo "\n ---- Output Log End ---- \n"; + + $expectedLog = [ + 'log_level' => 'ALERT', + 'message' => 'ErrorException', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 39, + 'context' => $context, + 'remote_ip_address' => '127.0.0.1', + 'user_agent' => 'unknown', + 'datetime' => $resultArray['datetime'], + 'timezone' => date_default_timezone_get(), + 'process_time' => $resultArray['process_time'], + 'errors' => [ + 'message' => 'TestCritical', + 'code' => 500, + 'file' => __FILE__, + 'line' => 31, + 'trace' => $resultArray['errors']['trace'], + ], + ]; + + $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); + $this->assertSame($expectedLog, $resultArray); + } +} From 1e4d0002d43c36fccfcd3cfb2ba43811db1f247e Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 12:05:45 +0900 Subject: [PATCH 10/15] modify #20 separated the test of the "emergency" method into another class --- tests/Logger/EmergencyTest.php | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/Logger/EmergencyTest.php diff --git a/tests/Logger/EmergencyTest.php b/tests/Logger/EmergencyTest.php new file mode 100644 index 0000000..bc6c5ef --- /dev/null +++ b/tests/Logger/EmergencyTest.php @@ -0,0 +1,72 @@ + 'keitakn', + 'email' => 'dummy@email.com', + ]; + + $loggerBuilder = new LoggerBuilder(); + $logger = $loggerBuilder->build(); + $logger->emergency($exception, $context); + + $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultArray = json_decode($resultJson, true); + + echo "\n ---- Output Log Begin ---- \n"; + echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo "\n ---- Output Log End ---- \n"; + + $expectedLog = [ + 'log_level' => 'EMERGENCY', + 'message' => 'ErrorException', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 39, + 'context' => $context, + 'remote_ip_address' => '127.0.0.1', + 'user_agent' => 'unknown', + 'datetime' => $resultArray['datetime'], + 'timezone' => date_default_timezone_get(), + 'process_time' => $resultArray['process_time'], + 'errors' => [ + 'message' => 'TestCritical', + 'code' => 500, + 'file' => __FILE__, + 'line' => 31, + 'trace' => $resultArray['errors']['trace'], + ], + ]; + + $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); + $this->assertSame($expectedLog, $resultArray); + } +} From dadaeef49e5fd9eceae27502a6a22ffaf3256c98 Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 12:10:01 +0900 Subject: [PATCH 11/15] modify #20 separated the test of the "error" method into another class --- tests/Logger/ErrorTest.php | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/Logger/ErrorTest.php diff --git a/tests/Logger/ErrorTest.php b/tests/Logger/ErrorTest.php new file mode 100644 index 0000000..6e83213 --- /dev/null +++ b/tests/Logger/ErrorTest.php @@ -0,0 +1,71 @@ + 'keitakn', + 'email' => 'dummy@email.com', + ]; + + $loggerBuilder = new LoggerBuilder(); + $logger = $loggerBuilder->build(); + $logger->error($exception, $context); + + $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultArray = json_decode($resultJson, true); + + echo "\n ---- Output Log Begin ---- \n"; + echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo "\n ---- Output Log End ---- \n"; + + $expectedLog = [ + 'log_level' => 'ERROR', + 'message' => 'Exception', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 38, + 'context' => $context, + 'remote_ip_address' => '127.0.0.1', + 'user_agent' => 'unknown', + 'datetime' => $resultArray['datetime'], + 'timezone' => date_default_timezone_get(), + 'process_time' => $resultArray['process_time'], + 'errors' => [ + 'message' => 'TestException', + 'code' => 500, + 'file' => __FILE__, + 'line' => 30, + 'trace' => $resultArray['errors']['trace'], + ], + ]; + + $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); + $this->assertSame($expectedLog, $resultArray); + } +} From f7692c4519ad17c0e676f0211ad843709eb71118 Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 12:23:32 +0900 Subject: [PATCH 12/15] remove #20 Delete the method moved to another class. --- tests/Logger/LoggerTest.php | 362 +----------------------------------- 1 file changed, 4 insertions(+), 358 deletions(-) diff --git a/tests/Logger/LoggerTest.php b/tests/Logger/LoggerTest.php index 95a1474..3b39f9c 100644 --- a/tests/Logger/LoggerTest.php +++ b/tests/Logger/LoggerTest.php @@ -22,97 +22,6 @@ public function setUp() } } - /** - * @test - */ - public function outputInfoLog() - { - $context = [ - 'title' => 'Test', - 'price' => 4000, - 'list' => [1, 2, 3], - 'user' => [ - 'id' => 100, - 'name' => 'keitakn', - ], - ]; - - $loggerBuilder = new LoggerBuilder(); - $logger = $loggerBuilder->build(); - $logger->info('🐱', $context); - - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); - $resultArray = json_decode($resultJson, true); - - echo "\n ---- Output Log Begin ---- \n"; - echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); - echo "\n ---- Output Log End ---- \n"; - - $expectedLog = [ - 'log_level' => 'INFO', - 'message' => '🐱', - 'trace_id' => $logger->getTraceId(), - 'file' => __FILE__, - 'line' => 42, - 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', - 'datetime' => $resultArray['datetime'], - 'timezone' => date_default_timezone_get(), - 'process_time' => $resultArray['process_time'], - ]; - - $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); - $this->assertSame($expectedLog, $resultArray); - } - - /** - * @test - */ - public function outputErrorLog() - { - $exception = new \Exception('TestException', 500); - $context = [ - 'name' => 'keitakn', - 'email' => 'dummy@email.com', - ]; - - $loggerBuilder = new LoggerBuilder(); - $logger = $loggerBuilder->build(); - $logger->error($exception, $context); - - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); - $resultArray = json_decode($resultJson, true); - - echo "\n ---- Output Log Begin ---- \n"; - echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); - echo "\n ---- Output Log End ---- \n"; - - $expectedLog = [ - 'log_level' => 'ERROR', - 'message' => 'Exception', - 'trace_id' => $logger->getTraceId(), - 'file' => __FILE__, - 'line' => 82, - 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', - 'datetime' => $resultArray['datetime'], - 'timezone' => date_default_timezone_get(), - 'process_time' => $resultArray['process_time'], - 'errors' => [ - 'message' => 'TestException', - 'code' => 500, - 'file' => __FILE__, - 'line' => 74, - 'trace' => $resultArray['errors']['trace'], - ], - ]; - - $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); - $this->assertSame($expectedLog, $resultArray); - } - /** * @test */ @@ -143,7 +52,7 @@ public function outputUserAgent() 'message' => 'testOutputUserAgent', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 130, + 'line' => 39, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => $userAgent, @@ -186,7 +95,7 @@ public function outputRemoteIpAddress() 'message' => 'testOutputRemoteIpAddress', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 173, + 'line' => 82, 'context' => $context, 'remote_ip_address' => $remoteIpAddress, 'user_agent' => 'unknown', @@ -225,7 +134,7 @@ public function setTraceIdIsOutput() 'message' => 'testSetTraceIdIsOutput', 'trace_id' => 'MyTraceID', 'file' => __FILE__, - 'line' => 214, + 'line' => 123, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', @@ -274,7 +183,7 @@ public function setLogFileName() 'message' => 'testSetLogFileName', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 263, + 'line' => 172, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', @@ -314,267 +223,4 @@ public function setLogLevel() $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); $this->assertSame(500, $logger->getLogLevel()); } - - /** - * @test - */ - public function outputDebugLog() - { - $context = [ - 'title' => 'Test', - ]; - - $loggerBuilder = new LoggerBuilder(); - $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); - $logger = $loggerBuilder->build(); - $logger->debug('🐶', $context); - - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); - $resultArray = json_decode($resultJson, true); - - echo "\n ---- Output Log Begin ---- \n"; - echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); - echo "\n ---- Output Log End ---- \n"; - - $expectedLog = [ - 'log_level' => 'DEBUG', - 'message' => '🐶', - 'trace_id' => $logger->getTraceId(), - 'file' => __FILE__, - 'line' => 330, - 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', - 'datetime' => $resultArray['datetime'], - 'timezone' => date_default_timezone_get(), - 'process_time' => $resultArray['process_time'], - ]; - - $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); - $this->assertSame($expectedLog, $resultArray); - } - - /** - * @test - * @throws \Exception - */ - public function outputNoticeLog() - { - $context = [ - 'title' => 'Test', - ]; - - $loggerBuilder = new LoggerBuilder(); - $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); - $logger = $loggerBuilder->build(); - $logger->notice('🐶', $context); - - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); - $resultArray = json_decode($resultJson, true); - - echo "\n ---- Output Log Begin ---- \n"; - echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); - echo "\n ---- Output Log End ---- \n"; - - $expectedLog = [ - 'log_level' => 'NOTICE', - 'message' => '🐶', - 'trace_id' => $logger->getTraceId(), - 'file' => __FILE__, - 'line' => 370, - 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', - 'datetime' => $resultArray['datetime'], - 'timezone' => date_default_timezone_get(), - 'process_time' => $resultArray['process_time'], - ]; - - $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); - $this->assertSame($expectedLog, $resultArray); - } - - /** - * @test - * @throws \Exception - */ - public function outputWarningLog() - { - $context = [ - 'title' => 'Test', - ]; - - $loggerBuilder = new LoggerBuilder(); - $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); - $logger = $loggerBuilder->build(); - $logger->warning('🐶', $context); - - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); - $resultArray = json_decode($resultJson, true); - - echo "\n ---- Output Log Begin ---- \n"; - echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); - echo "\n ---- Output Log End ---- \n"; - - $expectedLog = [ - 'log_level' => 'WARNING', - 'message' => '🐶', - 'trace_id' => $logger->getTraceId(), - 'file' => __FILE__, - 'line' => 410, - 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', - 'datetime' => $resultArray['datetime'], - 'timezone' => date_default_timezone_get(), - 'process_time' => $resultArray['process_time'], - ]; - - $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); - $this->assertSame($expectedLog, $resultArray); - } - - /** - * @test - * @throws \Exception - */ - public function outputCriticalLog() - { - $exception = new \ErrorException('TestCritical', 500); - $context = [ - 'name' => 'keitakn', - 'email' => 'dummy@email.com', - ]; - - $loggerBuilder = new LoggerBuilder(); - $logger = $loggerBuilder->build(); - $logger->critical($exception, $context); - - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); - $resultArray = json_decode($resultJson, true); - - echo "\n ---- Output Log Begin ---- \n"; - echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); - echo "\n ---- Output Log End ---- \n"; - - $expectedLog = [ - 'log_level' => 'CRITICAL', - 'message' => 'ErrorException', - 'trace_id' => $logger->getTraceId(), - 'file' => __FILE__, - 'line' => 451, - 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', - 'datetime' => $resultArray['datetime'], - 'timezone' => date_default_timezone_get(), - 'process_time' => $resultArray['process_time'], - 'errors' => [ - 'message' => 'TestCritical', - 'code' => 500, - 'file' => __FILE__, - 'line' => 443, - 'trace' => $resultArray['errors']['trace'], - ], - ]; - - $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); - $this->assertSame($expectedLog, $resultArray); - } - - /** - * @test - * @throws \Exception - */ - public function outputAlertLog() - { - $exception = new \ErrorException('TestCritical', 500); - $context = [ - 'name' => 'keitakn', - 'email' => 'dummy@email.com', - ]; - - $loggerBuilder = new LoggerBuilder(); - $logger = $loggerBuilder->build(); - $logger->alert($exception, $context); - - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); - $resultArray = json_decode($resultJson, true); - - echo "\n ---- Output Log Begin ---- \n"; - echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); - echo "\n ---- Output Log End ---- \n"; - - $expectedLog = [ - 'log_level' => 'ALERT', - 'message' => 'ErrorException', - 'trace_id' => $logger->getTraceId(), - 'file' => __FILE__, - 'line' => 499, - 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', - 'datetime' => $resultArray['datetime'], - 'timezone' => date_default_timezone_get(), - 'process_time' => $resultArray['process_time'], - 'errors' => [ - 'message' => 'TestCritical', - 'code' => 500, - 'file' => __FILE__, - 'line' => 491, - 'trace' => $resultArray['errors']['trace'], - ], - ]; - - $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); - $this->assertSame($expectedLog, $resultArray); - } - - /** - * @test - * @throws \Exception - */ - public function outputEmergencyLog() - { - $exception = new \ErrorException('TestCritical', 500); - $context = [ - 'name' => 'keitakn', - 'email' => 'dummy@email.com', - ]; - - $loggerBuilder = new LoggerBuilder(); - $logger = $loggerBuilder->build(); - $logger->emergency($exception, $context); - - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); - $resultArray = json_decode($resultJson, true); - - echo "\n ---- Output Log Begin ---- \n"; - echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); - echo "\n ---- Output Log End ---- \n"; - - $expectedLog = [ - 'log_level' => 'EMERGENCY', - 'message' => 'ErrorException', - 'trace_id' => $logger->getTraceId(), - 'file' => __FILE__, - 'line' => 547, - 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', - 'datetime' => $resultArray['datetime'], - 'timezone' => date_default_timezone_get(), - 'process_time' => $resultArray['process_time'], - 'errors' => [ - 'message' => 'TestCritical', - 'code' => 500, - 'file' => __FILE__, - 'line' => 539, - 'trace' => $resultArray['errors']['trace'], - ], - ]; - - $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); - $this->assertSame($expectedLog, $resultArray); - } } From 631d59044710f2739296384806997c57b000177c Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 12:53:58 +0900 Subject: [PATCH 13/15] modify #20 refactored the test code. - Variable file name - Changed to set log file name to output according to log level --- tests/Logger/AlertTest.php | 30 ++++++++++++++++++++++-------- tests/Logger/CriticalTest.php | 30 ++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/tests/Logger/AlertTest.php b/tests/Logger/AlertTest.php index 1970600..b8c3f04 100644 --- a/tests/Logger/AlertTest.php +++ b/tests/Logger/AlertTest.php @@ -12,19 +12,32 @@ */ class AlertTest extends TestCase { + /** + * @var string + */ + private $outputFileBaseName; + + /** + * @var string + */ + private $outputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + $this->outputFileBaseName = '/tmp/alert-log-test.log'; + $this->outputFileName = '/tmp/alert-log-test-' . date('Y-m-d') . '.log'; + + if (file_exists($this->outputFileName)) { + unlink($this->outputFileName); } } /** * @test - * @throws \Exception */ public function outputAlertLog() { @@ -35,10 +48,11 @@ public function outputAlertLog() ]; $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setFileName($this->outputFileBaseName); $logger = $loggerBuilder->build(); $logger->alert($exception, $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -50,7 +64,7 @@ public function outputAlertLog() 'message' => 'ErrorException', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 39, + 'line' => 53, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', @@ -61,7 +75,7 @@ public function outputAlertLog() 'message' => 'TestCritical', 'code' => 500, 'file' => __FILE__, - 'line' => 31, + 'line' => 44, 'trace' => $resultArray['errors']['trace'], ], ]; diff --git a/tests/Logger/CriticalTest.php b/tests/Logger/CriticalTest.php index 81be4d0..664ba88 100644 --- a/tests/Logger/CriticalTest.php +++ b/tests/Logger/CriticalTest.php @@ -12,19 +12,32 @@ */ class CriticalTest extends TestCase { + /** + * @var string + */ + private $outputFileBaseName; + + /** + * @var string + */ + private $outputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + $this->outputFileBaseName = '/tmp/critical-log-test.log'; + $this->outputFileName = '/tmp/critical-log-test-' . date('Y-m-d') . '.log'; + + if (file_exists($this->outputFileName)) { + unlink($this->outputFileName); } } /** * @test - * @throws \Exception */ public function outputCriticalLog() { @@ -35,10 +48,11 @@ public function outputCriticalLog() ]; $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setFileName($this->outputFileBaseName); $logger = $loggerBuilder->build(); $logger->critical($exception, $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -50,7 +64,7 @@ public function outputCriticalLog() 'message' => 'ErrorException', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 39, + 'line' => 53, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', @@ -61,7 +75,7 @@ public function outputCriticalLog() 'message' => 'TestCritical', 'code' => 500, 'file' => __FILE__, - 'line' => 31, + 'line' => 44, 'trace' => $resultArray['errors']['trace'], ], ]; From 88c5d40338b7d83d06ea170a5d9cd37831d14afc Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 14:55:07 +0900 Subject: [PATCH 14/15] modify #20 refactored the test code. - Variable file name - Changed to set log file name to output according to log level --- tests/Logger/DebugTest.php | 27 +++++++++++++++++++++------ tests/Logger/EmergencyTest.php | 33 ++++++++++++++++++++++++--------- tests/Logger/ErrorTest.php | 29 ++++++++++++++++++++++------- tests/Logger/InfoTest.php | 27 +++++++++++++++++++++------ tests/Logger/NoticeTest.php | 27 +++++++++++++++++++++------ tests/Logger/WarningTest.php | 27 +++++++++++++++++++++------ 6 files changed, 130 insertions(+), 40 deletions(-) diff --git a/tests/Logger/DebugTest.php b/tests/Logger/DebugTest.php index 1a8c012..73b2ecf 100644 --- a/tests/Logger/DebugTest.php +++ b/tests/Logger/DebugTest.php @@ -12,13 +12,27 @@ */ class DebugTest extends TestCase { + /** + * @var string + */ + private $outputFileBaseName; + + /** + * @var string + */ + private $outputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + $this->outputFileBaseName = '/tmp/debug-log-test.log'; + $this->outputFileName = '/tmp/debug-log-test-' . date('Y-m-d') . '.log'; + + if (file_exists($this->outputFileName)) { + unlink($this->outputFileName); } } @@ -32,11 +46,12 @@ public function outputDebugLog() ]; $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setFileName($this->outputFileBaseName); $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); $logger = $loggerBuilder->build(); $logger->debug('🐶', $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -48,7 +63,7 @@ public function outputDebugLog() 'message' => '🐶', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 37, + 'line' => 52, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', diff --git a/tests/Logger/EmergencyTest.php b/tests/Logger/EmergencyTest.php index bc6c5ef..f63ce7b 100644 --- a/tests/Logger/EmergencyTest.php +++ b/tests/Logger/EmergencyTest.php @@ -12,13 +12,27 @@ */ class EmergencyTest extends TestCase { + /** + * @var string + */ + private $outputFileBaseName; + + /** + * @var string + */ + private $outputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + $this->outputFileBaseName = '/tmp/emergency-log-test.log'; + $this->outputFileName = '/tmp/emergency-log-test-' . date('Y-m-d') . '.log'; + + if (file_exists($this->outputFileName)) { + unlink($this->outputFileName); } } @@ -28,17 +42,18 @@ public function setUp() */ public function outputEmergencyLog() { - $exception = new \ErrorException('TestCritical', 500); + $exception = new \ErrorException('TestEmergency', 500); $context = [ 'name' => 'keitakn', 'email' => 'dummy@email.com', ]; $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setFileName($this->outputFileBaseName); $logger = $loggerBuilder->build(); $logger->emergency($exception, $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -50,7 +65,7 @@ public function outputEmergencyLog() 'message' => 'ErrorException', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 39, + 'line' => 54, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', @@ -58,10 +73,10 @@ public function outputEmergencyLog() 'timezone' => date_default_timezone_get(), 'process_time' => $resultArray['process_time'], 'errors' => [ - 'message' => 'TestCritical', + 'message' => 'TestEmergency', 'code' => 500, 'file' => __FILE__, - 'line' => 31, + 'line' => 45, 'trace' => $resultArray['errors']['trace'], ], ]; diff --git a/tests/Logger/ErrorTest.php b/tests/Logger/ErrorTest.php index 6e83213..5954a0d 100644 --- a/tests/Logger/ErrorTest.php +++ b/tests/Logger/ErrorTest.php @@ -12,13 +12,27 @@ */ class ErrorTest extends TestCase { + /** + * @var string + */ + private $outputFileBaseName; + + /** + * @var string + */ + private $outputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + $this->outputFileBaseName = '/tmp/error-log-test.log'; + $this->outputFileName = '/tmp/error-log-test-' . date('Y-m-d') . '.log'; + + if (file_exists($this->outputFileName)) { + unlink($this->outputFileName); } } @@ -34,10 +48,11 @@ public function outputErrorLog() ]; $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setFileName($this->outputFileBaseName); $logger = $loggerBuilder->build(); $logger->error($exception, $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -49,7 +64,7 @@ public function outputErrorLog() 'message' => 'Exception', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 38, + 'line' => 53, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', @@ -60,7 +75,7 @@ public function outputErrorLog() 'message' => 'TestException', 'code' => 500, 'file' => __FILE__, - 'line' => 30, + 'line' => 44, 'trace' => $resultArray['errors']['trace'], ], ]; diff --git a/tests/Logger/InfoTest.php b/tests/Logger/InfoTest.php index eef219e..a39d59f 100644 --- a/tests/Logger/InfoTest.php +++ b/tests/Logger/InfoTest.php @@ -12,13 +12,27 @@ */ class InfoTest extends TestCase { + /** + * @var string + */ + private $outputFileBaseName; + + /** + * @var string + */ + private $outputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + $this->outputFileBaseName = '/tmp/info-log-test.log'; + $this->outputFileName = '/tmp/info-log-test-' . date('Y-m-d') . '.log'; + + if (file_exists($this->outputFileName)) { + unlink($this->outputFileName); } } @@ -38,10 +52,11 @@ public function outputInfoLog() ]; $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setFileName($this->outputFileBaseName); $logger = $loggerBuilder->build(); $logger->info('🐱', $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -53,7 +68,7 @@ public function outputInfoLog() 'message' => '🐱', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 42, + 'line' => 57, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', diff --git a/tests/Logger/NoticeTest.php b/tests/Logger/NoticeTest.php index 598e9d2..660949f 100644 --- a/tests/Logger/NoticeTest.php +++ b/tests/Logger/NoticeTest.php @@ -12,13 +12,27 @@ */ class NoticeTest extends TestCase { + /** + * @var string + */ + private $outputFileBaseName; + + /** + * @var string + */ + private $outputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + $this->outputFileBaseName = '/tmp/notice-log-test.log'; + $this->outputFileName = '/tmp/notice-log-test-' . date('Y-m-d') . '.log'; + + if (file_exists($this->outputFileName)) { + unlink($this->outputFileName); } } @@ -33,11 +47,12 @@ public function outputNoticeLog() ]; $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setFileName($this->outputFileBaseName); $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); $logger = $loggerBuilder->build(); $logger->notice('🐶', $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -49,7 +64,7 @@ public function outputNoticeLog() 'message' => '🐶', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 38, + 'line' => 53, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', diff --git a/tests/Logger/WarningTest.php b/tests/Logger/WarningTest.php index b11dc24..cadd042 100644 --- a/tests/Logger/WarningTest.php +++ b/tests/Logger/WarningTest.php @@ -12,13 +12,27 @@ */ class WarningTest extends TestCase { + /** + * @var string + */ + private $outputFileBaseName; + + /** + * @var string + */ + private $outputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + $this->outputFileBaseName = '/tmp/warning-log-test.log'; + $this->outputFileName = '/tmp/warning-log-test-' . date('Y-m-d') . '.log'; + + if (file_exists($this->outputFileName)) { + unlink($this->outputFileName); } } @@ -33,11 +47,12 @@ public function outputWarningLog() ]; $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setFileName($this->outputFileBaseName); $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG); $logger = $loggerBuilder->build(); $logger->warning('🐶', $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -49,7 +64,7 @@ public function outputWarningLog() 'message' => '🐶', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 38, + 'line' => 53, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', From 099a7bbf568a4b9d62641ee3c8588d6a71558e4a Mon Sep 17 00:00:00 2001 From: keitakn Date: Thu, 7 Jun 2018 15:08:15 +0900 Subject: [PATCH 15/15] modify #20 refactored the test code. Variable of default log file name --- tests/Logger/LoggerTest.php | 54 +++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/tests/Logger/LoggerTest.php b/tests/Logger/LoggerTest.php index 3b39f9c..94fb909 100644 --- a/tests/Logger/LoggerTest.php +++ b/tests/Logger/LoggerTest.php @@ -7,18 +7,33 @@ /** * Class PhpJsonLoggerTest * + * Add common test code to this class for all log methods + * * @package Nekonomokochan\Tests */ class LoggerTest extends TestCase { + /** + * @var string + */ + private $defaultOutputFileBaseName; + + /** + * @var string + */ + private $defaultOutputFileName; + + /** + * Delete the log file used last time to test the contents of the log file + */ public function setUp() { parent::setUp(); + $this->defaultOutputFileBaseName = '/tmp/php-json-logger.log'; + $this->defaultOutputFileName = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - // Delete the log file to assert the log file - $defaultFile = '/tmp/php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($defaultFile)) { - unlink($defaultFile); + if (file_exists($this->defaultOutputFileName)) { + unlink($this->defaultOutputFileName); } } @@ -40,7 +55,7 @@ public function outputUserAgent() unset($_SERVER['HTTP_USER_AGENT']); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->defaultOutputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -52,7 +67,7 @@ public function outputUserAgent() 'message' => 'testOutputUserAgent', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 39, + 'line' => 54, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => $userAgent, @@ -83,7 +98,7 @@ public function outputRemoteIpAddress() unset($_SERVER['REMOTE_ADDR']); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->defaultOutputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -95,7 +110,7 @@ public function outputRemoteIpAddress() 'message' => 'testOutputRemoteIpAddress', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 82, + 'line' => 97, 'context' => $context, 'remote_ip_address' => $remoteIpAddress, 'user_agent' => 'unknown', @@ -122,7 +137,7 @@ public function setTraceIdIsOutput() $logger = $loggerBuilder->build(); $logger->info('testSetTraceIdIsOutput', $context); - $resultJson = file_get_contents('/tmp/php-json-logger-' . date('Y-m-d') . '.log'); + $resultJson = file_get_contents($this->defaultOutputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -134,7 +149,7 @@ public function setTraceIdIsOutput() 'message' => 'testSetTraceIdIsOutput', 'trace_id' => 'MyTraceID', 'file' => __FILE__, - 'line' => 123, + 'line' => 138, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', @@ -150,14 +165,13 @@ public function setTraceIdIsOutput() /** * @test - * @throws \Exception */ public function setLogFileName() { - $fileName = '/tmp/test-php-json-logger.log'; - $outputLogFile = '/tmp/test-php-json-logger-' . date('Y-m-d') . '.log'; - if (file_exists($outputLogFile)) { - unlink($outputLogFile); + $outputFileBaseName = '/tmp/test-php-json-logger.log'; + $outputFileName = '/tmp/test-php-json-logger-' . date('Y-m-d') . '.log'; + if (file_exists($outputFileName)) { + unlink($outputFileName); } $context = [ @@ -167,11 +181,11 @@ public function setLogFileName() ]; $loggerBuilder = new LoggerBuilder(); - $loggerBuilder->setFileName($fileName); + $loggerBuilder->setFileName($outputFileBaseName); $logger = $loggerBuilder->build(); $logger->info('testSetLogFileName', $context); - $resultJson = file_get_contents($outputLogFile); + $resultJson = file_get_contents($outputFileName); $resultArray = json_decode($resultJson, true); echo "\n ---- Output Log Begin ---- \n"; @@ -183,7 +197,7 @@ public function setLogFileName() 'message' => 'testSetLogFileName', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 172, + 'line' => 186, 'context' => $context, 'remote_ip_address' => '127.0.0.1', 'user_agent' => 'unknown', @@ -194,7 +208,7 @@ public function setLogFileName() $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); $this->assertSame( - $fileName, + $outputFileBaseName, $logger->getLogFileName() ); $this->assertSame($expectedLog, $resultArray); @@ -217,7 +231,7 @@ public function setLogLevel() $logger->info('testSetLogLevel', $context); $this->assertFalse( - file_exists('/tmp/php-json-logger-' . date('Y-m-d') . '.log') + file_exists($this->defaultOutputFileName) ); $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName());