1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* @author Carsten Brandt <mail@cebe.cc>
*/
namespace yiiunit\framework\log;
use yii\helpers\FileHelper;
use yii\log\Dispatcher;
use yii\log\Logger;
use yii\log\Target;
use Yii;
use yiiunit\TestCase;
/**
* @group log
*/
class FileTargetTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}
public function booleanDataProvider()
{
return [
[true],
[false]
];
}
/**
* @dataProvider booleanDataProvider
*/
public function testRotate($rotateByCopy)
{
$logFile = Yii::getAlias('@yiiunit/runtime/log/filetargettest.log');
FileHelper::removeDirectory(dirname($logFile));
mkdir(dirname($logFile), 0777, true);
$logger = new Logger();
$dispatcher = new Dispatcher([
'logger' => $logger,
'targets' => [
'file' => [
'class' => 'yii\log\FileTarget',
'logFile' => $logFile,
'levels' => ['warning'],
'maxFileSize' => 1024, // 1 MB
'maxLogFiles' => 1, // one file for rotation and one normal log file
'logVars' => [],
'rotateByCopy' => $rotateByCopy
]
]
]);
// one file
$logger->log(str_repeat('x', 1024), Logger::LEVEL_WARNING);
$logger->flush(true);
clearstatcache();
$this->assertTrue(file_exists($logFile));
$this->assertFalse(file_exists($logFile . '.1'));
$this->assertFalse(file_exists($logFile . '.2'));
$this->assertFalse(file_exists($logFile . '.3'));
$this->assertFalse(file_exists($logFile . '.4'));
// exceed max size
for($i = 0; $i < 1024; $i++) {
$logger->log(str_repeat('x', 1024), Logger::LEVEL_WARNING);
}
$logger->flush(true);
// first rotate
$logger->log(str_repeat('x', 1024), Logger::LEVEL_WARNING);
$logger->flush(true);
clearstatcache();
$this->assertTrue(file_exists($logFile));
$this->assertTrue(file_exists($logFile . '.1'));
$this->assertFalse(file_exists($logFile . '.2'));
$this->assertFalse(file_exists($logFile . '.3'));
$this->assertFalse(file_exists($logFile . '.4'));
// second rotate
for($i = 0; $i < 1024; $i++) {
$logger->log(str_repeat('x', 1024), Logger::LEVEL_WARNING);
}
$logger->flush(true);
clearstatcache();
$this->assertTrue(file_exists($logFile));
$this->assertTrue(file_exists($logFile . '.1'));
$this->assertFalse(file_exists($logFile . '.2'));
$this->assertFalse(file_exists($logFile . '.3'));
$this->assertFalse(file_exists($logFile . '.4'));
}
}