Skip to content

Commit 4efc669

Browse files
authored
Enable trucate log (#40)
1 parent 6e459ee commit 4efc669

File tree

7 files changed

+72
-20
lines changed

7 files changed

+72
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/tests/tmp
55
/phpunit.xml
66
/.phpunit.result.cache
7+
/.phpunit.cache
78
/.php-cs-fixer.cache

.phpunit.cache/test-results

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ return [
7373
*/
7474
'log_max_files' => env('LOG_MAX_FILES', 5),
7575

76+
/*
77+
|--------------------------------------------------------------------------
78+
| Truncate Log file
79+
|--------------------------------------------------------------------------
80+
|
81+
| This option defines if the log file must be truncated after rotated.
82+
| If you prefer not truncate file, set this value at false.
83+
*/
84+
'truncate' => env('LOG_TRUNCATE', true),
85+
7686
/*
7787
|--------------------------------------------------------------------------
7888
| Other files to rotated

config/rotate.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
*/
3737
'log_max_files' => env('LOG_MAX_FILES', 5),
3838

39+
/*
40+
|--------------------------------------------------------------------------
41+
| Truncate Log file
42+
|--------------------------------------------------------------------------
43+
|
44+
| This option defines if the log file must be truncated after rotated.
45+
| If you prefer not truncate file, set this value at false.
46+
*/
47+
'truncate' => env('LOG_TRUNCATE', true),
48+
3949
/*
4050
|--------------------------------------------------------------------------
4151
| Other files to rotated

src/Rotate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private function buildRotateDefault(array $options = []): Rotation
3636
'then' => function ($filenameRotated, $filename) {
3737
event(new RotateWasSuccessful($filename, $filenameRotated));
3838
},
39+
'truncate' => config('rotate.truncate', true),
3940
'catch' => function ($error) {
4041
event(new RotateHasFailed('', $error));
4142
},

tests/Commands/RotateFileTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ public function setUp(): void
1313
{
1414
parent::setUp();
1515

16-
file_put_contents($this->tmpDir.'/file1', 'test');
17-
file_put_contents($this->tmpDir.'/file2', 'test');
16+
file_put_contents($this->tmpDir . '/file1', 'test');
17+
file_put_contents($this->tmpDir . '/file2', 'test');
1818
}
1919

20-
/** @test **/
21-
public function it_can_rotate_file()
20+
public function test_it_can_rotate_file()
2221
{
23-
$file1 = $this->tmpDir.'/file1';
24-
$file2 = $this->tmpDir.'/file2';
22+
$file1 = $this->tmpDir . '/file1';
23+
$file2 = $this->tmpDir . '/file2';
2524

2625
$resultCode = Artisan::call('rotate:files', [
2726
'--file' => [$file1, $file2],
@@ -30,7 +29,7 @@ public function it_can_rotate_file()
3029
Event::assertDispatched(RotateWasSuccessful::class, 2);
3130

3231
$this->assertEquals($resultCode, 0);
33-
$this->assertFileExists($file1.'.1.gz');
34-
$this->assertFileExists($file2.'.1.gz');
32+
$this->assertFileExists($file1 . '.1.gz');
33+
$this->assertFileExists($file2 . '.1.gz');
3534
}
3635
}

tests/RotateTest.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ public function test_no_rotate_if_file_logs_not_exits()
1919
Event::assertDispatched(RotateHasFailed::class, 0);
2020

2121
$this->assertEquals($resultCode, 0);
22-
$this->assertFileDoesNotExist(app()->storagePath().'/logs/laravel.log.1.gz');
22+
$this->assertFileDoesNotExist(app()->storagePath() . '/logs/laravel.log.1.gz');
2323
}
2424

2525
public function test_no_rotate_if_file_logs_is_empty()
2626
{
27-
touch(app()->storagePath().'/logs/laravel.log');
27+
touch(app()->storagePath() . '/logs/laravel.log');
2828

2929
$resultCode = Artisan::call('rotate:logs');
3030

3131
Event::assertDispatched(RotateWasSuccessful::class, 0);
3232
Event::assertDispatched(RotateHasFailed::class, 0);
3333

3434
$this->assertEquals($resultCode, 0);
35-
$this->assertFileDoesNotExist(app()->storagePath().'/logs/laravel.log.1.gz');
35+
$this->assertFileDoesNotExist(app()->storagePath() . '/logs/laravel.log.1.gz');
3636
}
3737

3838
public function test_it_not_rotate_logs_daily()
@@ -54,7 +54,7 @@ public function test_it_not_rotate_logs_daily()
5454
$this->assertEquals($resultCode, 0);
5555

5656
foreach ($files as $file) {
57-
$this->assertFileDoesNotExist($file.'.1.gz');
57+
$this->assertFileDoesNotExist($file . '.1.gz');
5858
}
5959
}
6060

@@ -64,24 +64,24 @@ public function test_it_can_rotate_logs_custom_stream_file()
6464
'driver' => 'monolog',
6565
'handler' => StreamHandler::class,
6666
'with' => [
67-
'stream' => app()->storagePath().'/logs/custom.log',
67+
'stream' => app()->storagePath() . '/logs/custom.log',
6868
],
6969
]);
7070

7171
$this->app['config']->set('logging.default', 'custom');
7272

7373
$this->writeLog();
7474

75-
$this->assertFileExists(app()->storagePath().'/logs/custom.log');
75+
$this->assertFileExists(app()->storagePath() . '/logs/custom.log');
7676

7777
$resultCode = Artisan::call('rotate:logs');
7878

7979
Event::assertDispatched(RotateWasSuccessful::class, 1);
8080

8181
$this->assertEquals($resultCode, 0);
82-
$this->assertFileExists(app()->storagePath().'/logs/custom.log.1.gz');
82+
$this->assertFileExists(app()->storagePath() . '/logs/custom.log.1.gz');
8383

84-
unlink(app()->storagePath().'/logs/custom.log.1.gz');
84+
unlink(app()->storagePath() . '/logs/custom.log.1.gz');
8585
}
8686

8787
public function test_it_not_rotate_logs_custom_stream_std()
@@ -108,18 +108,50 @@ public function test_it_can_write_log_after_rotate()
108108
{
109109
$this->writeLog();
110110

111-
$this->assertFileExists(app()->storagePath().'/logs/laravel.log');
111+
$this->assertFileExists(app()->storagePath() . '/logs/laravel.log');
112112

113113
$resultCode = Artisan::call('rotate:logs');
114114

115115
Event::assertDispatched(RotateWasSuccessful::class, 1);
116116

117117
$this->assertEquals($resultCode, 0);
118-
$this->assertFileExists(app()->storagePath().'/logs/laravel.log.1.gz');
118+
$this->assertFileExists(app()->storagePath() . '/logs/laravel.log.1.gz');
119119

120120
$this->writeLog();
121121

122-
//$this->assertGreaterThan(0, filesize(app()->storagePath().'/logs/laravel.log'));
122+
$this->assertGreaterThan(0, filesize(app()->storagePath() . '/logs/laravel.log'));
123+
}
124+
125+
public function test_log_file_exits_if_truncate_enable()
126+
{
127+
$this->app['config']->set('rotate.truncate', true);
128+
$this->writeLog();
129+
130+
$this->assertFileExists(app()->storagePath() . '/logs/laravel.log');
131+
132+
$resultCode = Artisan::call('rotate:logs');
133+
134+
Event::assertDispatched(RotateWasSuccessful::class, 1);
135+
136+
$this->assertEquals($resultCode, 0);
137+
$this->assertFileExists(app()->storagePath() . '/logs/laravel.log.1.gz');
138+
$this->assertFileExists(app()->storagePath() . '/logs/laravel.log');
139+
}
140+
141+
public function test_log_file_does_not_exits_if_truncate_disable()
142+
{
143+
$this->app['config']->set('rotate.truncate', false);
144+
$this->writeLog();
145+
146+
$this->assertFileExists(app()->storagePath() . '/logs/laravel.log');
147+
148+
$resultCode = Artisan::call('rotate:logs');
149+
150+
Event::assertDispatched(RotateWasSuccessful::class, 1);
151+
152+
$this->assertEquals($resultCode, 0);
153+
$this->assertFileExists(app()->storagePath() . '/logs/laravel.log.1.gz');
154+
$this->assertFileDoesNotExist(app()->storagePath() . '/logs/laravel.log');
123155
}
124156

125157
public function test_rotate_foreing_files()

0 commit comments

Comments
 (0)