protected function log($data)
 {
     foreach ($this->urls as $url) {
         $url = trim($url);
         $url = parse_url($url);
         switch ($url['scheme']) {
             case 'json-path':
                 $path = __DIR__ . '/../../' . $url['host'] . $url['path'];
                 $path = str_replace('{date}', date('Ymd'), $path);
                 $json = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
                 //$path = $this->getRootPath() . '/app/logs/requests/' . date('Ymd') . '/';
                 if (!file_exists($path)) {
                     mkdir($path, 0777, true);
                 }
                 $filename = $path . '/' . date('Ymd-His') . '-' . $data['request-id'] . '.json';
                 file_put_contents($filename, $json . "\n");
                 break;
             case 'gelf-udp':
                 $transport = new \Gelf\Transport\UdpTransport($url['host'], $url['port'], \Gelf\Transport\UdpTransport::CHUNK_SIZE_WAN);
                 $gelfData = $data;
                 $publisher = new \Gelf\Publisher();
                 $publisher->addTransport($transport);
                 $message = new \Gelf\Message();
                 $message->setShortMessage("Request")->setLevel(\Psr\Log\LogLevel::DEBUG)->setFullMessage("")->setFacility("")->setHost($data['host']);
                 $gelfData['host'] = null;
                 $gelfData['datetime'] = null;
                 array_filter($gelfData);
                 foreach ($gelfData as $key => $value) {
                     $message->setAdditional($key, $value);
                 }
                 $res = $publisher->publish($message);
                 break;
             default:
                 throw new RuntimeException('Unsupported request_log url scheme: ' . $url['scheme']);
         }
     }
 }
Exemple #2
0
<?php

require_once __DIR__ . DIRECTORY_SEPARATOR . '../../vendor/autoload.php';
$transport = new Gelf\Transport\TcpTransport("127.0.0.1", 12201);
$publisher = new Gelf\Publisher();
$publisher->addTransport($transport);
$logger = new Gelf\Logger($publisher);
$logger->debug("A debug message.");
$logger->alert("An alert message", ['structure' => ['data' => [0, 1]]]);
try {
    throw new Exception("Test exception");
} catch (Exception $e) {
    $logger->emergency("Exception example", array('exception' => $e));
}
$message = new Gelf\Message();
$message->setShortMessage("Structured message")->setLevel(\Psr\Log\LogLevel::ALERT)->setFullMessage("There was a foo in bar")->setFacility("example-facility")->setAdditional('foo', 'bar')->setAdditional('bar', 'baz');
$publisher->publish($message);
$logger->warning("A warning message.", ['structure' => ['with' => ['several' => 'nested', 'levels']]]);
$logger->info(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . "bacon.txt"));
 public function configureRequestLogger()
 {
     if (!isset($this['parameters']['request_log'])) {
         return;
     }
     $this->after(function (Request $request, Response $response) {
         $data = ['datetime' => date('Y-m-d H:i:s'), 'method' => $request->getMethod(), 'scheme' => $request->getScheme(), 'host' => $request->getHttpHost(), 'uri' => $request->getRequestUri(), 'route' => $request->get('_route')];
         if (isset($this['current_user'])) {
             $data['username'] = $this['current_user']->getName();
         }
         $data['address'] = $request->getClientIp();
         $data['session-id'] = $request->getSession()->getId();
         $data['agent'] = $request->getSession()->getId();
         if ($request->headers->has('User-Agent')) {
             $data['agent'] = $request->headers->get('User-Agent');
         }
         if ($request->headers->has('referer')) {
             $data['referer'] = $request->headers->get('referer');
         }
         // response details
         $data['status'] = $response->getStatusCode();
         if ($response->headers->has('Content-Type')) {
             $data['content-type'] = $response->headers->get('content-type');
         }
         $urls = $this['parameters']['request_log'];
         foreach (explode(',', $urls) as $url) {
             $url = trim($url);
             $url = parse_url($url);
             //print_r($url);
             switch ($url['scheme']) {
                 case 'json-path':
                     $path = '/' . $url['scheme'] . $url['path'];
                     $json = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
                     $path = $this->getRootPath() . '/app/logs/requests/' . date('Ymd') . '/';
                     if (!file_exists($path)) {
                         mkdir($path, 0777, true);
                     }
                     $filename = $path . '/' . date('Ymd-His') . '-' . sha1(rand()) . '.json';
                     file_put_contents($filename, $json . "\n");
                     break;
                 case 'gelf-udp':
                     $transport = new \Gelf\Transport\UdpTransport($url['host'], $url['port'], \Gelf\Transport\UdpTransport::CHUNK_SIZE_WAN);
                     $gelfData = $data;
                     $publisher = new \Gelf\Publisher();
                     $publisher->addTransport($transport);
                     $message = new \Gelf\Message();
                     $message->setShortMessage("Request")->setLevel(\Psr\Log\LogLevel::DEBUG)->setFullMessage("")->setFacility("")->setHost($data['host']);
                     $gelfData['host'] = null;
                     $gelfData['datetime'] = null;
                     array_filter($gelfData);
                     foreach ($gelfData as $key => $value) {
                         $message->setAdditional($key, $value);
                     }
                     $res = $publisher->publish($message);
                     break;
                 default:
                     throw new RuntimeException('Unsupported request_log url scheme: ' . $url['scheme']);
             }
         }
     });
 }
Exemple #4
0
function saveInventory($rowresult)
{
    global $publisher;
    if (DEBUG) {
        echo "Save inventory information into GrayLog\n";
    }
    $values = $rowresult[0]->columns;
    //$title              = $values["info:title"]->value;
    $ipaddr = $values["info:ip"]->value;
    $oSystem = $values["info:OS"]->value;
    //$location = geoip_record_by_name($ipaddr);
    $ip_name = gethostbyaddr($ipaddr);
    $message = new Gelf\Message();
    $message->setShortMessage("Inventory information for " . $ipaddr)->setAdditional("ip", $ipaddr)->setAdditional("dns_reverse", $ip_name)->setAdditional("operating_system", $oSystem)->setAdditional("priority", "INFO")->setLevel(\Psr\Log\LogLevel::NOTICE);
    $publisher->publish($message);
}
Exemple #5
0
<?php

/*
 * This file is part of the php-gelf package.
 *
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require_once __DIR__ . '/../vendor/autoload.php';
// We need a transport - UDP via port 12201 is standard.
$transport = new Gelf\Transport\UdpTransport("127.0.0.1", 12201, Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN);
// While the UDP transport is itself a publisher, we wrap it in a real Publisher for convenience
// A publisher allows for message validation before transmission, and it calso supports to send messages
// to multiple backends at once
$publisher = new Gelf\Publisher();
$publisher->addTransport($transport);
// Now we can create custom messages and publish them
$message = new Gelf\Message();
$message->setShortMessage("Foobar!")->setLevel(\Psr\Log\LogLevel::ALERT)->setFullMessage("There was a foo in bar")->setFacility("example-facility");
$publisher->publish($message);
// The implementation of PSR-3 is encapsulated in the Logger-class.
// It provides high-level logging methods, such as alert(), info(), etc.
$logger = new Gelf\Logger($publisher, "example-facility");
// Now we can log...
$logger->alert("Foobaz!");
Exemple #6
-1
 public function log($err)
 {
     $config = Registry::get('config');
     // We need a transport - UDP via port 12201 is standard.
     $transport = new \Gelf\Transport\UdpTransport($config->error->logger->gelf->ip, $config->error->logger->gelf->port, \Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN);
     // While the UDP transport is itself a publisher, we wrap it in a real Publisher for convenience
     // A publisher allows for message validation before transmission, and it calso supports to send messages
     // to multiple backends at once
     $publisher = new \Gelf\Publisher();
     $publisher->addTransport($transport);
     $fullMessage = \Nf\Front\Response\Cli::displayErrorHelper($err);
     // Now we can create custom messages and publish them
     $message = new \Gelf\Message();
     $message->setShortMessage(Handler::recursiveArrayToString($err['message']))->setLevel(\Psr\Log\LogLevel::ERROR)->setFile($err['file'])->setLine($err['line'])->setFullMessage($fullMessage);
     if (php_sapi_name() == 'cli') {
         global $argv;
         $message->setAdditional('url', 'su ' . $_SERVER['LOGNAME'] . ' -c "php ' . Registry::get('applicationPath') . '/html/' . implode(' ', $argv) . '"');
     } else {
         $message->setAdditional('url', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
     }
     if (isset($config->error->logger->additionals)) {
         foreach ($config->error->logger->additionals as $additionalName => $additionalValue) {
             $message->setAdditional($additionalName, $additionalValue);
         }
     }
     if ($publisher->publish($message)) {
         return true;
     } else {
         return false;
     }
 }