Created
May 28, 2014 21:07
-
-
Save GaryRogers/01bf11fc172a0ae31add to your computer and use it in GitHub Desktop.
Monolog Oracle Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Monolog\Handler; | |
use Monolog\Logger; | |
use Monolog\Formatter\LineFormatter; | |
// https://github.com/Seldaek/monolog/blob/master/doc/extending.md | |
class OracleHandler extends AbstractProcessingHandler { | |
private $oracleInstance; | |
private $oracleUser; | |
private $oraclePassword; | |
private $oracleConnection; | |
public function __construct($instance, $user, $password, $level = Logger::DEBUG, $bubble = true) { | |
$this->oracleInstance = $instance; | |
$this->oracleUser = $user; | |
$this->oraclePassword = $password; | |
$this->oracleConnection = oci_pconnect( | |
$this->oracleUser, | |
$this->oraclePassword, | |
$this->oracleInstance | |
); | |
parent::__construct($level, $bubble); | |
} | |
protected function write(array $record) { | |
if ( ! $this->oracleConnection ) { | |
$e = oci_error(); | |
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); | |
} | |
$insertStatement = ' | |
INSERT INTO | |
MONOLOG ( | |
LOG_TIME, | |
LOG_LEVEL, | |
LOG_HOSTNAME, | |
LOG_USER, | |
LOG_IP, | |
LOG_CONTEXT, | |
LOG_MESSAGE | |
) VALUES ( | |
:time_bv, | |
:level_bv, | |
:hostname_bv, | |
:user_bv, | |
:ip_bv, | |
:context_bv, | |
:message_bv | |
)'; | |
if (isset($record['datetime'])) { | |
$datetime = $record['datetime']->format('d-M-y h.i.s.u000 A'); | |
} else { | |
$datetime = date('d-M-y h.i.s.u000 A', time()); | |
} | |
if (isset($record['level_name'])) { | |
$level = $record['level_name']; | |
} else { | |
$level = 'DEBUG'; | |
} | |
if (isset($record['context']['hostname'])) { | |
$hostname = $record['context']['hostname']; | |
unset($record['context']['hostname']); | |
} else { | |
$hostname = gethostname(); | |
} | |
if ( isset($record['context']['userID'])) { | |
$userID = $record['context']['userID']; | |
unset($record['context']['userID']); | |
} else { | |
$userID = 'NA'; | |
} | |
if ( isset($record['context']['ip'])) { | |
$ip = $record['context']['ip']; | |
unset($record['context']['ip']); | |
} else { | |
$ip = 'NA'; | |
} | |
if ( isset($record['context'])) { | |
if ( count($record['context']) > 0 ) { | |
$context = json_encode($record['context']); | |
} else { | |
$context = null; | |
} | |
} else { | |
$context = null; | |
} | |
$message = $record['formatted']; | |
$oracleStatement = oci_parse($this->oracleConnection, $insertStatement); | |
oci_bind_by_name($oracleStatement, ':time_bv', $datetime); | |
oci_bind_by_name($oracleStatement, ':level_bv', $level); | |
oci_bind_by_name($oracleStatement, ':hostname_bv', $hostname); | |
oci_bind_by_name($oracleStatement, ':user_bv', $userID); | |
oci_bind_by_name($oracleStatement, ':ip_bv', $ip); | |
oci_bind_by_name($oracleStatement, ':context_bv', $context); | |
oci_bind_by_name($oracleStatement, ':message_bv', $message); | |
oci_execute($oracleStatement); | |
} | |
protected function getDefaultFormatter() { | |
$outputFormat = '%message%'; | |
$formatter = new LineFormatter($outputFormat); | |
return $formatter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment