This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/log_logstash/main.php
thoughever f15407bc75 X-Real-IP support and Varnish PURGE config options
X-Real-IP for core functionality

Global config define REVERSE_PROXY_X_HEADERS

Config host and port for varnish PURGE

config option to specify PURGE protocol

exception in curl purge now shows error code

ipv6 x-real-ip addresses are now validated properly

X-Forwarded-Proto enabled by define
2022-01-20 22:25:22 +00:00

57 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
class LogLogstash extends Extension
{
public function onLog(LogEvent $event)
{
global $user;
try {
$data = [
"@type" => "shimmie",
"@message" => $event->message,
"@fields" => [
"username" => ($user && $user->name) ? $user->name : "Anonymous",
"section" => $event->section,
"priority" => $event->priority,
"time" => $event->time,
],
#"@request" => $_SERVER,
"@request" => [
"UID" => get_request_id(),
"REMOTE_ADDR" => get_real_ip(),
],
];
$this->send_data($data);
} catch (Exception $e) {
// we can't log that logging is broken
}
}
private function send_data($data)
{
global $config;
$host = $config->get_string("log_logstash_host");
if (!$host) {
return;
}
try {
$parts = explode(":", $host);
$host = $parts[0];
$port = (int)$parts[1];
$fp = fsockopen("udp://$host", $port, $errno, $errstr);
if (! $fp) {
return;
}
fwrite($fp, json_encode($data));
fclose($fp);
} catch (Exception $e) {
// we can't log that logging is broken
}
}
}