Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow http context processing to be overwritten. #49

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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