Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #827 from bolt/feature/logging
Feature: Add logs, including Log Viewer screen
- Loading branch information
Showing
16 changed files
with
623 additions
and
34 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
monolog: | ||
channels: ['db'] | ||
handlers: | ||
db: | ||
channels: ['db'] | ||
type: service | ||
id: Bolt\Log\LogHandler |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bolt\Controller\Backend; | ||
|
||
use Bolt\Controller\TwigAwareController; | ||
use Bolt\Repository\LogRepository; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
/** | ||
* @Security("is_granted('ROLE_ADMIN')") | ||
*/ | ||
class LogViewerController extends TwigAwareController implements BackendZone | ||
{ | ||
/** | ||
* @Route("/logviewer", name="bolt_logviewer", methods={"GET"}) | ||
*/ | ||
public function index(LogRepository $log, Request $request): Response | ||
{ | ||
$amount = $this->config->get('general/log/amount', 10); | ||
$page = (int) $request->get('page', 1); | ||
|
||
/** @var Log $items */ | ||
$items = $log->findLatest($page, $amount); | ||
|
||
return $this->renderTemplate('@bolt/pages/logviewer.html.twig', [ | ||
'items' => $items, | ||
]); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bolt\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ORM\Entity(repositoryClass="AppBundle\Repository\LogRepository") | ||
* @ORM\Table(name="log") | ||
* @ORM\HasLifecycleCallbacks | ||
*/ | ||
class Log | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\Column(type="integer") | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @ORM\Column(name="message", type="text") | ||
*/ | ||
private $message; | ||
|
||
/** | ||
* @ORM\Column(name="context", type="array", nullable=true) | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* @ORM\Column(name="level", type="smallint") | ||
*/ | ||
private $level; | ||
|
||
/** | ||
* @ORM\Column(name="level_name", type="string", length=50) | ||
*/ | ||
private $levelName; | ||
|
||
/** | ||
* @ORM\Column(name="created_at", type="datetime") | ||
*/ | ||
private $createdAt; | ||
|
||
/** | ||
* @ORM\Column(name="extra", type="array", nullable=true) | ||
*/ | ||
private $extra; | ||
|
||
/** | ||
* @ORM\Column(name="user", type="array", nullable=true) | ||
*/ | ||
private $user; | ||
|
||
/** | ||
* @ORM\Column(name="location", type="array", nullable=true) | ||
*/ | ||
private $location; | ||
|
||
/** | ||
* @ORM\PrePersist | ||
*/ | ||
public function onPrePersist(): void | ||
{ | ||
$this->createdAt = new \DateTime(); | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(int $id): self | ||
{ | ||
$this->id = $id; | ||
return $this; | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function setMessage(string $message): self | ||
{ | ||
$this->message = $message; | ||
return $this; | ||
} | ||
|
||
public function getContext(): ?array | ||
{ | ||
return $this->context; | ||
} | ||
|
||
public function setContext(?array $context): self | ||
{ | ||
$this->context = $context; | ||
return $this; | ||
} | ||
|
||
public function getLevel(): int | ||
{ | ||
return $this->level; | ||
} | ||
|
||
public function setLevel(int $level): self | ||
{ | ||
$this->level = $level; | ||
return $this; | ||
} | ||
|
||
public function getLevelName(): string | ||
{ | ||
return $this->levelName; | ||
} | ||
|
||
public function setLevelName(string $levelName): self | ||
{ | ||
$this->levelName = $levelName; | ||
return $this; | ||
} | ||
|
||
public function getExtra(): ?array | ||
{ | ||
return $this->extra; | ||
} | ||
|
||
public function setExtra(?array $extra): self | ||
{ | ||
$this->extra = $extra; | ||
return $this; | ||
} | ||
|
||
public function getCreatedAt(): \DateTime | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function setCreatedAt(\DateTime $createdAt): self | ||
{ | ||
$this->createdAt = $createdAt; | ||
return $this; | ||
} | ||
|
||
public function getLocation(): ?array | ||
{ | ||
return $this->location; | ||
} | ||
|
||
public function setLocation(?array $location): self | ||
{ | ||
$this->location = $location; | ||
return $this; | ||
} | ||
|
||
public function getUser(): ?array | ||
{ | ||
return $this->user; | ||
} | ||
|
||
public function setUser(?array $user): self | ||
{ | ||
$this->user = $user; | ||
return $this; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bolt\Log; | ||
|
||
use Bolt\Entity\Log; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Monolog\Handler\AbstractProcessingHandler; | ||
|
||
class LogHandler extends AbstractProcessingHandler | ||
{ | ||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
protected $em; | ||
|
||
public function __construct(EntityManagerInterface $em) | ||
{ | ||
parent::__construct(); | ||
$this->em = $em; | ||
} | ||
|
||
/** | ||
* Called when writing to our database | ||
*/ | ||
protected function write(array $record): void | ||
{ | ||
$logEntry = new Log(); | ||
$logEntry->setMessage($record['message']); | ||
$logEntry->setLevel($record['level']); | ||
$logEntry->setLevelName($record['level_name']); | ||
$logEntry->setExtra($record['extra']); | ||
$logEntry->setUser($record['user'] ?? null); | ||
$logEntry->setLocation($record['location']); | ||
$logEntry->setContext($record['context']); | ||
|
||
$this->em->persist($logEntry); | ||
$this->em->flush(); | ||
} | ||
} |
Oops, something went wrong.