| name | acc-create-psr3-logger |
| description | Generates PSR-3 Logger implementation for PHP 8.5. Creates LoggerInterface implementations with log levels, context interpolation, and LoggerAwareTrait usage. Includes unit tests. |
PSR-3 Logger Generator
Overview
Generates PSR-3 compliant logger implementations following Psr\Log\LoggerInterface.
When to Use
- Implementing custom logging solutions
- Creating application-specific loggers
- Building logging adapters for external services
- Testing with mock/null loggers
Generated Components
| Component | Description | Location |
|---|
| Logger Implementation | Concrete logger class | src/Infrastructure/Logger/ |
| LoggerAware Trait | For classes needing logger | src/Infrastructure/Logger/ |
| Null Logger | Testing/no-op logger | src/Infrastructure/Logger/ |
| Unit Tests | PHPUnit tests | tests/Unit/Infrastructure/Logger/ |
File Naming
| Type | Pattern | Example |
|---|
| File Logger | {Name}Logger.php | FileLogger.php |
| Stream Logger | StreamLogger.php | StreamLogger.php |
| Null Logger | NullLogger.php | NullLogger.php |
| Trait | LoggerAwareTrait.php | LoggerAwareTrait.php |
Template: File Logger
<?php
declare(strict_types=1);
namespace App\Infrastructure\Logger;
use DateTimeImmutable;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Stringable;
final class FileLogger implements LoggerInterface
{
private const DATE_FORMAT = 'Y-m-d H:i:s.u';
public function __construct(
private readonly string $logFile,
private readonly string $minLevel = LogLevel::DEBUG,
) {
}
public function emergency(string|Stringable $message, array $context = []): void
{
$this->(::, , );
}
{
->(::, , );
}
{
->(::, , );
}
{
->(::, , );
}
{
->(::, , );
}
{
->(::, , );
}
{
->(::, , );
}
{
->(::, , );
}
{
(!->()) {
;
}
= ( ())->(::);
= ->(() , );
= (
,
,
(),
,
->(),
);
(->logFile, , FILE_APPEND | LOCK_EX);
}
{
= [
:: => ,
:: => ,
:: => ,
:: => ,
:: => ,
:: => ,
:: => ,
:: => ,
];
([] ?? ) >= ([->minLevel] ?? );
}
{
= [];
( => ) {
(() || ) {
[ . . ] = () ;
}
}
(, );
}
{
(()) {
;
}
= (
,
fn() => !() && ! ,
);
(()) {
;
}
. (, JSON_UNESCAPED_SLASHES);
}
}
Template: Null Logger
<?php
declare(strict_types=1);
namespace App\Infrastructure\Logger;
use Psr\Log\LoggerInterface;
use Stringable;
final readonly class NullLogger implements LoggerInterface
{
public function emergency(string|Stringable $message, array $context = []): void
{
}
public function alert(string|Stringable $message, array $context = []): void
{
}
public function critical(string|Stringable $message, array $context = []): void
{
}
public function error(| , = []):
{
}
{
}
{
}
{
}
{
}
{
}
}
Template: Logger Aware
<?php
declare(strict_types=1);
namespace App\Infrastructure\Logger;
use Psr\Log\LoggerInterface;
trait LoggerAwareTrait
{
private ?LoggerInterface $logger = null;
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
protected function getLogger(): LoggerInterface
{
return $this->logger ?? new NullLogger();
}
}
Template: Unit Test
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Infrastructure\Logger;
use App\Infrastructure\Logger\FileLogger;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;
#[Group('unit')]
#[CoversClass(FileLogger::class)]
final class FileLoggerTest extends TestCase
{
private string $logFile;
protected function setUp():
{
->logFile = () . . () . ;
}
{
((->logFile)) {
(->logFile);
}
}
{
= (->logFile);
->();
= (->logFile);
::(, );
::(, );
}
{
= (->logFile);
->(, [ => ]);
= (->logFile);
::(, );
}
{
= (->logFile, ::);
->();
->();
->();
= (->logFile);
::(, );
::(, );
::(, );
}
{
= (->logFile);
->(, [ => , => ]);
= (->logFile);
::(, );
}
}
Usage Examples
Basic Logging
<?php
use App\Infrastructure\Logger\FileLogger;
use Psr\Log\LogLevel;
$logger = new FileLogger('/var/log/app.log', LogLevel::INFO);
$logger->info('Application started');
$logger->error('Something went wrong', ['exception' => $e->getMessage()]);
With Context Interpolation
<?php
$logger->info('User {user_id} performed {action}', [
'user_id' => 123,
'action' => 'login',
'ip' => '192.168.1.1',
]);
Logger Aware Service
<?php
declare(strict_types=1);
namespace App\Application\User\Handler;
use App\Infrastructure\Logger\LoggerAwareTrait;
use Psr\Log\LoggerAwareInterface;
final class CreateUserHandler implements LoggerAwareInterface
{
use LoggerAwareTrait;
public function __invoke(CreateUserCommand $command): void
{
$this->getLogger()->info('Creating user', ['email' => $command->email]);
$this->getLogger()->info('User created successfully');
}
}
Requirements
{
"require": {
"psr/log": "^3.0"
}
}
See Also
references/templates.md - Additional logger implementations
references/examples.md - Real-world usage examples