Skip to content

super-kernel/logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

English | 中文文档

super-kernel/logger

Log component of SuperKernel framework

License PHP Monolog SuperKernel


Overview

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 the Logger class, 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.


Installation

composer require super-kernel/logger

Configuration

This component only scans properties with public visibility in classes annotated with #[Configuration(LoggerConfigInterface::class)].

Single handler configuration

<?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, 
            ], 
        ], 
    ];
}

Multiple handler configurations

<?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, 
                ], 
            ], 
        ], 
    ];
}

🚀 Example

Standard output log

<?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'); 
    }
}

Standard output log

<?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');
    }
}

Advanced

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

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

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.

Override the LoggerInterface class provider

<?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'); 
    }
}

More usage

Please visit Visit monolog/monolog to learn more.

About

The logger component for super-kernel.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages