English | 中文文档
super-kernel/logger is the official logging component of SuperKernel, providing two types of logging capabilities:
-
Standard Output Logging: By default, it outputs to the terminal (
stdout) via theLoggerclass, suitable for development and CLI environments. -
Monolog Extended Logging: Automatically creates developer-defined multichannel log instances via
LoggerFactory, supporting various Handlers such as file, network, and queues.
This component conforms to the psr/log standard and is seamlessly integrated with SuperKernel's dependency injection system and configuration system.
composer require super-kernel/loggerThis component only scans properties with
publicvisibility in classes annotated with#[Configuration(LoggerConfigInterface::class)].
<?php
declare(strict_types=1);
namespace Src\Config\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use SuperKernel\Attribute\Configuration;
use SuperKernel\Logger\Contract\LoggerConfigInterface;
#[Configuration(LoggerConfigInterface::class)]
final class LoggerConfig implements LoggerConfigInterface
{
public array $default = [
'handler' => [
'class' => StreamHandler::class,
'constructor' => [
'stream' => 'logs/default.log',
'level' => Level::Debug,
],
],
'formatter' => [
'class' => LineFormatter::class,
'constructor' => [
'format' => null,
'dateFormat' => null,
'allowInlineLineBreaks' => true,
],
],
];
}<?php
declare(strict_types=1);
namespace Src\Config\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use SuperKernel\Attribute\Configuration;
use SuperKernel\Logger\Contract\LoggerConfigInterface;
#[Configuration(LoggerConfigInterface::class)]
final class LoggerConfig implements LoggerConfigInterface
{
public array $default = [
[
'handler' => [
'class' => StreamHandler::class,
'constructor' => [
'stream' => 'logs/default.log',
'level' => Level::Debug,
],
],
'formatter' => [
'class' => LineFormatter::class,
'constructor' => [
'format' => null,
'dateFormat' => null,
'allowInlineLineBreaks' => true,
],
],
],
[
'handler' => [
'class' => StreamHandler::class,
'constructor' => [
'stream' => 'logs/default.log',
'level' => Level::Debug,
],
],
'formatter' => [
'class' => LineFormatter::class,
'constructor' => [
'format' => null,
'dateFormat' => null,
'allowInlineLineBreaks' => true,
],
],
],
];
}<?php
declare(strict_types=1);
namespace Src\Service;
use Psr\Log\LoggerInterface;
final class DemoService
{
public function __construct(private readonly LoggerInterface $logger)
{
}
public function method()
{
$this->logger->info('Application started');
$this->logger->error('Something went wrong');
}
}<?php
declare(strict_types=1);
namespace Src\Service;
use Psr\Log\LoggerInterface;
final class DemoService
{
private readonly LoggerInterface $logger;
public function __construct(LoggerFactoryInterface $loggerFactory)
{
$this->logger = $loggerFactory->get('default');
}
public function method()
{
$this->logger->info('Application started');
$this->logger->error('Something went wrong');
}
}The logs output by framework components are standard output logs. We generally believe that such log output does not need to be recorded, but developers who have such needs can consider the following method:
nohup your_command > output.log 2>&1 &
After exiting the terminal, the process continues to run in the background, and standard output logs will be written to output.log.
setsid your_command > output.log 2>&1 &
Similar to nohup, but it does not inherit the original terminal's session ID, thus more completely detaching from the controlling terminal.
<?php
declare(strict_types=1);
namespace Src\Provider;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Stringable;
use SuperKernel\Attribute\Factory;
use SuperKernel\Attribute\Provider;
use Symfony\Component\Console\Output\ConsoleOutput;
#[
Provider(class: LoggerInterface::class, priority: 2),
Factory,
]
final class Logger implements LoggerInterface
{
public function __invoke(LoggerFactoryInterface $loggerFactory): LoggerInterface
{
return $loggerFactory->get('your logger name');
}
}Please visit Visit monolog/monolog to learn more.