Skip to content

Commit b8fe7fd

Browse files
committed
Merge branch 'release/v1.1.0'
2 parents 8554746 + 45117e9 commit b8fe7fd

File tree

6 files changed

+133
-12
lines changed

6 files changed

+133
-12
lines changed

CHANGELOG.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# Changelog
22

3-
## 1.0.0 - 2018-04-04
3+
## [1.1.0] - 2018-04-19
4+
### Added
5+
- Log files (file name and size)
6+
- Log memory usage
7+
- Log peak memory usage
8+
- Log current git branch and commit
9+
### Changed
10+
- Date format config parametr name (dateFormat -> date_foramt)
11+
12+
## [1.0.0] - 2018-04-04
413
- Initial release

README.md

+33-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Total Downloads](https://poser.pugx.org/chelout/laravel-http-logger/downloads)](https://packagist.org/packages/chelout/laravel-http-logger)
44
[![License](https://poser.pugx.org/chelout/laravel-http-logger/license)](https://packagist.org/packages/chelout/laravel-http-logger)
55

6-
This package provides a middleware to log incoming http requests data (body data, headers and session data). It utilizes [Laravel 5.6 logging services](https://laravel.com/docs/5.6/logging) functionality.
6+
This package provides a middleware to log incoming http requests data (body data, files, headers and session data). It utilizes [Laravel 5.6 logging servises](https://laravel.com/docs/5.6/logging) functionality.
77
This package might be useful to log user requests to public apis.
88

99
## Installation
@@ -46,7 +46,25 @@ return [
4646
* For for details see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
4747
* and https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
4848
*/
49-
'dateFormat' => null, // "Y-m-d\TH:i:sP"
49+
'date_format' => null, // "Y-m-d\TH:i:sP"
50+
51+
/*
52+
* Log current memory usage
53+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryUsageProcessor.php
54+
*/
55+
'memory_usage' => true,
56+
57+
/*
58+
* Log peak memory usage
59+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryPeakUsageProcessor.php
60+
*/
61+
'memory_peak_usage' => true,
62+
63+
/*
64+
* Log current git branch and commit
65+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/GitProcessor.php
66+
*/
67+
'git' => true,
5068

5169
/*
5270
* false - don't log body fields
@@ -61,6 +79,19 @@ return [
6179
'except' => [],
6280
],
6381

82+
/*
83+
* false - don't log uploaded files
84+
* ['only'] - log files only
85+
* ['except'] - don't log files
86+
*
87+
* If ['only'] is set, ['except'] parametr will be omitted
88+
*/
89+
// 'files' => false,
90+
'files' => [
91+
'only' => [],
92+
'except' => [],
93+
],
94+
6495
/*
6596
* false - don't log headers
6697
* ['only'] - log headers only
@@ -152,7 +183,6 @@ return [
152183

153184
### Todo
154185
- tests
155-
- log file uploads
156186
- log git data?
157187
- log memory usage?
158188

config/http-logger.php

+36-5
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,35 @@
1212

1313
/*
1414
* Log message format.
15-
* For for details see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
16-
* and https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
15+
* @see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
16+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
1717
*/
1818
'format' => "[%datetime%] %extra.method% %extra.url% from %extra.ips% %context%\n",
1919

2020
/*
2121
* Log message datetime format.
22-
* For for details see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
23-
* and https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
22+
* @see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
23+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
2424
*/
25-
'dateFormat' => null, // "Y-m-d\TH:i:sP"
25+
'date_format' => null, // "Y-m-d\TH:i:sP"
26+
27+
/*
28+
* Log current memory usage
29+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryUsageProcessor.php
30+
*/
31+
'memory_usage' => true,
32+
33+
/*
34+
* Log peak memory usage
35+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryPeakUsageProcessor.php
36+
*/
37+
'memory_peak_usage' => true,
38+
39+
/*
40+
* Log current git branch and commit
41+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/GitProcessor.php
42+
*/
43+
'git' => true,
2644

2745
/*
2846
* false - don't log body fields
@@ -37,6 +55,19 @@
3755
'except' => [],
3856
],
3957

58+
/*
59+
* false - don't log uploaded files
60+
* ['only'] - log files only
61+
* ['except'] - don't log files
62+
*
63+
* If ['only'] is set, ['except'] parametr will be omitted
64+
*/
65+
// 'files' => false,
66+
'files' => [
67+
'only' => [],
68+
'except' => [],
69+
],
70+
4071
/*
4172
* false - don't log headers
4273
* ['only'] - log headers only

src/Loggers/HttpLogger.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use Monolog\Formatter\LineFormatter;
77
use Monolog\Handler\RotatingFileHandler;
88
use Monolog\Logger;
9+
use Monolog\Processor\GitProcessor;
10+
use Monolog\Processor\MemoryPeakUsageProcessor;
11+
use Monolog\Processor\MemoryUsageProcessor;
912

1013
class HttpLogger
1114
{
@@ -18,12 +21,24 @@ public function __invoke(array $config)
1821
{
1922
$logger = new Logger('http-logger');
2023

24+
if (config('http-logger.memory_usage')) {
25+
$logger->pushProcessor(new MemoryUsageProcessor);
26+
}
27+
28+
if (config('http-logger.memory_peak_usage')) {
29+
$logger->pushProcessor(new MemoryPeakUsageProcessor);
30+
}
31+
32+
if (config('http-logger.git')) {
33+
$logger->pushProcessor(new GitProcessor);
34+
}
35+
2136
return $logger->pushProcessor(new RequestDataProcessor)
2237
->pushHandler(
2338
(new RotatingFileHandler(
2439
config('http-logger.path'),
2540
config('http-logger.max_files')
26-
))->setFormatter(new LineFormatter(config('http-logger.format'), config('http-logger.dateFormat'), true, true))
41+
))->setFormatter(new LineFormatter(config('http-logger.format'), config('http-logger.date_format'), true, true))
2742
);
2843
}
2944
}

src/Loggers/MonologCustomizer.php

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Chelout\HttpLogger\Loggers;
44

55
use Chelout\HttpLogger\Processors\RequestDataProcessor;
6+
use Monolog\Processor\GitProcessor;
7+
use Monolog\Processor\MemoryPeakUsageProcessor;
8+
use Monolog\Processor\MemoryUsageProcessor;
69

710
class MonologCustomizer
811
{
@@ -13,6 +16,18 @@ class MonologCustomizer
1316
*/
1417
public function __invoke($logger)
1518
{
19+
if (config('http-logger.memory_usage')) {
20+
$logger->pushProcessor(new MemoryUsageProcessor);
21+
}
22+
23+
if (config('http-logger.memory_peak_usage')) {
24+
$logger->pushProcessor(new MemoryPeakUsageProcessor);
25+
}
26+
27+
if (config('http-logger.git')) {
28+
$logger->pushProcessor(new GitProcessor);
29+
}
30+
1631
return $logger->pushProcessor(new RequestDataProcessor);
1732
}
1833
}

src/Processors/RequestDataProcessor.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function __invoke($record): array
1414
return array_merge_recursive($record, [
1515
'context' => array_filter([
1616
'data' => $this->processContext('data'),
17+
'files' => $this->processContext('files'),
1718
'headers' => $this->processContext('headers'),
1819
'session' => $this->processContext('session'),
1920
]),
@@ -64,13 +65,33 @@ protected function processContext(string $name): array
6465
}
6566

6667
/**
67-
* Get request body data.
68+
* Get request body data except files.
6869
*
6970
* @return array
7071
*/
7172
protected function getData(): array
7273
{
73-
return request()->all();
74+
return request()->except(
75+
request()->files->keys()
76+
);
77+
}
78+
79+
/**
80+
* Get files.
81+
*
82+
* @return array
83+
*/
84+
protected function getFiles(): array
85+
{
86+
return collect(request()->files->all())
87+
->flatten()
88+
->map(function ($file) {
89+
return [
90+
'name' => $file->getClientOriginalName(),
91+
'size' => $file->getClientSize(),
92+
];
93+
})
94+
->toArray();
7495
}
7596

7697
/**

0 commit comments

Comments
 (0)