Skip to content
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.5.0] - 2022-03-02
### Added
- Remove GitProcessor
- Add More details on Git
- Add New Application Details

## [1.4.0] - 2022-02-15
### Added
- Support for Laravel 9.x and PHP 8.1
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Sometimes, we need more than just *stack trace* to debug the issue easily. The t
## Requirements
| PHP | Laravel | Package |
|--------|---------|---------|
| 8.0+ | 9.x | v1.5.0 |
| 8.0+ | 9.x | v1.4.0 |
| 7.3+ | 8.x | v1.3.0 |
| 7.2.5+ | 7.x | v1.2.0 |
Expand Down Expand Up @@ -53,7 +54,9 @@ It has following configuration settings:

* (bool) log_memory_usage => Set to *true* if you wish to log memory usage [Reference](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryUsageProcessor.php)

* (bool) log_git_data => Set to *true* if you wish to log git branch and commit details [Reference](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/GitProcessor.php)
* (bool) log_git_data => Set to *true* if you wish to log git branch name, last commit message, last commit id, staged or (un)staged changes.

* (bool) log_app_details => Set to *true* if you wish to log application data. It will include Laravel Version, PHP Version, Config Cached and Route Cached details.

* (array) ignore_input_fields => If input data is being sent, you can specify the inputs from the user that should not be logged. for example, password,cc number, etc.

Expand Down
2 changes: 2 additions & 0 deletions config/laravel_log_enhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

'log_git_data' => false,

'log_app_details' => false,

// You can specify the inputs from the user that should not be logged
'ignore_input_fields' => ['password', 'confirm_password', 'password_confirmation'],
];
7 changes: 1 addition & 6 deletions src/LogEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Freshbitsweb\LaravelLogEnhancer;

use Monolog\Processor\GitProcessor;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\WebProcessor;

Expand All @@ -21,15 +20,11 @@ public function __invoke($logger)
$handler->pushProcessor(new WebProcessor);
}

$handler->pushProcessor(new RequestDataProcessor);

if (config('laravel_log_enhancer.log_memory_usage')) {
$handler->pushProcessor(new MemoryUsageProcessor);
}

if (config('laravel_log_enhancer.log_git_data')) {
$handler->pushProcessor(new GitProcessor);
}
$handler->pushProcessor(new RequestDataProcessor);
}
}
}
41 changes: 41 additions & 0 deletions src/RequestDataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,47 @@ public function __invoke($record)
$record['extra']['session'] = session()->all();
}

if (config('laravel_log_enhancer.log_git_data')) {
$record['extra']['git'] = $this->getGitDetails();
}

if (config('laravel_log_enhancer.log_app_details')) {
$record['extra']['Application Details'] = [
'Laravel Version' => app()::VERSION,
'PHP Version' => phpversion(),
'Config Cached' => app()->configurationIsCached() ? 'Yes' : 'No',
'Route Cached' => app()->routesAreCached() ? 'Yes' : 'No',
];
}

return $record;
}

public function getGitDetails()
{
$gitDetails = [];
$lastCommitDetails = `git show -s --format=%B`;
$gitDetails['Last Commit Message'] = preg_filter("/(.*?)\n*/s", '\\1', $lastCommitDetails);

$currentHeadDetails = `git branch -v --no-abbrev`;
if (
$currentHeadDetails &&
preg_match('{^\* (.+?)\s+([a-f0-9]{40})(?:\s|$)}m', $currentHeadDetails, $matches)
) {
$gitDetails['branch'] = $matches[1];
$gitDetails['commit'] = $matches[2];
}

$stagedChanges = `git diff --cached`;
if ($stagedChanges) {
$gitDetails['warning'][] = 'Last commit is dirty. Staged changes have been made since this commit.';
}

$unStagedChanges = `git diff`;
if ($unStagedChanges) {
$gitDetails['warning'][] = 'Last commit is dirty. (Un)staged changes have been made since this commit.';
}

return $gitDetails;
}
}
21 changes: 15 additions & 6 deletions tests/LogEnhancerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Freshbitsweb\LaravelLogEnhancer\RequestDataProcessor;
use Illuminate\Log\Logger;
use Monolog\Processor\GitProcessor;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\WebProcessor;

Expand All @@ -13,20 +12,18 @@ class LogEnhancerTest extends TestCase
/** @test */
public function it_adds_request_details_to_logs()
{
config(['laravel_log_enhancer.log_memory_usage' => true]);
config(['laravel_log_enhancer.log_request_details' => true]);
$logger = $this->app[Logger::class];

$handlers = $logger->getHandlers();
foreach ($handlers as $handler) {
if (config('laravel_log_enhancer.log_git_data')) {
$this->assertInstanceOf(GitProcessor::class, $handler->popProcessor());
}
$this->assertInstanceOf(RequestDataProcessor::class, $handler->popProcessor());

if (config('laravel_log_enhancer.log_memory_usage')) {
$this->assertInstanceOf(MemoryUsageProcessor::class, $handler->popProcessor());
}

$this->assertInstanceOf(RequestDataProcessor::class, $handler->popProcessor());

if (config('laravel_log_enhancer.log_request_details')) {
$this->assertInstanceOf(WebProcessor::class, $handler->popProcessor());
}
Expand Down Expand Up @@ -68,5 +65,17 @@ public function it_adds_other_details_as_per_the_configuration()
} else {
$this->assertArrayNotHasKey('session', $record['extra']);
}

if (config('laravel_log_enhancer.log_git_data')) {
$this->assertArrayHasKey('git', $record['extra']);
} else {
$this->assertArrayNotHasKey('git', $record['extra']);
}

if (config('laravel_log_enhancer.log_app_details')) {
$this->assertArrayHasKey('Application Details', $record['extra']);
} else {
$this->assertArrayNotHasKey('Application Details', $record['extra']);
}
}
}