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