Skip to content

Commit

Permalink
Allow http context processing to be overwritten. (#49)
Browse files Browse the repository at this point in the history
* Allowed http context to be disabled

* Allowed http context to be disabled
  • Loading branch information
LauJosefsen authored Feb 12, 2024
1 parent 3a9b665 commit ba570f4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 79 deletions.
79 changes: 0 additions & 79 deletions src/FilebeatContextProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Cego;

use Throwable;
use UAParser\Parser;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

Expand All @@ -15,13 +14,6 @@ public function __invoke(LogRecord $record): LogRecord
{
$record->extra = array_merge($record->extra, $this->extras);

if (isset($_SERVER['REQUEST_METHOD'])) {
$record->extra = array_merge($record->extra, ['http' => self::httpExtras()]);
$record->extra = array_merge($record->extra, ['url' => self::urlExtras()]);
$record->extra = array_merge($record->extra, ['user_agent' => self::userAgentExtras()]);
$record->extra = array_merge($record->extra, ['client' => self::clientExtras()]);
}

$record->extra = array_merge($record->extra, ['php' => self::phpExtras()]);

if (isset($record->context['exception']) && $record->context['exception'] instanceof Throwable) {
Expand Down Expand Up @@ -68,75 +60,4 @@ public static function phpExtras(): array
'argv_string' => $_SERVER['argv'] ?? null ? implode(' ', $_SERVER['argv']) : null,
];
}

public static function clientExtras(): array
{
return [
'ip' => $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? null,
'address' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null,
'geo' => [
'country_iso_code' => $_SERVER['HTTP_CF_IPCOUNTRY'] ?? null,
],
];
}

public static function httpExtras(): array
{
return [
'request' => [
'id' => $_SERVER['HTTP_CF_RAY'] ?? null,
'method' => $_SERVER['REQUEST_METHOD'] ?? null,
],
];
}

public static function urlExtras(): array
{
return [
'path' => $_SERVER['REQUEST_URI'] ?? null,
'method' => $_SERVER['REQUEST_METHOD'] ?? null,
'referer' => $_SERVER['HTTP_REFERER'] ?? null,
'domain' => $_SERVER['HTTP_HOST'] ?? null,
];
}

private static function userAgentExtras(): array|null
{
$original = $_SERVER['HTTP_USER_AGENT'] ?? null;

if ( ! isset($original)) {
return null;
}

try {
$parser = Parser::create();
$result = $parser->parse($original);
} catch (Throwable $throwable) {
return [
'original' => $original,
'error' => [
'message' => $throwable->getMessage(),
'stack_trace' => $throwable->getTraceAsString(),
],
];
}

return [
'original' => $original,
'browser' => [
'name' => $result->ua->family,
'major' => $result->ua->major,
'minor' => $result->ua->minor,
'patch' => $result->ua->patch,
],
'os' => [
'name' => $result->os->family,
'major' => $result->os->major,
'minor' => $result->os->minor,
'patch' => $result->os->patch,
'patch_minor' => $result->os->patchMinor,
],
'device.name' => $result->device->family,
];
}
}
94 changes: 94 additions & 0 deletions src/FilebeatHttpContextProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Cego;

use Throwable;
use UAParser\Parser;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

class FilebeatHttpContextProcessor implements ProcessorInterface
{
public function __invoke(LogRecord $record): LogRecord
{
if (isset($_SERVER['REQUEST_METHOD'])) {
$record->extra = array_merge($record->extra, ['http' => self::httpExtras()]);
$record->extra = array_merge($record->extra, ['url' => self::urlExtras()]);
$record->extra = array_merge($record->extra, ['user_agent' => self::userAgentExtras()]);
$record->extra = array_merge($record->extra, ['client' => self::clientExtras()]);
}

return $record;
}

public static function clientExtras(): array
{
return [
'ip' => $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? null,
'address' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null,
'geo' => [
'country_iso_code' => $_SERVER['HTTP_CF_IPCOUNTRY'] ?? null,
],
];
}

public static function httpExtras(): array
{
return [
'request' => [
'id' => $_SERVER['HTTP_CF_RAY'] ?? null,
'method' => $_SERVER['REQUEST_METHOD'] ?? null,
],
];
}

public static function urlExtras(): array
{
return [
'path' => $_SERVER['REQUEST_URI'] ?? null,
'method' => $_SERVER['REQUEST_METHOD'] ?? null,
'referer' => $_SERVER['HTTP_REFERER'] ?? null,
'domain' => $_SERVER['HTTP_HOST'] ?? null,
];
}

private static function userAgentExtras(): array|null
{
$original = $_SERVER['HTTP_USER_AGENT'] ?? null;

if ( ! isset($original)) {
return null;
}

try {
$parser = Parser::create();
$result = $parser->parse($original);
} catch (Throwable $throwable) {
return [
'original' => $original,
'error' => [
'message' => $throwable->getMessage(),
'stack_trace' => $throwable->getTraceAsString(),
],
];
}

return [
'original' => $original,
'browser' => [
'name' => $result->ua->family,
'major' => $result->ua->major,
'minor' => $result->ua->minor,
'patch' => $result->ua->patch,
],
'os' => [
'name' => $result->os->family,
'major' => $result->os->major,
'minor' => $result->os->minor,
'patch' => $result->os->patch,
'patch_minor' => $result->os->patchMinor,
],
'device.name' => $result->device->family,
];
}
}
7 changes: 7 additions & 0 deletions src/FilebeatLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public function __invoke(array $config): Logger

$logger->setHandlers([$handler]);
$logger->pushProcessor(new FilebeatContextProcessor($extras));

if (isset($config['httpContextProcessor'])) {
$logger->pushProcessor($config['httpContextProcessor']);
} else {
$logger->pushProcessor(new FilebeatHttpContextProcessor());
}

$logger->setExceptionHandler(function (Throwable $throwable): void {
error_log("$throwable");
});
Expand Down

0 comments on commit ba570f4

Please sign in to comment.