Skip to content

Commit

Permalink
Merge pull request #22 from freshbitsweb/log-more-details
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravmak authored Mar 2, 2022
2 parents 95a97c4 + 4f59100 commit 8de8cea
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 32 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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
- Allow developers to log application details
- Log more details about Git

## [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;
}
}
78 changes: 53 additions & 25 deletions tests/LogEnhancerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,45 @@

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

class LogEnhancerTest extends TestCase
{
/** @test */
public function it_adds_request_details_to_logs()
public function it_adds_respective_processors_to_the_log_handler()
{
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());
$this->assertInstanceOf(MemoryUsageProcessor::class, $handler->popProcessor());
$this->assertInstanceOf(WebProcessor::class, $handler->popProcessor());
}
}

if (config('laravel_log_enhancer.log_memory_usage')) {
$this->assertInstanceOf(MemoryUsageProcessor::class, $handler->popProcessor());
}
/** @test */
public function it_does_not_add_processors_to_the_log_handler_when_not_configured()
{
config(['laravel_log_enhancer.log_memory_usage' => false]);
config(['laravel_log_enhancer.log_request_details' => false]);
$logger = $this->app[Logger::class];

$handlers = $logger->getHandlers();
foreach ($handlers as $handler) {
$this->assertInstanceOf(RequestDataProcessor::class, $handler->popProcessor());

if (config('laravel_log_enhancer.log_request_details')) {
$this->assertInstanceOf(WebProcessor::class, $handler->popProcessor());
}
$this->expectException(LogicException::class);
$handler->popProcessor();
}
}

/** @test */
public function it_skips_input_details_as_per_the_configuration()
public function it_does_not_add_extra_input_details_to_logs()
{
$record = [];

Expand All @@ -47,26 +55,46 @@ public function it_skips_input_details_as_per_the_configuration()
}

/** @test */
public function it_adds_other_details_as_per_the_configuration()
public function it_adds_other_details_to_the_logs_as_per_the_configuration()
{
$record = [];

config(['laravel_log_enhancer.log_request_headers' => rand(0, 1)]);
config(['laravel_log_enhancer.log_session_data' => rand(0, 1)]);
config(['laravel_log_enhancer.log_request_headers' => true]);
config(['laravel_log_enhancer.log_session_data' => true]);
config(['laravel_log_enhancer.log_git_data' => true]);
config(['laravel_log_enhancer.log_app_details' => true]);

$requestDataProcessor = new RequestDataProcessor;
$record = $requestDataProcessor($record);

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

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

$this->assertArrayHasKey('git', $record['extra']);

$this->assertArrayHasKey('Application Details', $record['extra']);
}

/** @test */
public function it_does_not_add_other_details_to_the_logs_as_per_the_configuration()
{
$record = [];

config(['laravel_log_enhancer.log_request_headers' => false]);
config(['laravel_log_enhancer.log_session_data' => false]);
config(['laravel_log_enhancer.log_git_data' => false]);
config(['laravel_log_enhancer.log_app_details' => false]);

$requestDataProcessor = new RequestDataProcessor;
$record = $requestDataProcessor($record);

$this->assertArrayNotHasKey('headers', $record['extra']);

$this->assertArrayNotHasKey('session', $record['extra']);

$this->assertArrayNotHasKey('git', $record['extra']);

$this->assertArrayNotHasKey('Application Details', $record['extra']);
}
}

0 comments on commit 8de8cea

Please sign in to comment.