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']); } } }
<?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']); } } }); }
// Open HBase and MySQL connection if (DEBUG) { echo "Open connections\n"; } $transport->open(); if (MYSQL) { $con = mysql_connect("{$mysqlHost}:{$mysqlPort}", "{$mysqlUser}", "{$mysqlPass}"); mysql_select_db($mysqlDbName, $con); if (!$con) { die('Could not connect to MySQL: ' . mysql_error()); } } // GrayLog if (GRAYLOG) { $graylogTransport = new Gelf\Transport\UdpTransport($graylog_host, $graylog_port, Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN); $publisher = new Gelf\Publisher(); $publisher->addTransport($graylogTransport); } // Get last CID from MySQL, this counter will be used on last access if (MYSQL) { $cid = getLastCID($sensorid, $con); } else { $cid = 1; } // Get last seen event $startrow = getNextHBaseRow(); if (DEBUG) { echo "Start row: {$startrow} \n"; } $lastHBaseID = 0; // Get HBase pointer
|-------------------------------------------------------------------------- | Bind Important Interfaces |-------------------------------------------------------------------------- | | Next, we need to bind some important interfaces into the container so | we will be able to resolve them when needed. The kernels serve the | incoming requests to this application from both the web and CLI. | */ $app->singleton(Illuminate\Contracts\Http\Kernel::class, App\Http\Kernel::class); $app->singleton(Illuminate\Contracts\Console\Kernel::class, App\Console\Kernel::class); $app->singleton(Illuminate\Contracts\Debug\ExceptionHandler::class, App\Exceptions\Handler::class); /* |-------------------------------------------------------------------------- | Return The Application |-------------------------------------------------------------------------- | | This script returns the application instance. The instance is given to | the calling script so we can separate the building of the instances | from the actual running of the application and sending responses. | */ if (getenv('GRAYLOG')) { $app->configureMonologUsing(function ($monolog) { $transport = new \Gelf\Transport\UdpTransport(getenv('GRAYLOG_IP'), getenv('GRAYLOG_UDP_PORT'), \Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN); $publisher = new \Gelf\Publisher(); $publisher->addTransport($transport); $monolog->pushHandler(new \Monolog\Handler\GelfHandler($publisher)); }); } return $app;
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; } }