Commit 537da486 authored by Alex Ne's avatar Alex Ne

Логер ошибок

parent 052332a0
<?php <?php
namespace X\ETrace; namespace X\ETrace;
class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable {
{ /**
* @var mixed
*/
protected $time, $microtime, $session_id;
/** /**
* Хранилище объектов * Хранилище объектов
* @var array * @var array
*/ */
protected $__collection = []; protected $__collection = [];
/**
* @var array
*/
protected $context_collection = [];
public function __construct() {
$this->time = time();
$this->microtime = microtime();
$this->session_id = md5($this->microtime);
}
// -------------------------------------------------------------------- // --------------------------------------------------------------------
public function Serialize() public function Serialize() {
{
return serialize($this); return serialize($this);
} }
...@@ -24,10 +34,8 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -24,10 +34,8 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* @throws Exception * @throws Exception
* @return void * @return void
*/ */
private function __check_type(&$object) private function __check_type(&$object) {
{ if ( ! ($object instanceof EItem)) {
if ( ! ($object instanceof EItem))
{
throw new \Exception('Объект типа `' . get_class($object) throw new \Exception('Объект типа `' . get_class($object)
. '` не может быть добавлен в коллекцию объектов типа `' . $this->__type . '`'); . '` не может быть добавлен в коллекцию объектов типа `' . $this->__type . '`');
} }
...@@ -41,11 +49,9 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -41,11 +49,9 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* @param object(s) Объекты * @param object(s) Объекты
* @return mixed Collection * @return mixed Collection
*/ */
public function add() public function add() {
{
$args = func_get_args(); $args = func_get_args();
foreach ($args as $object) foreach ($args as $object) {
{
$this->offsetSet(null, $object); $this->offsetSet(null, $object);
} }
return $this; return $this;
...@@ -59,11 +65,9 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -59,11 +65,9 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* @param object(s) Объекты * @param object(s) Объекты
* @return mixed Collection * @return mixed Collection
*/ */
public function remove() public function remove() {
{
$args = func_get_args(); $args = func_get_args();
foreach ($args as $object) foreach ($args as $object) {
{
unset($this->__collection[array_search($object, $this->__collection)]); unset($this->__collection[array_search($object, $this->__collection)]);
} }
return $this; return $this;
...@@ -76,8 +80,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -76,8 +80,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* *
* @return mixed Collection * @return mixed Collection
*/ */
public function clear() public function clear() {
{
$this->__collection = []; $this->__collection = [];
return $this; return $this;
} }
...@@ -89,8 +92,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -89,8 +92,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* *
* @return bool * @return bool
*/ */
public function isEmpty() public function isEmpty() {
{
return empty($this->__collection); return empty($this->__collection);
} }
...@@ -104,8 +106,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -104,8 +106,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* *
* @return CollectionIterator * @return CollectionIterator
*/ */
public function getIterator() public function getIterator() {
{
return new \ArrayIterator($this->__collection); return new \ArrayIterator($this->__collection);
} }
...@@ -121,16 +122,14 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -121,16 +122,14 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* @param mixed $offset Object * @param mixed $offset Object
* @return void * @return void
*/ */
public function offsetSet($offset, $object) public function offsetSet($offset, $object) {
{
$this->__check_type($object); $this->__check_type($object);
$offset = $object->getHash(); $offset = $object->getHash();
if (isset($this->__collection[$offset])) $this->context_collection[$offset][] = $object->getContext();
{ $object->clean_context();
if (isset($this->__collection[$offset])) {
$this->__collection[$offset]->increment(); $this->__collection[$offset]->increment();
} } else {
else
{
$this->__collection[$offset] = $object; $this->__collection[$offset] = $object;
} }
} }
...@@ -143,8 +142,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -143,8 +142,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* @param integer $offset Ключ * @param integer $offset Ключ
* @return bool * @return bool
*/ */
public function offsetExists($offset) public function offsetExists($offset) {
{
return isset($this->__collection[$offset]); return isset($this->__collection[$offset]);
} }
...@@ -156,8 +154,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -156,8 +154,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* @param integer $offset Ключ * @param integer $offset Ключ
* @return void * @return void
*/ */
public function offsetUnset($offset) public function offsetUnset($offset) {
{
unset($this->__collection[$offset]); unset($this->__collection[$offset]);
} }
...@@ -169,10 +166,8 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -169,10 +166,8 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* @param integer $offset Ключ * @param integer $offset Ключ
* @return mixed * @return mixed
*/ */
public function offsetGet($offset) public function offsetGet($offset) {
{ if (isset($this->__collection[$offset]) === FALSE) {
if (isset($this->__collection[$offset]) === FALSE)
{
return NULL; return NULL;
} }
return $this->__collection[$offset]; return $this->__collection[$offset];
...@@ -188,8 +183,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable ...@@ -188,8 +183,7 @@ class ECollection implements \IteratorAggregate, \ArrayAccess, \Countable
* *
* @return integer * @return integer
*/ */
public function count() public function count() {
{
return sizeof($this->__collection); return sizeof($this->__collection);
} }
} }
......
...@@ -5,6 +5,11 @@ namespace X\ETrace; ...@@ -5,6 +5,11 @@ namespace X\ETrace;
* *
*/ */
class EItem extends \Exception { class EItem extends \Exception {
/**
* @var mixed
*/
protected $run_context;
/** /**
* @var int * @var int
*/ */
...@@ -13,11 +18,22 @@ class EItem extends \Exception { ...@@ -13,11 +18,22 @@ class EItem extends \Exception {
* @var mixed * @var mixed
*/ */
protected $hash; protected $hash;
/** /**
* @var mixed * @var mixed
*/ */
protected $context; protected $host;
/**
* @var mixed
*/
protected $type;
/**
* @var mixed
*/
protected $session_id;
/**
* @var mixed
*/
protected $object_name;
/** /**
* @param enum $type paranoid | trace | system | fatal | error | notification * @param enum $type paranoid | trace | system | fatal | error | notification
...@@ -39,9 +55,12 @@ class EItem extends \Exception { ...@@ -39,9 +55,12 @@ class EItem extends \Exception {
if ( ! ($line === false)) { if ( ! ($line === false)) {
$this->line = $line; $this->line = $line;
} }
$this->type = $type;
$this->context = $context; $this->run_context = $context;
$this->hash = $this->calcHash(); $this->hash = $this->calcHash();
$this->host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : "no.host";
$this->session_id = md5(microtime());
$this->object_name = (new \ReflectionClass($this))->getName();
} }
/** /**
...@@ -62,7 +81,7 @@ class EItem extends \Exception { ...@@ -62,7 +81,7 @@ class EItem extends \Exception {
* @return mixed * @return mixed
*/ */
public function getContext() { public function getContext() {
return $this->context; return $this->run_context;
} }
/** /**
...@@ -75,15 +94,17 @@ class EItem extends \Exception { ...@@ -75,15 +94,17 @@ class EItem extends \Exception {
public function Model() { public function Model() {
return return
[ [
"hash" => $this->hash, "hash" => $this->hash,
"message" => $this->message, "host" => $this->host,
"code" => $this->code, "type" => $this->type,
"file" => $this->file, "code" => $this->code,
"line" => $this->line, "file" => $this->file,
"count" => $this->count, "line" => $this->line,
"trace" => $this->Trace(), "count" => $this->count,
"context" => $this->context, "trace" => $this->Trace(),
"globals" => $GLOBALS, "message" => $this->message,
"object_name" => $this->object_name,
"context" => $this->run_context,
]; ];
} }
...@@ -91,6 +112,17 @@ class EItem extends \Exception { ...@@ -91,6 +112,17 @@ class EItem extends \Exception {
$this->count++; $this->count++;
} }
/**
* @return mixed
*/
public function count() {
return $this->count;
}
public function clean_context() {
$this->run_context = [];
}
/** /**
* @return hex * @return hex
*/ */
...@@ -98,8 +130,20 @@ class EItem extends \Exception { ...@@ -98,8 +130,20 @@ class EItem extends \Exception {
return $this->hash; return $this->hash;
} }
/**
* @param $uid
* @return mixed
*/
private function calcHash() { private function calcHash() {
return md5(serialize([$this->message, $this->code, $this->file, $this->line, $this->Trace()])); return md5(serialize([
$this->object_name,
$this->host,
$this->type,
$this->message,
$this->code,
$this->file,
$this->line,
$this->Trace()]));
} }
} }
?> ?>
\ No newline at end of file
...@@ -6,23 +6,23 @@ class Import extends EItem { ...@@ -6,23 +6,23 @@ class Import extends EItem {
* @var array * @var array
*/ */
protected $trace; protected $trace;
/**
* @var string
*/
protected $original;
/** /**
* @param string $original * @param $exception
* @param string $message
* @param int $code
* @param string $file
* @param int $line
* @param array $trace
*/ */
public function __construct($original, $message, $code = 0, $file = false, $line = false, $trace = []) { public function __construct($exception) {
parent::__construct("system", $message, $code, $file, $line, []); parent::__construct("system",
$this->original = $original; $exception->getMessage(),
$this->trace = $trace; $exception->getCode(),
$exception->getFile(),
$exception->getLine(), []);
$this->trace = $exception->getTrace();
$this->object_name = (new \ReflectionClass($exception))->getName();
if ($exception instanceof \PDOException) {
$this->context["MySQL_Driver"] = $exception->errorInfo;
}
} }
/** /**
...@@ -31,12 +31,5 @@ class Import extends EItem { ...@@ -31,12 +31,5 @@ class Import extends EItem {
public function Trace() { public function Trace() {
return $this->trace; return $this->trace;
} }
/**
* @return array
*/
public function Model() {
return array_merge(["original" => $this->original], parent::Model());
}
} }
?> ?>
\ No newline at end of file
<?php <?php
namespace X\ETrace; namespace X\ETrace;
class Notification extends EItem class Notification extends EItem {
{
/** /**
* Упрощенный интерфейс для посылки сообщений администратору из неких участков кода. * Упрощенный интерфейс для посылки сообщений администратору из неких участков кода.
* *
* @param $message * @param $message
* @param array $context * @param array $context
*/ */
public function __construct($message, $context = []) public function __construct($message, $context = []) {
{ parent::__construct("notification", $message, 1, false, false, $context);
parent::__construct("notification", $message, 0, false, false, $context);
}
/**
* @return mixed
*/
public function Model()
{
$model = parent::Model();
unset($model["global"]);
return $model;
} }
} }
?> ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment