Skip to content

Commit 4457754

Browse files
committed
init
0 parents  commit 4457754

20 files changed

+837
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
vendor
3+
composer.lock
4+
tests/files

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
env:
9+
matrix:
10+
- COMPOSER_FLAGS="--prefer-lowest"
11+
- COMPOSER_FLAGS=""
12+
13+
before_script:
14+
- travis_retry composer self-update
15+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
16+
17+
script:
18+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Cesar Garcia
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# PHP class to logs rotation
3+
PHP Class to Rotate files with compression
4+
5+
## Usage
6+
7+
This is an example:
8+
9+
```php
10+
use use Cesargb\Log\Rotation;
11+
12+
$fileLog='file.log';
13+
14+
$rotation = new Rotation();
15+
16+
$rotation->addProcessor(new GzProcessor());
17+
18+
$rotation->addProcessor(
19+
(new RotativeProcessor())->setMaxFiles(7)
20+
);
21+
22+
$rotation->rotate($fileLog);
23+
```

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "cesargb/php-log-rotation",
3+
"description": "PHP Class to rotate log files",
4+
"type": "library",
5+
"keywords": [
6+
"php",
7+
"log",
8+
"rotation"
9+
],
10+
"homepage": "https://github.com/cesargb/php-log-rotation",
11+
"authors": [
12+
{
13+
"name": "Cesar Garcia",
14+
"email": "cesargb@gmail.com"
15+
}
16+
],
17+
"license": "MIT",
18+
"require": {
19+
"php": "^5.6 || ^7.0"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Cesargb\\Log\\": "src"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Cesargb\\Log\\Test\\": "tests"
29+
}
30+
},
31+
"scripts": {
32+
"test": "vendor/bin/phpunit"
33+
},
34+
"require-dev": {
35+
"phpunit/phpunit": "^5.7 || ^7.0"
36+
}
37+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="League Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Cesargb\Log\Exceptions;
4+
5+
use Exception;
6+
7+
class DirectoryIsNotValid extends Exception
8+
{
9+
public function __construct($dir, $message) {
10+
parent::__construct(sprintf('The dir %s is not writable: %s.', $dir, $message), 0, null);
11+
}
12+
}

src/Exceptions/FileIsNotValid.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Cesargb\Log\Exceptions;
4+
5+
use Exception;
6+
7+
class FileIsNotValid extends Exception
8+
{
9+
public function __construct($file, $message) {
10+
parent::__construct(sprintf('The file %s %s.', $file, $message), 0, null);
11+
}
12+
}

src/Exceptions/RotateFailed.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Cesargb\Log\Exceptions;
4+
5+
use Exception;
6+
7+
class RotateFailed extends Exception
8+
{
9+
public function __construct($fileSource, $fileDestination, $message = '') {
10+
parent::__construct(sprintf('Fail to move data from file %s to %s: %s.', $fileSource, $fileDestination, $message), 0, null);
11+
}
12+
}

src/Handlers/AbstractHandler.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Cesargb\Log\Handlers;
4+
5+
use Exception;
6+
use Cesargb\Log\Exceptions\RotateFailed;
7+
use Cesargb\Log\Exceptions\FileIsNotValid;
8+
use Cesargb\Log\Exceptions\DirectoryIsNotValid;
9+
10+
abstract class AbstractHandler implements HandlerInterface
11+
{
12+
13+
protected $file;
14+
15+
protected $compress;
16+
17+
public function __construct($file, bool $compress = true)
18+
{
19+
$this->file = $file;
20+
21+
$this->compress = $compress;
22+
23+
clearstatcache();
24+
}
25+
26+
protected function validateSource()
27+
{
28+
if (! is_writable($this->file)) {
29+
return new FileIsNotValid($this->file, 'is not writable');
30+
}
31+
32+
return true;
33+
}
34+
35+
protected function validateDestination($fileDestination)
36+
{
37+
$dir_destination = dirname($fileDestination);
38+
39+
if (! is_dir($dir_destination)) {
40+
if (! file_exists($dir_destination)) {
41+
if (! mkdir($dir_destination, 0777, true)) {
42+
return new DirectoryIsNotValid($this->file, 'Not is writable');
43+
}
44+
} else {
45+
return new DirectoryIsNotValid($this->file, 'Exists and is not a directory');
46+
}
47+
}
48+
49+
if (! is_writable($dir_destination)) {
50+
return new DirectoryIsNotValid($this->file, 'Not is writable');
51+
}
52+
53+
return true;
54+
}
55+
56+
protected function moveTo($fileDestination)
57+
{
58+
if ($e = $this->validateSource($this->file) !== true) {
59+
return $e;
60+
}
61+
62+
if ($e = $this->validateDestination($fileDestination) !== true) {
63+
return $e;
64+
}
65+
66+
if (filesize($this->file) == 0) {
67+
return true;
68+
}
69+
70+
$fdSource = fopen($this->file, 'r+');
71+
72+
if (! $fdSource) {
73+
return new FileIsNotValid($this->file, 'is not writable');
74+
}
75+
76+
if (! flock($fdSource, LOCK_EX)) {
77+
fclose($fdSource);
78+
79+
return new FileIsNotValid($this->file, 'is not lockeable');
80+
}
81+
82+
if (! copy($this->file, $fileDestination)) {
83+
fclose($fdSource);
84+
85+
return new RotateFailed($this->file, $fileDestination, 'fail to copy data');
86+
}
87+
88+
if (! ftruncate($fdSource, 0)) {
89+
fclose($fdSource);
90+
91+
unlink($fileDestination);
92+
93+
return new FileIsNotValid($this->file, 'fail to truncate');
94+
}
95+
96+
flock($fdSource, LOCK_UN);
97+
98+
fflush($fdSource);
99+
100+
fclose($fdSource);
101+
102+
clearstatcache();
103+
104+
return true;
105+
}
106+
}

0 commit comments

Comments
 (0)