Skip to content

Commit

Permalink
Add log file format by date
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Aug 9, 2019
1 parent f3a05da commit f649df2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"SwoftTest\\Stdlib\\Unit\\": "src/stdlib/test/unit/",
"SwoftTest\\Stdlib\\Testing\\": "src/stdlib/test/testing/",
"SwoftTest\\Log\\Unit\\": "src/log/test/unit/",
"SwoftTest\\Log\\Testing\\": "src/log/test/testing/",
"SwoftTest\\Config\\Testing\\": "src/config/test/testing/",
"SwoftTest\\Config\\Unit\\": "src/config/test/unit/",
"SwoftTest\\Auth\\Unit\\": "src/auth/test/unit/",
Expand Down
26 changes: 26 additions & 0 deletions src/log/src/Handler/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class FileHandler extends AbstractProcessingHandler
public function init(): void
{
$this->logFile = alias($this->logFile);
$this->logFile = $this->formatFile($this->logFile);
$this->createDir();

if (is_array($this->levels)) {
Expand Down Expand Up @@ -191,4 +192,29 @@ public function isHandling(array $record): bool

return in_array($record['level'], $this->levelValues, true);
}

public function formatFile(string $logFile): string
{
$logPath = dirname($logFile);
$fileName = basename($logFile);

$math = [];
if (!preg_match('/%(.*)\{(.*)\}/', $fileName, $math)) {
return $logFile;
}

$type = $math[1];
$value = $math[2];

// Date format
$formatFile = $logFile;
switch ($type) {
case 'd':
$formatValue = date($value);
$formatFile = str_replace("%{$type}{{$value}}", $formatValue, $logFile);
break;
}

return $formatFile;
}
}
5 changes: 5 additions & 0 deletions src/log/test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
// vendor at component dir
use Composer\Autoload\ClassLoader;
use SwoftTest\Testing\TestApplication;

if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
require dirname(__DIR__) . '/vendor/autoload.php';
Expand Down Expand Up @@ -31,3 +32,7 @@
} else {
exit('Please run "composer install" to install the dependencies' . PHP_EOL);
}

$application = new TestApplication();
$application->setBeanFile(__DIR__ . '/testing/bean.php');
$application->run();
28 changes: 28 additions & 0 deletions src/log/test/testing/bean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Monolog\Formatter\LineFormatter;
use Swoft\Log\Handler\FileHandler;
use Swoft\Log\Logger;

return [
'lineFormatter' => [
'class' => LineFormatter::class,
'format' => '%datetime% [%level_name%] [%channel%] [%event%] [tid:%tid%] [cid:%cid%] [traceid:%traceid%] [spanid:%spanid%] [parentid:%parentid%] %messages%',
'dateFormat' => 'Y-m-d H:i:s',
],
'testFileHandler' => [
'class' => FileHandler::class,
'logFile' => '@runtime/logs/error.log',
'formatter' => bean('lineFormatter'),
'levels' => 'error,warning',
],
'logger' => [
'class' => Logger::class,
'flushRequest' => false,
'enable' => false,
'handlers' => [
'application' => bean('applicationHandler'),
'notice' => bean('noticeHandler'),
],
]
];
37 changes: 37 additions & 0 deletions src/log/test/unit/FileHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);


namespace SwoftTest\Log\Unit;


use PHPUnit\Framework\TestCase;
use Swoft\Bean\BeanFactory;
use Swoft\Log\Handler\FileHandler;

/**
* Class FileHandlerTest
*
* @since 2.0
*/
class FileHandlerTest extends TestCase
{
public function testFormatFile(): void
{
$result = $this->getHandler()->formatFile("notice-%d{Y-m-d}.log");
$this->assertEquals($result, 'notice-' . date('Y-m-d') . '.log');

$result = $this->getHandler()->formatFile("notice.log");
$this->assertEquals($result, 'notice.log');

$result = $this->getHandler()->formatFile("%d{Y-m-d}notice.log");
$this->assertEquals($result, date('Y-m-d') . 'notice.log');

$result = $this->getHandler()->formatFile("notice.log%d{Y-m-d}");
$this->assertEquals($result, 'notice.log' . date('Y-m-d'));
}

private function getHandler(): FileHandler
{
return BeanFactory::getBean('testFileHandler');
}
}

0 comments on commit f649df2

Please sign in to comment.