Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"quillstack/unit-tests": "^0.2.15"
},
"require": {
"psr/log": "^3.0"
"ext-json": "*",
"psr/log": "^3.0",
"quillstack/di": "^0.0.8",
"quillstack/local-storage": "^0.0.6"
},
"extra": {
"branch-alias": {
Expand Down
12 changes: 12 additions & 0 deletions src/Exceptions/HandlerNotSetException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Quillstack\Logger\Exceptions;

use Quillstack\Logger\LoggerException;

class HandlerNotSetException extends LoggerException
{
//
}
10 changes: 10 additions & 0 deletions src/HandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Quillstack\Logger;

interface HandlerInterface
{
public function log($level, \Stringable|string $message, array $context = []): bool;
}
23 changes: 23 additions & 0 deletions src/Handlers/LocalStorageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Quillstack\Logger\Handlers;

use Quillstack\LocalStorage\LocalStorage;
use Quillstack\Logger\HandlerInterface;

class LocalStorageHandler implements HandlerInterface
{
public function __construct(private string $path, private LocalStorage $localStorage)
{
//
}

public function log($level, \Stringable|string $message, array $context = []): bool
{
$line = $level . ' ' . $message . ' ' . json_encode($context);

return $this->localStorage->save($this->path, $line);
}
}
98 changes: 98 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Quillstack\Logger;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Quillstack\Logger\Exceptions\HandlerNotSetException;

class Logger implements LoggerInterface
{
private ?HandlerInterface $handler;

/**
* Sets handler object (mandatory).
*/
public function setHandler(HandlerInterface $handler): void
{
$this->handler = $handler;
}

/**
* {@inheritDoc}
*/
public function emergency(\Stringable|string $message, array $context = []): void
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}

/**
* {@inheritDoc}
*/
public function alert(\Stringable|string $message, array $context = []): void
{
$this->log(LogLevel::ALERT, $message, $context);
}

/**
* {@inheritDoc}
*/
public function critical(\Stringable|string $message, array $context = []): void
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

/**
* {@inheritDoc}
*/
public function error(\Stringable|string $message, array $context = []): void
{
$this->log(LogLevel::ERROR, $message, $context);
}

/**
* {@inheritDoc}
*/
public function warning(\Stringable|string $message, array $context = []): void
{
$this->log(LogLevel::WARNING, $message, $context);
}

/**
* {@inheritDoc}
*/
public function notice(\Stringable|string $message, array $context = []): void
{
$this->log(LogLevel::NOTICE, $message, $context);
}

/**
* {@inheritDoc}
*/
public function info(\Stringable|string $message, array $context = []): void
{
$this->log(LogLevel::INFO, $message, $context);
}

/**
* {@inheritDoc}
*/
public function debug(\Stringable|string $message, array $context = []): void
{
$this->log(LogLevel::DEBUG, $message, $context);
}

/**
* {@inheritDoc}
*/
public function log($level, \Stringable|string $message, array $context = []): void
{
if ($this->handler === null) {
throw new HandlerNotSetException();
}

$this->handler->log($level, $message, $context);
}
}
10 changes: 10 additions & 0 deletions src/LoggerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Quillstack\Logger;

class LoggerException extends \RuntimeException
{
//
}