Skip to content

Commit 8de8cea

Browse files
authored
Merge pull request #22 from freshbitsweb/log-more-details
2 parents 95a97c4 + 4f59100 commit 8de8cea

File tree

6 files changed

+106
-32
lines changed

6 files changed

+106
-32
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.5.0] - 2022-03-02
8+
### Added
9+
- Allow developers to log application details
10+
- Log more details about Git
11+
712
## [1.4.0] - 2022-02-15
813
### Added
914
- Support for Laravel 9.x and PHP 8.1

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Sometimes, we need more than just *stack trace* to debug the issue easily. The t
1212
## Requirements
1313
| PHP | Laravel | Package |
1414
|--------|---------|---------|
15+
| 8.0+ | 9.x | v1.5.0 |
1516
| 8.0+ | 9.x | v1.4.0 |
1617
| 7.3+ | 8.x | v1.3.0 |
1718
| 7.2.5+ | 7.x | v1.2.0 |
@@ -53,7 +54,9 @@ It has following configuration settings:
5354

5455
* (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)
5556

56-
* (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)
57+
* (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.
58+
59+
* (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.
5760

5861
* (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.
5962

config/laravel_log_enhancer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
'log_git_data' => false,
1515

16+
'log_app_details' => false,
17+
1618
// You can specify the inputs from the user that should not be logged
1719
'ignore_input_fields' => ['password', 'confirm_password', 'password_confirmation'],
1820
];

src/LogEnhancer.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Freshbitsweb\LaravelLogEnhancer;
44

5-
use Monolog\Processor\GitProcessor;
65
use Monolog\Processor\MemoryUsageProcessor;
76
use Monolog\Processor\WebProcessor;
87

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

24-
$handler->pushProcessor(new RequestDataProcessor);
25-
2623
if (config('laravel_log_enhancer.log_memory_usage')) {
2724
$handler->pushProcessor(new MemoryUsageProcessor);
2825
}
2926

30-
if (config('laravel_log_enhancer.log_git_data')) {
31-
$handler->pushProcessor(new GitProcessor);
32-
}
27+
$handler->pushProcessor(new RequestDataProcessor);
3328
}
3429
}
3530
}

src/RequestDataProcessor.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,47 @@ public function __invoke($record)
2121
$record['extra']['session'] = session()->all();
2222
}
2323

24+
if (config('laravel_log_enhancer.log_git_data')) {
25+
$record['extra']['git'] = $this->getGitDetails();
26+
}
27+
28+
if (config('laravel_log_enhancer.log_app_details')) {
29+
$record['extra']['Application Details'] = [
30+
'Laravel Version' => app()::VERSION,
31+
'PHP Version' => phpversion(),
32+
'Config Cached' => app()->configurationIsCached() ? 'Yes' : 'No',
33+
'Route Cached' => app()->routesAreCached() ? 'Yes' : 'No',
34+
];
35+
}
36+
2437
return $record;
2538
}
39+
40+
public function getGitDetails()
41+
{
42+
$gitDetails = [];
43+
$lastCommitDetails = `git show -s --format=%B`;
44+
$gitDetails['Last Commit Message'] = preg_filter("/(.*?)\n*/s", '\\1', $lastCommitDetails);
45+
46+
$currentHeadDetails = `git branch -v --no-abbrev`;
47+
if (
48+
$currentHeadDetails &&
49+
preg_match('{^\* (.+?)\s+([a-f0-9]{40})(?:\s|$)}m', $currentHeadDetails, $matches)
50+
) {
51+
$gitDetails['branch'] = $matches[1];
52+
$gitDetails['commit'] = $matches[2];
53+
}
54+
55+
$stagedChanges = `git diff --cached`;
56+
if ($stagedChanges) {
57+
$gitDetails['warning'][] = 'Last commit is dirty. Staged changes have been made since this commit.';
58+
}
59+
60+
$unStagedChanges = `git diff`;
61+
if ($unStagedChanges) {
62+
$gitDetails['warning'][] = 'Last commit is dirty. (Un)staged changes have been made since this commit.';
63+
}
64+
65+
return $gitDetails;
66+
}
2667
}

tests/LogEnhancerTest.php

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,45 @@
44

55
use Freshbitsweb\LaravelLogEnhancer\RequestDataProcessor;
66
use Illuminate\Log\Logger;
7-
use Monolog\Processor\GitProcessor;
7+
use LogicException;
88
use Monolog\Processor\MemoryUsageProcessor;
99
use Monolog\Processor\WebProcessor;
1010

1111
class LogEnhancerTest extends TestCase
1212
{
1313
/** @test */
14-
public function it_adds_request_details_to_logs()
14+
public function it_adds_respective_processors_to_the_log_handler()
1515
{
16+
config(['laravel_log_enhancer.log_memory_usage' => true]);
17+
config(['laravel_log_enhancer.log_request_details' => true]);
1618
$logger = $this->app[Logger::class];
1719

1820
$handlers = $logger->getHandlers();
1921
foreach ($handlers as $handler) {
20-
if (config('laravel_log_enhancer.log_git_data')) {
21-
$this->assertInstanceOf(GitProcessor::class, $handler->popProcessor());
22-
}
22+
$this->assertInstanceOf(RequestDataProcessor::class, $handler->popProcessor());
23+
$this->assertInstanceOf(MemoryUsageProcessor::class, $handler->popProcessor());
24+
$this->assertInstanceOf(WebProcessor::class, $handler->popProcessor());
25+
}
26+
}
2327

24-
if (config('laravel_log_enhancer.log_memory_usage')) {
25-
$this->assertInstanceOf(MemoryUsageProcessor::class, $handler->popProcessor());
26-
}
28+
/** @test */
29+
public function it_does_not_add_processors_to_the_log_handler_when_not_configured()
30+
{
31+
config(['laravel_log_enhancer.log_memory_usage' => false]);
32+
config(['laravel_log_enhancer.log_request_details' => false]);
33+
$logger = $this->app[Logger::class];
2734

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

30-
if (config('laravel_log_enhancer.log_request_details')) {
31-
$this->assertInstanceOf(WebProcessor::class, $handler->popProcessor());
32-
}
39+
$this->expectException(LogicException::class);
40+
$handler->popProcessor();
3341
}
3442
}
3543

3644
/** @test */
37-
public function it_skips_input_details_as_per_the_configuration()
45+
public function it_does_not_add_extra_input_details_to_logs()
3846
{
3947
$record = [];
4048

@@ -47,26 +55,46 @@ public function it_skips_input_details_as_per_the_configuration()
4755
}
4856

4957
/** @test */
50-
public function it_adds_other_details_as_per_the_configuration()
58+
public function it_adds_other_details_to_the_logs_as_per_the_configuration()
5159
{
5260
$record = [];
5361

54-
config(['laravel_log_enhancer.log_request_headers' => rand(0, 1)]);
55-
config(['laravel_log_enhancer.log_session_data' => rand(0, 1)]);
62+
config(['laravel_log_enhancer.log_request_headers' => true]);
63+
config(['laravel_log_enhancer.log_session_data' => true]);
64+
config(['laravel_log_enhancer.log_git_data' => true]);
65+
config(['laravel_log_enhancer.log_app_details' => true]);
5666

5767
$requestDataProcessor = new RequestDataProcessor;
5868
$record = $requestDataProcessor($record);
5969

60-
if (config('laravel_log_enhancer.log_request_headers')) {
61-
$this->assertArrayHasKey('headers', $record['extra']);
62-
} else {
63-
$this->assertArrayNotHasKey('headers', $record['extra']);
64-
}
70+
$this->assertArrayHasKey('headers', $record['extra']);
6571

66-
if (config('laravel_log_enhancer.log_session_data')) {
67-
$this->assertArrayHasKey('session', $record['extra']);
68-
} else {
69-
$this->assertArrayNotHasKey('session', $record['extra']);
70-
}
72+
$this->assertArrayHasKey('session', $record['extra']);
73+
74+
$this->assertArrayHasKey('git', $record['extra']);
75+
76+
$this->assertArrayHasKey('Application Details', $record['extra']);
77+
}
78+
79+
/** @test */
80+
public function it_does_not_add_other_details_to_the_logs_as_per_the_configuration()
81+
{
82+
$record = [];
83+
84+
config(['laravel_log_enhancer.log_request_headers' => false]);
85+
config(['laravel_log_enhancer.log_session_data' => false]);
86+
config(['laravel_log_enhancer.log_git_data' => false]);
87+
config(['laravel_log_enhancer.log_app_details' => false]);
88+
89+
$requestDataProcessor = new RequestDataProcessor;
90+
$record = $requestDataProcessor($record);
91+
92+
$this->assertArrayNotHasKey('headers', $record['extra']);
93+
94+
$this->assertArrayNotHasKey('session', $record['extra']);
95+
96+
$this->assertArrayNotHasKey('git', $record['extra']);
97+
98+
$this->assertArrayNotHasKey('Application Details', $record['extra']);
7199
}
72100
}

0 commit comments

Comments
 (0)