Skip to content

Commit

Permalink
the PHP GELF class has arrived!
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Koopmann committed Aug 23, 2010
0 parents commit 1668c7d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nbproject
116 changes: 116 additions & 0 deletions gelf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

class GELFMessage {

private $graylogHostname;
private $graylogPort;

private $data;

public function __construct($graylogHostname, $graylogPort)
{
if (!is_numeric($graylogPort)) {
throw new Exception("Port must be numeric");
}

$this->graylogHostname = $graylogHostname;
$this->graylogPort = $graylogPort;
}

private function dataParamSet($dataType) {
if (isset($this->data[$dataType]) && strlen($this->data[$dataType]) > 0) {
return true;
}

return false;
}

public function send()
{
// Check if all required parameters are set.
if (!$this->dataParamSet("short_message") || !$this->dataParamSet("host")) {
print_r($this->data);
throw new Exception('Missing required data parameter: "short_message" and "host" are required.');
}

// Convert data array to JSON and GZIP.
$gzippedJsonData = gzcompress(json_encode($this->data));

// Send.
$sock = stream_socket_client('udp://' . gethostbyname($this->graylogHostname) .':' . $this->graylogPort);
fwrite($sock, $gzippedJsonData);
}

// Setters / Getters.

public function setShortMessage($message)
{
$this->data["short_message"] = $message;
}

public function setFullMessage($message)
{
$this->data["full_message"] = $message;
}

public function setHost($host)
{
$this->data["host"] = $host;
}

public function setLevel($level)
{
$this->data["level"] = $level;
}

public function setType($type)
{
$this->data["type"] = $type;
}

public function setFile($file)
{
$this->data["file"] = $file;
}

public function setLine($line)
{
$this->data["line"] = $line;
}

public function getShortMessage()
{
return isset($this->data["short_message"]) ? $this->data["short_message"] : null;
}

public function getFullMessage()
{
return isset($this->data["full_message"]) ? $this->data["full_message"] : null;
}

public function getHost()
{
return isset($this->data["host"]) ? $this->data["host"] : null;
}

public function getLevel()
{
return isset($this->data["level"]) ? $this->data["level"] : null;
}

public function getType()
{
return isset($this->data["type"]) ? $this->data["type"] : null;
}

public function getFile()
{
return isset($this->data["file"]) ? $this->data["file"] : null;
}

public function getLine()
{
return isset($this->data["line"]) ? $this->data["line"] : null;
}

}
13 changes: 13 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require('gelf.php');

$gelf = new GELFMessage('localhost', 12201);

$gelf->setShortMessage('something is broken.');
$gelf->setFullMessage('stacktrace here');
$gelf->setHost('somehost');
$gelf->setLevel(2);
$gelf->setFile('/var/www/example.php');
$gelf->setLine(1337);
$gelf->send();

0 comments on commit 1668c7d

Please sign in to comment.