Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
21 changes: 16 additions & 5 deletions src/PhpJsonLogger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class Logger
*/
private $logFileName;

/**
* @var int
* @see \Monolog\Handler\RotatingFileHandler::$maxFiles
*/
private $maxFiles;

/**
* Logger constructor.
*
Expand All @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down
28 changes: 28 additions & 0 deletions src/PhpJsonLogger/LoggerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -95,6 +100,12 @@ class LoggerBuilder
*/
private $skipStackFramesCount;

/**
* @var int
* @see \Monolog\Handler\RotatingFileHandler::$maxFiles
*/
private $maxFiles;

/**
* LoggerBuilder constructor.
*
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions tests/Logger/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}