Code cleanup
This commit is contained in:
parent
92f9445633
commit
49b9d139ec
@ -1,9 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace App\Ardent\LoggerBundle\Entity;
|
namespace App\Ardent\LoggerBundle\Entity;
|
||||||
|
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
@ -22,7 +20,7 @@ class LogEntry
|
|||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string")
|
* @ORM\Column(type="text")
|
||||||
*/
|
*/
|
||||||
private $message;
|
private $message;
|
||||||
|
|
||||||
@ -69,11 +67,13 @@ class LogEntry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $id
|
* @param mixed $id
|
||||||
|
*
|
||||||
* @return LogEntry
|
* @return LogEntry
|
||||||
*/
|
*/
|
||||||
public function setId($id)
|
public function setId($id)
|
||||||
{
|
{
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,11 +87,13 @@ class LogEntry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $message
|
* @param mixed $message
|
||||||
|
*
|
||||||
* @return LogEntry
|
* @return LogEntry
|
||||||
*/
|
*/
|
||||||
public function setMessage($message)
|
public function setMessage($message)
|
||||||
{
|
{
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,11 +107,13 @@ class LogEntry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $context
|
* @param mixed $context
|
||||||
|
*
|
||||||
* @return LogEntry
|
* @return LogEntry
|
||||||
*/
|
*/
|
||||||
public function setContext($context)
|
public function setContext($context)
|
||||||
{
|
{
|
||||||
$this->context = $context;
|
$this->context = $context;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,11 +127,13 @@ class LogEntry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $level
|
* @param mixed $level
|
||||||
|
*
|
||||||
* @return LogEntry
|
* @return LogEntry
|
||||||
*/
|
*/
|
||||||
public function setLevel($level)
|
public function setLevel($level)
|
||||||
{
|
{
|
||||||
$this->level = $level;
|
$this->level = $level;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,11 +147,13 @@ class LogEntry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $levelName
|
* @param mixed $levelName
|
||||||
|
*
|
||||||
* @return LogEntry
|
* @return LogEntry
|
||||||
*/
|
*/
|
||||||
public function setLevelName($levelName)
|
public function setLevelName($levelName)
|
||||||
{
|
{
|
||||||
$this->levelName = $levelName;
|
$this->levelName = $levelName;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,11 +167,13 @@ class LogEntry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $channel
|
* @param mixed $channel
|
||||||
|
*
|
||||||
* @return LogEntry
|
* @return LogEntry
|
||||||
*/
|
*/
|
||||||
public function setChannel($channel)
|
public function setChannel($channel)
|
||||||
{
|
{
|
||||||
$this->channel = $channel;
|
$this->channel = $channel;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,11 +187,13 @@ class LogEntry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $createdAt
|
* @param mixed $createdAt
|
||||||
|
*
|
||||||
* @return LogEntry
|
* @return LogEntry
|
||||||
*/
|
*/
|
||||||
public function setCreatedAt($createdAt)
|
public function setCreatedAt($createdAt)
|
||||||
{
|
{
|
||||||
$this->createdAt = $createdAt;
|
$this->createdAt = $createdAt;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,11 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace App\Ardent\LoggerBundle\Service;
|
namespace App\Ardent\LoggerBundle\Service;
|
||||||
|
|
||||||
|
|
||||||
use App\Ardent\LoggerBundle\Entity\LogEntry;
|
use App\Ardent\LoggerBundle\Entity\LogEntry;
|
||||||
use DateTime;
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use phpDocumentor\Reflection\Types\This;
|
use phpDocumentor\Reflection\Types\This;
|
||||||
use Psr\Log\InvalidArgumentException;
|
use Psr\Log\InvalidArgumentException;
|
||||||
@ -13,24 +10,24 @@ use Psr\Log\InvalidArgumentException;
|
|||||||
class LoggerService
|
class LoggerService
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Detailed debug information
|
* Detailed debug information.
|
||||||
*/
|
*/
|
||||||
const DEBUG = 100;
|
const DEBUG = 100;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interesting events
|
* Interesting events.
|
||||||
*
|
*
|
||||||
* Examples: User logs in, SQL logs.
|
* Examples: User logs in, SQL logs.
|
||||||
*/
|
*/
|
||||||
const INFO = 200;
|
const INFO = 200;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uncommon events
|
* Uncommon events.
|
||||||
*/
|
*/
|
||||||
const NOTICE = 250;
|
const NOTICE = 250;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exceptional occurrences that are not errors
|
* Exceptional occurrences that are not errors.
|
||||||
*
|
*
|
||||||
* Examples: Use of deprecated APIs, poor use of an API,
|
* Examples: Use of deprecated APIs, poor use of an API,
|
||||||
* undesirable things that are not necessarily wrong.
|
* undesirable things that are not necessarily wrong.
|
||||||
@ -38,19 +35,19 @@ class LoggerService
|
|||||||
const WARNING = 300;
|
const WARNING = 300;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runtime errors
|
* Runtime errors.
|
||||||
*/
|
*/
|
||||||
const ERROR = 400;
|
const ERROR = 400;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Critical conditions
|
* Critical conditions.
|
||||||
*
|
*
|
||||||
* Example: Application component unavailable, unexpected exception.
|
* Example: Application component unavailable, unexpected exception.
|
||||||
*/
|
*/
|
||||||
const CRITICAL = 500;
|
const CRITICAL = 500;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Action must be taken immediately
|
* Action must be taken immediately.
|
||||||
*
|
*
|
||||||
* Example: Entire website down, database unavailable, etc.
|
* Example: Entire website down, database unavailable, etc.
|
||||||
* This should trigger the SMS alerts and wake you up.
|
* This should trigger the SMS alerts and wake you up.
|
||||||
@ -63,9 +60,9 @@ class LoggerService
|
|||||||
const EMERGENCY = 600;
|
const EMERGENCY = 600;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logging levels from syslog protocol defined in RFC 5424
|
* Logging levels from syslog protocol defined in RFC 5424.
|
||||||
*
|
*
|
||||||
* @var array $levels Logging levels
|
* @var array Logging levels
|
||||||
*/
|
*/
|
||||||
protected static $levels = array(
|
protected static $levels = array(
|
||||||
self::DEBUG => 'DEBUG',
|
self::DEBUG => 'DEBUG',
|
||||||
@ -87,11 +84,12 @@ class LoggerService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* LoggerService constructor.
|
* LoggerService constructor.
|
||||||
|
*
|
||||||
* @param EntityManagerInterface $em
|
* @param EntityManagerInterface $em
|
||||||
*/
|
*/
|
||||||
public function __construct(EntityManagerInterface $em)
|
public function __construct(EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
$this->name = "";
|
$this->name = '';
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,8 +121,8 @@ class LoggerService
|
|||||||
{
|
{
|
||||||
$levelName = static::getLevelName($level);
|
$levelName = static::getLevelName($level);
|
||||||
|
|
||||||
if( $level > self::WARNING) {
|
if ($level > self::WARNING) {
|
||||||
$context += $this->automaticContext();
|
$context = array_merge($context, $this->automaticContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
$logEntry = (new LogEntry())
|
$logEntry = (new LogEntry())
|
||||||
@ -138,11 +136,12 @@ class LoggerService
|
|||||||
$this->em->persist($logEntry);
|
$this->em->persist($logEntry);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
||||||
return true;
|
return $logEntry->getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function automaticContext()
|
private function automaticContext()
|
||||||
{
|
{
|
||||||
|
// filter the kernel classes and itself
|
||||||
$classFilters = [
|
$classFilters = [
|
||||||
'Ardent\LoggerBundle\Service\LoggerService',
|
'Ardent\LoggerBundle\Service\LoggerService',
|
||||||
'HttpKernel\Kernel',
|
'HttpKernel\Kernel',
|
||||||
@ -154,17 +153,18 @@ class LoggerService
|
|||||||
foreach ($backtraces as $trace) {
|
foreach ($backtraces as $trace) {
|
||||||
$skip = false;
|
$skip = false;
|
||||||
foreach ($classFilters as $class) {
|
foreach ($classFilters as $class) {
|
||||||
if(strpos($trace['class'], $class)) {
|
if (strpos($trace['class'], $class)) {
|
||||||
$skip = true;
|
$skip = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if($skip) {
|
if ($skip) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$context[] = "function[$traces]: ".$trace['function'];
|
$context[] = "function[$traces]: ".$trace['function'];
|
||||||
$context[] = "class[$traces]: ".$trace['class'];
|
$context[] = "class[$traces]: ".$trace['class'];
|
||||||
++$traces;
|
++$traces;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $context;
|
return $context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +179,6 @@ class LoggerService
|
|||||||
->orderBy('l.createdAt', 'DESC')
|
->orderBy('l.createdAt', 'DESC')
|
||||||
->setParameter(1, $level)
|
->setParameter(1, $level)
|
||||||
->setMaxResults($amount);
|
->setMaxResults($amount);
|
||||||
;
|
|
||||||
|
|
||||||
return $qb->getQuery()->getResult();
|
return $qb->getQuery()->getResult();
|
||||||
}
|
}
|
||||||
@ -188,6 +187,7 @@ class LoggerService
|
|||||||
* Gets the name of the logging level.
|
* Gets the name of the logging level.
|
||||||
*
|
*
|
||||||
* @param int $level
|
* @param int $level
|
||||||
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function getLevelName($level)
|
public static function getLevelName($level)
|
||||||
@ -209,11 +209,13 @@ class LoggerService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $name
|
* @param mixed $name
|
||||||
|
*
|
||||||
* @return LoggerService
|
* @return LoggerService
|
||||||
*/
|
*/
|
||||||
public function setName($name)
|
public function setName($name)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user