├── .php-cs-fixer.cache ├── .php-cs-fixer.dist.php ├── .phpunit.cache └── test-results ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── LogDumper.php ├── LogDumperServiceProvider.php └── helpers.php /.php-cs-fixer.cache: -------------------------------------------------------------------------------- 1 | {"php":"8.3.17","version":"3.69.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces_position":true,"class_definition":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"single_space_around_construct":{"constructs_followed_by_a_single_space":["abstract","as","case","catch","class","do","else","elseif","final","for","foreach","function","if","interface","namespace","private","protected","public","static","switch","trait","try","use_lambda","while"],"constructs_preceded_by_a_single_space":["as","else","elseif","use_lambda"]},"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true},"hashes":{"src\/LogDumperServiceProvider.php":"2214c7337c0cf12079fbfe0e1c982ece","src\/LogDumper.php":"aca586f81a69bc2a704c0cc0c68c5613","src\/helpers.php":"a72b1093d0951aaaf2fa8c3dac5aec09","tests\/TestCase.php":"5d120abc5f0ec8d3c6612fb9a1d7a834","tests\/LogDumperTest.php":"189ce409aafff7cf1a5f2158ff8e34a2","tests\/LogFake.php":"22547f0e4756ec144a73d1042d4cfdd1"}} -------------------------------------------------------------------------------- /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | notPath('bootstrap/*') 5 | ->notPath('storage/*') 6 | ->notPath('resources/view/mail/*') 7 | ->in([ 8 | __DIR__ . '/src', 9 | __DIR__ . '/tests', 10 | ]) 11 | ->name('*.php') 12 | ->notName('*.blade.php') 13 | ->ignoreDotFiles(true) 14 | ->ignoreVCS(true); 15 | 16 | return (new PhpCsFixer\Config) 17 | ->setRules([ 18 | '@PSR2' => true, 19 | 'array_syntax' => ['syntax' => 'short'], 20 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 21 | 'no_unused_imports' => true, 22 | 'not_operator_with_successor_space' => true, 23 | 'trailing_comma_in_multiline' => true, 24 | 'phpdoc_scalar' => true, 25 | 'unary_operator_spaces' => true, 26 | 'binary_operator_spaces' => true, 27 | 'blank_line_before_statement' => [ 28 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 29 | ], 30 | 'phpdoc_single_line_var_spacing' => true, 31 | 'phpdoc_var_without_name' => true, 32 | 'method_argument_space' => [ 33 | 'on_multiline' => 'ensure_fully_multiline', 34 | 'keep_multiple_spaces_after_comma' => true, 35 | ] 36 | ]) 37 | ->setFinder($finder); 38 | -------------------------------------------------------------------------------- /.phpunit.cache/test-results: -------------------------------------------------------------------------------- 1 | {"version":1,"defects":[],"times":{"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_a_single_thing":0.018,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_multiple_things":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_an_array":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_an_object":0.001,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_will_not_blow_up_when_passing_no_exceptions":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_debug_level_stuff":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_using_the_notice_level":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_using_the_info_level":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_using_the_alert_level":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_using_the_warning_level":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_using_the_error_level":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_using_the_critical_level":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_using_the_emergency_level":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_mix_levels":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_disable_output":0.001,"Spatie\\LogDumper\\Tests\\LogDumperTest::enable_accepts_a_boolean":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_start_logging_queries":0.003,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_stop_logging_queries":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::calling_log_queries_twice_will_not_log_all_queries_twice":0,"Spatie\\LogDumper\\Tests\\LogDumperTest::it_can_log_all_queries_in_a_callable":0}} -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-log-dump` will be documented in this file 4 | 5 | ## 1.6.0 - 2025-02-17 6 | 7 | ### What's Changed 8 | 9 | * Laravel 12.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-log-dumper/pull/9 10 | 11 | **Full Changelog**: https://github.com/spatie/laravel-log-dumper/compare/1.5.0...1.6.0 12 | 13 | ## Support Laravel 11 - 2024-04-02 14 | 15 | ### What's Changed 16 | 17 | * Laravel 11.x Compatibility https://github.com/spatie/laravel-log-dumper/pull/8 18 | 19 | **Full Changelog**: https://github.com/spatie/laravel-log-dumper/compare/1.4.2...1.5.0 20 | 21 | ## 1.4.2 - 2023-02-15 22 | 23 | - support Laravel 10 24 | 25 | ## 1.4.1 - 2022-01-19 26 | 27 | - support Laravel 9 28 | 29 | ## 1.4.0 - 2020-11-26 30 | 31 | - Support PHP 8.0 32 | 33 | ## 1.3.2 - 2020-09-09 34 | 35 | - Support Laravel 8 36 | 37 | ## 1.3.1 - 2020-07-17 38 | 39 | - fix spelling of `stopLoggingQueries` (#5) 40 | 41 | ## 1.3.0 - 2020-07-17 42 | 43 | - add query logging 44 | 45 | ## 1.2.0 - 2020-06-18 46 | 47 | - add `enable` and `disable` 48 | 49 | ## 1.1.0 - 2020-04-14 50 | 51 | - add support for different log levels 52 | 53 | ## 1.0.0 - 2020-04-07 54 | 55 | - initial release 56 | 57 | ## 0.0.1 - 2020-04-03 58 | 59 | - experimental release 60 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A function to dump anything to the log 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-log-dumper.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-log-dumper) 4 | [![run-tests](https://github.com/spatie/laravel-log-dumper/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-log-dumper/actions/workflows/run-tests.yml) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-log-dumper.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-log-dumper) 6 | 7 | This package contains a function `ld`. Any argument you pass to it will be dumped to the log. You can pass any kind of value to it. 8 | 9 | ```php 10 | ld('a string', ['an array'], new Class()); 11 | ``` 12 | 13 | Under the hood, Symfony's `VarDumper` is used to create string representations. 14 | 15 | ## Support us 16 | 17 | [](https://spatie.be/github-ad-click/laravel-log-dumper) 18 | 19 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 20 | 21 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 22 | 23 | ## Installation 24 | 25 | You can install the package via composer: 26 | 27 | ```bash 28 | composer require spatie/laravel-log-dumper 29 | ``` 30 | 31 | ## Usage 32 | 33 | You can pass any variable you want to `ld`. 34 | 35 | ```php 36 | ld('a string', ['an array'], new Class()); 37 | ``` 38 | 39 | All arguments will be converted to strings and will be written to the application log using the `info` level. 40 | 41 | ## Using other log levels 42 | 43 | If you want to use another log level, you can just call the method you want on `ld`. You can pass these methods any type of argument and any number of arguments. They will all be logged. 44 | 45 | ```php 46 | // logs using the `error` level 47 | ld()->error('a string', ['an array'], new Class()) 48 | ``` 49 | 50 | Of course, you can mix and chain different levels. 51 | 52 | ```php 53 | ld() 54 | ->debug('Debug info', ['an array']) 55 | ->error('Error info', new Class); 56 | ``` 57 | 58 | ## Enabling and disabling logging 59 | 60 | You can disable logging by calling `disable`. 61 | 62 | ```php 63 | ld('foo'); // will be logged 64 | 65 | ld()->disable(); 66 | 67 | ld('bar'); // will not be logged 68 | 69 | ld()->enable(); 70 | 71 | ld('baz'); // will be logged 72 | ``` 73 | 74 | You can pass a boolean to `enable`. This can be handy when you want to log only one iteration of a loop. 75 | 76 | ```php 77 | foreach (range(1, 3) as $i) { 78 | // only things in the third iteration will be logged 79 | ld()->enable($i === 3); 80 | 81 | ld('we are in the third iteration'); 82 | } 83 | ``` 84 | 85 | ## Logging queries 86 | 87 | You can log all queries with `logQueries`. 88 | 89 | ````php 90 | ld()->logQueries(); // all queries after this call will be logged 91 | ```` 92 | 93 | If you wish to stop logging queries, call `stopLoggingQueries`. 94 | 95 | ````php 96 | ld()->stopLoggingQueries(); // all queries after this call will not be logged anymore 97 | ```` 98 | 99 | Alternatively to manually starting and stopping listening for queries, you can also pass a closure to `logQueries`. Only the queries executed inside the closure will be logged. 100 | 101 | ````php 102 | ld()->logQueries(function() { 103 | $this->mailAllUsers(); // all queries executed in this closure will be logged 104 | }); 105 | 106 | User::get(); // this query will not be logged 107 | ```` 108 | 109 | ## Testing 110 | 111 | ``` bash 112 | composer test 113 | ``` 114 | 115 | ## Changelog 116 | 117 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 118 | 119 | ## Contributing 120 | 121 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 122 | 123 | ## Security 124 | 125 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 126 | 127 | ## Credits 128 | 129 | - [Freek Van der Herten](https://github.com/freekmurze) 130 | - [All Contributors](../../contributors) 131 | 132 | ## License 133 | 134 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 135 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/laravel-log-dumper", 3 | "description": "A function to dump anything to the log", 4 | "keywords": [ 5 | "spatie", 6 | "laravel-log-dumper" 7 | ], 8 | "homepage": "https://github.com/spatie/laravel-log-dumper", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Freek Van der Herten", 13 | "email": "freek@spatie.be", 14 | "homepage": "https://spatie.be", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "illuminate/database": "^8.72|^9.0|^10.0|^11.0|^12.0", 21 | "illuminate/support": "^8.72|^9.0|^10.0|^11.0|^12.0" 22 | }, 23 | "require-dev": { 24 | "ext-json": "*", 25 | "orchestra/testbench": "^6.23|^7.0|^8.0|^9.0|^10.0", 26 | "phpunit/phpunit": "^9.4|^10.5|^11.5.3", 27 | "spatie/phpunit-snapshot-assertions": "^4.0|^5.1" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Spatie\\LogDumper\\": "src" 32 | }, 33 | "files": [ 34 | "src/helpers.php" 35 | ] 36 | }, 37 | "autoload-dev": { 38 | "psr-4": { 39 | "Spatie\\LogDumper\\Tests\\": "tests" 40 | } 41 | }, 42 | "scripts": { 43 | "test": "vendor/bin/phpunit", 44 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage", 45 | "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" 46 | }, 47 | "config": { 48 | "sort-packages": true 49 | }, 50 | "minimum-stability": "dev", 51 | "prefer-stable": true, 52 | "extra": { 53 | "laravel": { 54 | "providers": [ 55 | "Spatie\\LogDumper\\LogDumperServiceProvider" 56 | ] 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/LogDumper.php: -------------------------------------------------------------------------------- 1 | cloner = new VarCloner(); 25 | 26 | $this->dumper = new CliDumper(); 27 | } 28 | 29 | public function debug(...$arguments): self 30 | { 31 | return $this->log('debug', ...$arguments); 32 | } 33 | 34 | public function notice(...$arguments): self 35 | { 36 | return $this->log('notice', ...$arguments); 37 | } 38 | 39 | public function info(...$arguments): self 40 | { 41 | return $this->log('info', ...$arguments); 42 | } 43 | 44 | public function alert(...$arguments): self 45 | { 46 | return $this->log('alert', ...$arguments); 47 | } 48 | 49 | public function warning(...$arguments): self 50 | { 51 | return $this->log('warning', ...$arguments); 52 | } 53 | 54 | public function error(...$arguments): self 55 | { 56 | return $this->log('error', ...$arguments); 57 | } 58 | 59 | public function critical(...$arguments): self 60 | { 61 | return $this->log('critical', ...$arguments); 62 | } 63 | 64 | public function emergency(...$arguments): self 65 | { 66 | return $this->log('emergency', ...$arguments); 67 | } 68 | 69 | public function enable(bool $enabled = true): self 70 | { 71 | $this->enabled = $enabled; 72 | 73 | return $this; 74 | } 75 | 76 | public function disable(): self 77 | { 78 | $this->enabled = false; 79 | 80 | return $this; 81 | } 82 | 83 | public function logQueries($callable = null): self 84 | { 85 | $wasLoggingQueries = $this->listenForQueries; 86 | 87 | $this->startLoggingQueries(); 88 | 89 | if (! is_null($callable)) { 90 | $callable(); 91 | 92 | if (! $wasLoggingQueries) { 93 | $this->stopLoggingQueries(); 94 | } 95 | } 96 | 97 | return $this; 98 | } 99 | 100 | public function startLoggingQueries(): self 101 | { 102 | DB::enableQueryLog(); 103 | 104 | $this->listenForQueries = true; 105 | 106 | if (! $this->queryListenerRegistered) { 107 | DB::listen(function (QueryExecuted $query) { 108 | if ($this->listenForQueries) { 109 | $this->info($query->sql); 110 | } 111 | }); 112 | 113 | $this->queryListenerRegistered = true; 114 | } 115 | 116 | 117 | return $this; 118 | } 119 | 120 | public function stopLoggingQueries(): self 121 | { 122 | DB::disableQueryLog(); 123 | 124 | $this->listenForQueries = false; 125 | 126 | return $this; 127 | } 128 | 129 | public function log(string $method, ...$arguments): self 130 | { 131 | if (! $this->enabled) { 132 | return $this; 133 | } 134 | 135 | foreach ($arguments as $argument) { 136 | $logOutput = $this->convertToString($argument); 137 | 138 | app('log')->$method($logOutput); 139 | } 140 | 141 | return $this; 142 | } 143 | 144 | protected function convertToString($argument): string 145 | { 146 | $clonedArgument = $this->cloner->cloneVar($argument); 147 | 148 | return $this->dumper->dump($clonedArgument, true); 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/LogDumperServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton(LogDumper::class); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | info(...$arguments); 9 | } 10 | } 11 | --------------------------------------------------------------------------------