diff --git a/README.md b/README.md index 404e616..953f872 100644 --- a/README.md +++ b/README.md @@ -199,5 +199,167 @@ These are the same as `logLevel` defined in [Monolog](https://github.com/Seldaek - ALERT = 550 - EMERGENCY = 600 +### Extend and use `\Nekonomokochan\PhpJsonLogger\JsonFormatter` + +You can make your own `\Monolog\Logger` using only `\Nekonomokochan\PhpJsonLogger\JsonFormatter`. + +This method is useful when you need `\Monolog\Logger` in web application framework(e.g. [Laravel](https://github.com/laravel/laravel)). + +The following is sample code. + +```php +setFormatter($formatter); + +$introspection = new IntrospectionProcessor( + Logger::INFO, + ['Nekonomokochan\\PhpJsonLogger\\'], + 0 +); + +$extraRecords = function ($record) { + $record['extra']['trace_id'] = 'ExtendedMonologTestTraceId'; + $record['extra']['created_time'] = microtime(true); + + return $record; +}; + +$extendedMonolog = new Logger( + 'ExtendedMonolog', + [$rotating], + [$introspection, $extraRecords] +); + +// output info log +$context = [ + 'cat' => '🐱', + 'dog' => '🐶', + 'rabbit' => '🐰', +]; + +$extendedMonolog->info('outputInfoLogTest', $context); +``` + +It is output to `extended-monolog-test-yyyy-mm-dd.log` as follows + +```json +{ + "log_level": "INFO", + "message": "outputInfoLogTest", + "trace_id": "ExtendedMonologTestTraceId", + "file": "\/home\/vagrant\/php-json-logger\/tests\/ExtendedMonologTest.php", + "line": 85, + "context": { + "cat": "🐱", + "dog": "🐶", + "rabbit": "🐰" + }, + "remote_ip_address": "127.0.0.1", + "user_agent": "unknown", + "datetime": "2018-06-06 17:14:26.042013", + "timezone": "Asia\/Tokyo", + "process_time": 0.1678466796875 +} +``` + +The following code is necessary to output `trace_id` and `process_time`. + +```php +$extraRecords = function ($record) { + $record['extra']['trace_id'] = 'ExtendedMonologTestTraceId'; + $record['extra']['created_time'] = microtime(true); + + return $record; +}; +``` + +The code below is the code necessary to normally display the stack trace with JSON. + +```php +$introspection = new IntrospectionProcessor( + Logger::INFO, + ['Nekonomokochan\\PhpJsonLogger\\'], + 0 +); +``` + +To output the stack trace to the log, execute the following code. + +```php +$exception = new \Exception('ExtendedMonologTest.outputErrorLog', 500); +$context = [ + 'cat' => '🐱(=^・^=)🐱', + 'dog' => '🐶Uo・ェ・oU🐶', + 'rabbit' => '🐰🐰🐰', +]; + +$extendedMonolog->error( + get_class($exception), + $this->formatPhpJsonLoggerErrorsContext($exception, $context) +); +``` + +Please pay attention to the part `$this->formatPhpJsonLoggerErrorsContext($exception, $context)`. + +This is necessary processing to format the error log into JSON and output it. + +This is the method implemented in `\Nekonomokochan\PhpJsonLogger\ErrorsContextFormat`. + +It is output to `extended-monolog-test-yyyy-mm-dd.log` as follows. + +```json +{ + "log_level": "ERROR", + "message": "Exception", + "trace_id": "ExtendedMonologTestTraceId", + "file": "\/home\/vagrant\/php-json-logger\/tests\/ExtendedMonologTest.php", + "line": 126, + "context": { + "cat": "🐱(=^・^=)🐱", + "dog": "🐶Uo・ェ・oU🐶", + "rabbit": "🐰🐰🐰" + }, + "remote_ip_address": "127.0.0.1", + "user_agent": "unknown", + "datetime": "2018-06-06 17:37:57.440757", + "timezone": "Asia\/Tokyo", + "process_time": 0.16093254089355469, + "errors": { + "message": "ExtendedMonologTest.outputErrorLog", + "code": 500, + "file": "\/home\/vagrant\/php-json-logger\/tests\/ExtendedMonologTest.php", + "line": 117, + "trace": [ + "#0 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php(1145): Nekonomokochan\\Tests\\ExtendedMonologTest->outputErrorLog()", + "#1 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php(840): PHPUnit\\Framework\\TestCase->runTest()", + "#2 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestResult.php(645): PHPUnit\\Framework\\TestCase->runBare()", + "#3 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php(798): PHPUnit\\Framework\\TestResult->run()", + "#4 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/Framework\/TestSuite.php(776): PHPUnit\\Framework\\TestCase->run()", + "#5 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/TextUI\/TestRunner.php(529): PHPUnit\\Framework\\TestSuite->run()", + "#6 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/TextUI\/Command.php(198): PHPUnit\\TextUI\\TestRunner->doRun()", + "#7 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/src\/TextUI\/Command.php(151): PHPUnit\\TextUI\\Command->run()", + "#8 \/home\/vagrant\/php-json-logger\/vendor\/phpunit\/phpunit\/phpunit(53): PHPUnit\\TextUI\\Command::main()" + ] + } +} +``` + +If you want to know more detailed usage, please look at `php-json-logger/tests/ExtendedMonologTest.php`. + ## License MIT diff --git a/src/PhpJsonLogger/ErrorsContextFormat.php b/src/PhpJsonLogger/ErrorsContextFormat.php index dd4f0d1..c56361a 100644 --- a/src/PhpJsonLogger/ErrorsContextFormat.php +++ b/src/PhpJsonLogger/ErrorsContextFormat.php @@ -1,6 +1,11 @@ logFileName, + 7, + Logger::INFO + ); + $rotating->setFormatter($formatter); + + $introspection = new IntrospectionProcessor( + Logger::INFO, + ['Nekonomokochan\\PhpJsonLogger\\'], + 0 + ); + + $extraRecords = function ($record) { + $record['extra']['trace_id'] = 'ExtendedMonologTestTraceId'; + $record['extra']['created_time'] = microtime(true); + + return $record; + }; + + $this->extendedMonolog = new Logger( + 'ExtendedMonolog', + [$rotating], + [$introspection, $extraRecords] + ); + } + + /** + * @test + */ + public function outputInfoLog() + { + $context = [ + 'cat' => '🐱', + 'dog' => '🐶', + 'rabbit' => '🐰', + ]; + + $this->extendedMonolog->info('outputInfoLogTest', $context); + + $resultJson = file_get_contents('/tmp/extended-monolog-test-' . 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' => 'outputInfoLogTest', + 'trace_id' => 'ExtendedMonologTestTraceId', + 'file' => __FILE__, + 'line' => 85, + '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('ExtendedMonolog', $this->extendedMonolog->getName()); + $this->assertSame($expectedLog, $resultArray); + } + + /** + * @test + */ + public function outputErrorLog() + { + $exception = new \Exception('ExtendedMonologTest.outputErrorLog', 500); + $context = [ + 'cat' => '🐱(=^・^=)🐱', + 'dog' => '🐶Uo・ェ・oU🐶', + 'rabbit' => '🐰🐰🐰', + ]; + + $this->extendedMonolog->error( + get_class($exception), + $this->formatPhpJsonLoggerErrorsContext($exception, $context) + ); + + $resultJson = file_get_contents('/tmp/extended-monolog-test-' . 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' => get_class($exception), + 'trace_id' => 'ExtendedMonologTestTraceId', + 'file' => __FILE__, + 'line' => 126, + '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' => $exception->getMessage(), + 'code' => $exception->getCode(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + 'trace' => $resultArray['errors']['trace'], + ], + ]; + + $this->assertSame('ExtendedMonolog', $this->extendedMonolog->getName()); + $this->assertSame($expectedLog, $resultArray); + } +}