summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Format.php28
-rw-r--r--src/Loggr.php27
2 files changed, 26 insertions, 29 deletions
diff --git a/src/Format.php b/src/Format.php
index 5f914b7..3909a7d 100644
--- a/src/Format.php
+++ b/src/Format.php
@@ -20,13 +20,13 @@ enum Format
* @param Message $message The message to be serialized.
* @return string The serialized message.
*/
- public function serialize(Message $message): string
+ public function serialize(Message $message, ?DateTime $dateTime = new DateTime): string
{
return match($this) {
- self::JSON => $this->serializeJson($message),
- self::IntelliJ => $this->serializeIntelliJ($message),
- self::Laravel => $this->serializeLaravel($message),
- self::Symfony => $this->serializeSymfony($message),
+ self::JSON => $this->serializeJson($message, $dateTime),
+ self::IntelliJ => $this->serializeIntelliJ($message, $dateTime),
+ self::Laravel => $this->serializeLaravel($message, $dateTime),
+ self::Symfony => $this->serializeSymfony($message, $dateTime),
};
}
@@ -36,14 +36,14 @@ enum Format
* @param Message $message The message to be serialized.
* @return string The JSON-encoded message.
*/
- private function serializeJson(Message $message): string
+ private function serializeJson(Message $message, DateTime $dateTime): string
{
return json_encode([
- 'date' => (new DateTime)->format('Y-m-d H:i:s'),
+ 'date' => $dateTime->format('Y-m-d H:i:s'),
'level' => $message->level->value,
'context' => $message->context,
'trace' => [
- 'file' => $message->trace['file'],
+ 'file' => pathinfo($message->trace['file'], PATHINFO_FILENAME),
'line' => $message->trace['line'],
],
]);
@@ -55,10 +55,10 @@ enum Format
* @param Message $message The message to be serialized.
* @return string The serialized message.
*/
- private function serializeLaravel(Message $message): string
+ private function serializeLaravel(Message $message, DateTime $dateTime): string
{
// Date
- $date = (new DateTime)->format('Y-m-d H:i:s');
+ $date = $dateTime->format('Y-m-d H:i:s');
$line = "[$date] ";
// File name and level
@@ -81,10 +81,10 @@ enum Format
* @param Message $message The message object that contains log details.
* @return string The formatted log string.
*/
- private function serializeSymfony(Message $message): string
+ private function serializeSymfony(Message $message, DateTime $dateTime): string
{
// Date
- $date = (new DateTime)->format('Y-m-d\TH:i:s.uP');
+ $date = $dateTime->format('Y-m-d\TH:i:s.uP');
$line = "[$date] ";
// File name and level
@@ -107,10 +107,10 @@ enum Format
* @param Message $message The message object that contains log details.
* @return string The formatted log string.
*/
- private function serializeIntelliJ(Message $message): string
+ private function serializeIntelliJ(Message $message, DateTime $dateTime): string
{
// Date
- $date = (new DateTime)->format('Y-m-d H:i:s');
+ $date = $dateTime->format('Y-m-d H:i:s');
$line = "{$date} ";
// Line number
diff --git a/src/Loggr.php b/src/Loggr.php
index 9b52f1d..6727bb1 100644
--- a/src/Loggr.php
+++ b/src/Loggr.php
@@ -15,6 +15,15 @@ class Loggr
{
private array $trace;
+ /**
+ * Since it would suck if a logging utility throws an exception,
+ * Loggr instead of throwing will simply set the error here, in case
+ * logging doesn't work, and you might want to find out why.
+ *
+ * @var string|null $error
+ */
+ public ?string $error = null;
+
public function __construct(
public ?Driver $driver = null,
public ?Format $format = Format::Laravel,
@@ -26,16 +35,12 @@ class Loggr
* @param Level $level The severity level of the log message. Defaults to Level::Info.
* @param mixed $context Additional data or context to include with the log message. Optional.
* @return void
- * @throws \Exception
*/
private function log(Level $level = Level::Info, mixed $context = null): void
{
- if (!$this->driver) {
- throw new \Exception("No driver has been set.");
- }
-
- if (!$this->format) {
- throw new \Exception("No format has been set.");
+ if (!$this->driver || !$this->format) {
+ $this->error = "Driver or format not set.";
+ return;
}
$this->driver->log($this->format->serialize(new Message(
@@ -50,7 +55,6 @@ class Loggr
*
* @param mixed $context Additional data or context to include with the emergency message. Optional.
* @return void
- * @throws \Exception
*/
public function emergency(mixed $context = null): void
{
@@ -63,7 +67,6 @@ class Loggr
*
* @param mixed $context Additional data or context to include with the log message. Optional.
* @return void
- * @throws \Exception
*/
public function alert(mixed $context = null): void
{
@@ -76,7 +79,6 @@ class Loggr
*
* @param mixed $context Additional data or context to include with the log message. Optional.
* @return void
- * @throws \Exception
*/
public function critical( mixed $context = null): void
{
@@ -89,7 +91,6 @@ class Loggr
*
* @param mixed $context Additional data or context to include with the error message. Optional.
* @return void
- * @throws \Exception
*/
public function error(mixed $context = null): void
{
@@ -102,7 +103,6 @@ class Loggr
*
* @param mixed $context Optional context information to include in the log.
* @return void
- * @throws \Exception
*/
public function warning(mixed $context = null): void
{
@@ -115,7 +115,6 @@ class Loggr
*
* @param mixed $context Optional context information to include in the log.
* @return void
- * @throws \Exception
*/
public function notice(mixed $context = null): void
{
@@ -128,7 +127,6 @@ class Loggr
*
* @param mixed $context Optional context information to include in the log.
* @return void
- * @throws \Exception
*/
public function info(mixed $context = null): void
{
@@ -141,7 +139,6 @@ class Loggr
*
* @param mixed $context Optional context information to include in the log.
* @return void
- * @throws \Exception
*/
public function debug(mixed $context = null): void
{