diff --git a/README.md b/README.md index 76ad33b..c6ca569 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,19 @@ These are the same as `logLevel` defined in [Monolog](https://github.com/Seldaek - ALERT = 550 - EMERGENCY = 600 +#### How to change Log Rotation Date + +This is the default setting to save logs for 7 days. + +If you want to change the log Rotation date, you can change it with the following code. + +The following code sets the log retention period to 2 days. + +```php +$loggerBuilder = new LoggerBuilder(); +$loggerBuilder->setMaxFiles(2); +``` + ### Extend and use `\Nekonomokochan\PhpJsonLogger\JsonFormatter` You can make your own `\Monolog\Logger` using only `\Nekonomokochan\PhpJsonLogger\JsonFormatter`. diff --git a/src/PhpJsonLogger/Logger.php b/src/PhpJsonLogger/Logger.php index 60dc961..f7466c4 100644 --- a/src/PhpJsonLogger/Logger.php +++ b/src/PhpJsonLogger/Logger.php @@ -40,6 +40,12 @@ class Logger */ private $logFileName; + /** + * @var int + * @see \Monolog\Handler\RotatingFileHandler::$maxFiles + */ + private $maxFiles; + /** * Logger constructor. * @@ -50,19 +56,16 @@ public function __construct(LoggerBuilder $builder) { $this->createdTime = microtime(true); $this->traceId = $builder->getTraceId(); - $this->generateTraceIdIfNeeded(); - $this->logFileName = $builder->getFileName(); - $this->logLevel = $builder->getLogLevel(); + $this->maxFiles = $builder->getMaxFiles(); $formatter = new JsonFormatter(); - // TODO The maxFiles should be configurable from outside $rotating = new RotatingFileHandler( $this->getLogFileName(), - 7, + $this->maxFiles, $this->getLogLevel() ); $rotating->setFormatter($formatter); @@ -200,6 +203,14 @@ public function getLogFileName(): string return $this->logFileName; } + /** + * @return int + */ + public function getMaxFiles(): int + { + return $this->maxFiles; + } + /** * Generate if TraceID is empty */ diff --git a/src/PhpJsonLogger/LoggerBuilder.php b/src/PhpJsonLogger/LoggerBuilder.php index 4b55049..d3d5c37 100644 --- a/src/PhpJsonLogger/LoggerBuilder.php +++ b/src/PhpJsonLogger/LoggerBuilder.php @@ -68,6 +68,11 @@ class LoggerBuilder */ const DEFAULT_SKIP_STACK_FRAMES_COUNT = 0; + /** + * @see \Monolog\Handler\RotatingFileHandler::$maxFiles + */ + const DEFAULT_MAX_FILES = 7; + /** * @var string */ @@ -95,6 +100,12 @@ class LoggerBuilder */ private $skipStackFramesCount; + /** + * @var int + * @see \Monolog\Handler\RotatingFileHandler::$maxFiles + */ + private $maxFiles; + /** * LoggerBuilder constructor. * @@ -107,6 +118,7 @@ public function __construct(string $traceId = '') $this->fileName = '/tmp/php-json-logger.log'; $this->setSkipClassesPartials(self::DEFAULT_SKIP_CLASSES_PARTIALS); $this->setSkipStackFramesCount(self::DEFAULT_SKIP_STACK_FRAMES_COUNT); + $this->setMaxFiles(self::DEFAULT_MAX_FILES); } /** @@ -189,6 +201,22 @@ public function setSkipStackFramesCount(int $skipStackFramesCount) $this->skipStackFramesCount = $skipStackFramesCount; } + /** + * @return int + */ + public function getMaxFiles(): int + { + return $this->maxFiles; + } + + /** + * @param int $maxFiles + */ + public function setMaxFiles(int $maxFiles) + { + $this->maxFiles = $maxFiles; + } + /** * @return Logger * @throws \Exception diff --git a/tests/Logger/LoggerTest.php b/tests/Logger/LoggerTest.php index d517c45..41fe2e1 100644 --- a/tests/Logger/LoggerTest.php +++ b/tests/Logger/LoggerTest.php @@ -283,4 +283,44 @@ public function outputHttpXForwardedFor() $this->assertSame('PhpJsonLogger', $logger->getMonologInstance()->getName()); $this->assertSame($expectedLog, $resultArray); } + + /** + * @test + */ + public function canSetMaxFiles() + { + $context = [ + 'name' => 'keitakn', + ]; + + $loggerBuilder = new LoggerBuilder(); + $loggerBuilder->setMaxFiles(2); + $logger = $loggerBuilder->build(); + $logger->info('testSetTraceIdIsOutput', $context); + + $resultJson = file_get_contents($this->defaultOutputFileName); + $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' => 'testSetTraceIdIsOutput', + 'trace_id' => $logger->getTraceId(), + 'file' => __FILE__, + 'line' => 299, + '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(2, $logger->getMaxFiles()); + $this->assertSame($expectedLog, $resultArray); + } }