Skip to content

Commit 13164aa

Browse files
committed
feature: add dumper strategy
1 parent 7e300c0 commit 13164aa

22 files changed

+317
-228
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.1.0 - 2023-01-23
2+
- feature: add dumper strategy
3+
14
## 2.0.1 - 2023-01-23
25
- refactor(docs): fix readme internal links
36
- bugfix(workflow): fix git workflow php versions and update action packages versions

README.md

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# PHP Dump Logger
2-
## It's All About Readability.
3-
4-
![SaliBhdr|typhoon][link-logo]
2+
## It's All About Readability
53

64
[![Total Downloads][ico-downloads]][link-downloads]
75
[![Today Downloads][ico-today-downloads]][link-downloads]
@@ -40,22 +38,27 @@
4038

4139
## Introduction <span id="introduction"></span>
4240
PHP dump logger uses [Symfony's var-dumper][link-symfony-var-dumper] to create a simple, easy to use, eye-friendly, and pretty log files for any PHP application.
43-
If you are a fan of using `dd()` and `dump()`, this package is for you.
41+
If you are a big fan of using `dd()` and `dump()`, this package is for you.
4442

4543
**Example log file content:**
4644

4745
![php dump logger](https://symfony.com/doc/6.2/_images/01-simple.png)
4846

49-
Have you ever tried to log something with Symfony's monolog or laravel's logger facade and tried to find the logged data inside a maze of text?
47+
Have you ever tried to log something with monolog, Symfony, or Laravel logger and tried to find the logged data inside a maze of text?
5048
Especially when you don't have time and don't want to go through installing and configuring Xdebug or there is a bug in production code,
5149
and you just want to see the API responses without messing with the code execution.
52-
The first solution that comes to mind is to use a logger to log the data instead of using `dd()`, `dump()`, or `var_dump()` functions.
53-
But as I said, loggers aren't producing readable files. You have to provide them with a string, and they are incapable of directly logging a complex class.
54-
How nice would it be to have a functionality like `dd()` without interruption in the code?
50+
51+
The problem with `dd()`, `dump()`, or `var_dump()` functions is that they print the data, which is something
52+
that sometimes you don't want, especially when the code execution should not be interrupted.
53+
54+
The first solution that comes to mind is to use a logger to log the data instead of using those print functions.
55+
But as I said, loggers aren't producing readable files, and sometimes you have to only provide them with a string, and they are incapable
56+
of directly logging a complex class. How nice would it be to have a functionality like `dd()` without interruption in the code?
5557

5658
Here **php-dump-logger** comes to the rescue.
5759

58-
**php-dump-logger** uses [Symfony's var-dumper][link-symfony-var-dumper] to generate the log file content and then saves the output to a file.
60+
**php-dump-logger** uses [Symfony's var-dumper][link-symfony-var-dumper] to generate the log content
61+
and then saves the output into a file. Which is by the way really pleasant in the eye.
5962
You can either log the data in a nice `html` or `log` format or even provide your own dumper.
6063

6164
**Features**
@@ -64,7 +67,7 @@ You can either log the data in a nice `html` or `log` format or even provide you
6467
* Readable log in `log` format
6568
* Ability to log classes, arrays, objects, and basically any variable you like. There is no limit.
6669
* Fully customization of log format and dumper
67-
* Separate daily and all in one logs
70+
* Separation of the daily and all in one logs
6871
* Logging file in a custom level
6972
* Changing the path and directory of the log
7073

@@ -153,7 +156,7 @@ $logger->warning(mixed $data);
153156
$logger->notice(mixed $data);
154157
$logger->info(mixed $data);
155158
$logger->debug(mixed $data);
156-
$logger->exception(\Throwable $exception);
159+
$logger->exception(\Throwable $e);
157160
$logger->log(mixed $data);
158161

159162
```
@@ -163,8 +166,8 @@ $logger->log(mixed $data);
163166
### Exception Logging <span id="exception-logging"></span>
164167

165168
If you want to log the exception in a much more readable way You should use the `exception()` method.
166-
It is good for creating logs for exceptions like this:
167-
If You want to see the trace of the exception you can set the second argument named `$withTrace` to true:
169+
This method is good for creating logs for exceptions. If You want to see the trace of the exception you can set
170+
the second argument named `$withTrace` to true:
168171

169172
```php
170173
try {
@@ -212,8 +215,8 @@ This will create a file named `custom-level.log`.
212215
### Path <span id="path"></span>
213216

214217
By default, the path to log directory is set to be `$_SERVER['DOCUMENT_ROOT']` but if you call this logger
215-
from a console command the document rule will be null and the logger could not find a directory to save the file, and it will throw
216-
InvalidArgument Exception. So make sure to prove the directory path like so:
218+
from a console command the document root will be null and the logger could not find a directory to save the file, and it will throw
219+
`InvalidArgumentException`. So make sure to provide the directory path like so:
217220

218221
```php
219222

@@ -250,7 +253,7 @@ Sometimes you want to put your log files in a directory with restricted permissi
250253
In order to do that you can change the log file's directory permission with the `permission()` method.
251254
remember that This directory permission is only applied in the directory's first creation.
252255
So you can't change the directory's permission after the creation.
253-
Remember to provide the apache group with the right permissions to create and execute the file in the directory.
256+
Remember to provide a group with the right permissions to create and execute the file in the directory.
254257
You don't want to create a directory that even PHP could not write into it.
255258
By default, the directory and the files inside it will have 0775 permission.
256259

@@ -289,7 +292,7 @@ info-2023-02-01.log
289292

290293
Calling `silent()` method allows you to log data without throwing an error.
291294

292-
Remember that in some cases the logger will throw an error like when the $path is empty, or when it couldn't write into the target file for permission reasons.
295+
Remember, in some cases the logger will throw an error when the $path is empty, or when it couldn't write into the target file for permission reasons.
293296

294297
So if you want to avoid that, and you don't want to interrupt the code execution, you can call `silent()` method.
295298
In the background this method will force the logger to execute the code in try-catch block and return a boolean instead of the exception.
@@ -361,7 +364,7 @@ $logger = Logger::html();
361364

362365
Raw Logger is base logger class that other loggers are using it. The only difference between Raw Logger and the others is that there
363366
is no dumper or path specified in this logger, and you have to provide a dumper and the file extension with `dumper()` and the path with `path()` method.
364-
Otherwise, it will throw `SaliBhdr\DumpLog\ExceptionsInvalidArgumentException`
367+
Otherwise, it will throw `SaliBhdr\DumpLog\Exceptions\InvalidArgumentException`
365368

366369
Remember that the dumper should be the instance of `Symfony\Component\VarDumper\Dumper\AbstractDumper`.
367370
Feel free to create your own dumper and use the Raw logger.
@@ -386,37 +389,75 @@ $logger->path('__path_to_dir__')
386389

387390
### Custom Logger <span id="custom-logger"></span>
388391

389-
You can create your own dump logger by implementing the `SaliBhdr\DumpLog\Contracts\DumpLoggerInterface` or `SaliBhdr\DumpLog\Contracts\ChangeableDumperLoggerInterface`.
390-
You can also use the RawLogger in your own logger by first instantiating the RawLogger in your Logger and then use the `SaliBhdr\DumpLog\Traits\LogsThroughRawLogger` trait.
392+
You can create your own dump logger by implementing one of these interfaces:
393+
- `SaliBhdr\DumpLog\Contracts\DumpLoggerInterface` : Without the ability to change the dumper
394+
- `SaliBhdr\DumpLog\Contracts\DumpLoggerAwareInterface` : You have to add `dumper()` setter
391395

392-
example:
396+
You can also use the RawLogger in your own logger by first instantiating
397+
the RawLogger in your Logger and then use the
398+
`SaliBhdr\DumpLog\Traits\LogsThroughRawLogger` trait.
399+
400+
If you use `DumpLoggerAwareInterface` you should keep in mind that the dumper should
401+
implement `SaliBhdr\DumpLog\Contracts\DumperStrategyInterface`;
402+
403+
Example dumper strategy:
393404

394405
```php
395406
<?php
396407

397-
use App\CustomDumper;
398-
use SaliBhdr\DumpLog\Contracts\DumpLoggerInterface;
408+
namespace App;
409+
410+
use SaliBhdr\DumpLog\Contracts\DumperStrategyInterface;
411+
use Symfony\Component\VarDumper\Dumper\AbstractDumper;
399412
use Symfony\Component\VarDumper\Dumper\CliDumper;
400413

414+
class CustomDumperStrategy implements DumperStrategyInterface
415+
{
416+
417+
public function getDumper() : AbstractDumper
418+
{
419+
# Here we want to render the log content with CliDumper
420+
return new CliDumper();
421+
}
422+
423+
public function getExtension() : string
424+
{
425+
# Here we want to save it as text file
426+
return 'txt';
427+
}
428+
429+
public function getTitle(): string
430+
{
431+
# title will be shown on top of each log (usually it's better to add the date and time)
432+
return "\n" . date('H:i:s') . "\n"
433+
}
434+
435+
}
436+
437+
```
438+
439+
And then you can use your own dumper strategy:
440+
441+
```php
442+
<?php
443+
444+
use App\CustomDumperStrategy;
445+
use SaliBhdr\DumpLog\Contracts\DumpLoggerInterface;
446+
401447
class CustomTextLogger implements DumpLoggerInterface
402448
{
403449
use LogsThroughRawLogger;
404450

405-
/**
406-
* @var RawLogger
407-
*/
408451
protected $logger;
409452

410453
public function __construct()
411-
{
412-
$dumper = new CustomDumper() or new CliDumper();
413-
$extension = 'txt';
414-
454+
{
415455
$this->logger = (new RawLogger())
416-
->dumper($dumper, $extension)
456+
->dumper(new CustomDumperStrategy())
417457
->path('__path-to-dir__');
418458
}
419459
}
460+
420461
```
421462
<br />
422463

@@ -429,7 +470,7 @@ use SaliBhdr\DumpLog\Factory\Logger;
429470

430471
Logger::make()
431472
->path('__path-to-dir__')
432-
->daily('__custom-dir-name__')
473+
->dir('__custom-dir-name__')
433474
->daily(true)
434475
->silent(true)
435476
->permission(0777)
@@ -493,7 +534,6 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
493534

494535
[link-codecov]: https://codecov.io/gh/SaliBhdr/php-dump-logger
495536
[link-testing]: https://github.com/SaliBhdr/php-dump-logger/actions/workflows/testing.yml
496-
[link-logo]: https://drive.google.com/a/domain.com/thumbnail?id=12yntFCiYIGJzI9FMUaF9cRtXKb0rXh9X
497537
[link-packagist]: https://packagist.org/packages/salibhdr/php-dump-logger
498538
[link-downloads]: https://packagist.org/packages/salibhdr/php-dump-logger/stats
499539
[link-packagist]: https://packagist.org/packages/salibhdr/php-dump-logger

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.1
1+
2.1.0

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"scripts": {
4949
"csfix": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist --allow-risky=yes",
50-
"test": "vendor/bin/phpunit --testdox",
50+
"test": "XDEBUG_MODE=coverage vendor/bin/phpunit --testdox",
5151
"testfix": [
5252
"@csfix",
5353
"@test"

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@
2828
</filter>
2929
<php>
3030
<env name="APP_ENV" value="testing"/>
31+
<ini name="error_reporting" value="-1" />
32+
<ini name="xdebug.mode" value="coverage" />
33+
<env name="XDEBUG_MODE" value="coverage" />
3134
</php>
3235
</phpunit>

src/Contracts/ChangeableDumperLoggerInterface.php

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace SaliBhdr\DumpLog\Contracts;
4+
5+
interface DumpLoggerAwareInterface extends DumpLoggerInterface
6+
{
7+
/**
8+
* This method helps to change the symfony's dumper
9+
*
10+
* @param DumperStrategyInterface $dumper
11+
*
12+
* @return $this
13+
*/
14+
public function dumper(DumperStrategyInterface $dumper): self;
15+
}

src/Contracts/DumpLoggerInterface.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@ interface DumpLoggerInterface extends LoggerInterface
77
/**
88
* Changes the base path of the directory that the log files should be saved
99
*
10-
* Default value is global server variable's 'DOCUMENT_ROOT' if exists or current directory which the logger is called
11-
*
1210
* @param string $path
1311
*
1412
* @return $this
1513
*/
16-
public function path(string $path): DumpLoggerInterface;
14+
public function path(string $path): self;
1715

1816
/**
1917
* Changes the name of the logs directory
2018
*
21-
* Default directory name is dump
22-
*
2319
* @param string $dir
2420
*
2521
* @return $this
@@ -29,8 +25,6 @@ public function dir(string $dir): self;
2925
/**
3026
* Changes the permission of the log directory
3127
*
32-
* Default is 0770 (all access to owner and group)
33-
*
3428
* @param int $permission
3529
*
3630
* @return $this
@@ -49,7 +43,7 @@ public function daily(bool $isDaily = true): self;
4943
/**
5044
* If set to true the logger will not throw error and simply will return boolean as result
5145
*
52-
* This is useful when you don't want the logger to interfere in the code execution and throw exception
46+
* This is useful when you want to prevent the logger from throwing an exception
5347
*
5448
* @param bool $silent
5549
*
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace SaliBhdr\DumpLog\Contracts;
4+
5+
use Symfony\Component\VarDumper\Dumper\AbstractDumper;
6+
7+
interface DumperStrategyInterface
8+
{
9+
/**
10+
* The dumper that is used for rendering the log content
11+
*
12+
* @return AbstractDumper
13+
*/
14+
public function getDumper(): AbstractDumper;
15+
16+
/**
17+
* The extension of log file
18+
*
19+
* @return string
20+
*/
21+
public function getExtension(): string;
22+
23+
/**
24+
* Title will be shown on top of each log (usually it's better to add the date and time)
25+
*
26+
* @return string
27+
*/
28+
public function getTitle(): string;
29+
}

src/Contracts/FactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface FactoryInterface
66
{
7-
public static function make(string $type): DumpLoggerInterface;
7+
public static function make(string $type);
88
}

0 commit comments

Comments
 (0)