PHP code example of tobento / app-logging

1. Go to this page and download the library: Download tobento/app-logging library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tobento / app-logging example snippets


use Tobento\App\AppFactory;
use Tobento\App\Logging\LoggersInterface;
use Psr\Log\LoggerInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Logging\Boot\Logging::class);
$app->booting();

// Implemented interfaces:
$logger = $app->get(LoggerInterface::class);
$loggers = $app->get(LoggersInterface::class);

// Run the app
$app->run();

use Tobento\App\Logging\LoggerTrait;

class SomeService
{
    use LoggerTrait;

    public function someAction(): void
    {
        $this->getLogger()->info('Some info');
        // is same as:
        $this->getLogger(name: static::class)->info('Some info');
        // if the named logger does not exists,
        // the default logger will be used.
        
        // or using another named logger:
        $this->getLogger(name: 'daily')->info('Some info');
    }
}

/*
|--------------------------------------------------------------------------
| Aliases
|--------------------------------------------------------------------------
*/

'aliases' => [
    SomeService::class => 'daily',
],

use Tobento\App\Logging\LoggersInterface;

$app->get(LoggersInterface::class)->addAlias(alias: SomeService::class, logger: 'daily');

use Tobento\App\Logging\LazyLoggers;
use Tobento\App\Logging\LoggerFactoryInterface;
use Tobento\App\Logging\LoggersInterface;
use Tobento\App\Logging\StackLoggerFactory;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

$loggers = new LazyLoggers(
    container: $container, // ContainerInterface
    loggers: [
        // using a closure:
        'daily' => static function (string $name, ContainerInterface $c): LoggerInterface {
            // create logger ...
            return $logger;
        },
        
        // using a factory:
        'stacked' => [
            // factory must implement LoggerFactoryInterface
            'factory' => StackLoggerFactory::class,
            'config' => [
                'loggers' => ['daily', 'another'],
            ],
        ],
        
        // or you may sometimes just create a logger (not lazy):
        'null' => new NullLogger(),
    ],
);

var_dump($loggers instanceof LoggersInterface);
// bool(true)

use Tobento\App\Logging\StackLoggerFactory;
use Tobento\App\Logging\LoggerFactoryInterface;
use Tobento\App\Logging\LoggersInterface;

$factory = new StackLoggerFactory(
    loggers: $loggers // LoggersInterface
);

var_dump($factory instanceof LoggerFactoryInterface);
// bool(true)

use Psr\Log\LoggerInterface;
use Tobento\App\Logging\StackLogger;

$logger = $factory->createLogger(name: 'stacked', config: [
    // specify the loggers you want to be stacked:
    'loggers' => ['daily', 'syslog'],
]);

var_dump($logger instanceof LoggerInterface);
// bool(true)

var_dump($logger instanceof StackLogger);
// bool(true)

use Tobento\App\Logging\Event;

use Tobento\App\Logging\Monolog\EventHandler;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

'loggers' => [

    'daily' => static function (string $name, ContainerInterface $c): LoggerInterface {
        return new Logger(
            name: $name,
            handlers: [
                // support events:
                $c->get(EventHandler::class),
            ],
        );
    },
],

use Psr\Log\LoggerInterface;

$loggers->add(name: 'daily', logger: $logger); // LoggerInterface

// or using a closure:
$loggers->add(name: 'daily', logger: static function (): LoggerInterface {
    return $createdLogger;
});

$loggers->addAlias(alias: 'alias', logger: 'daily');

// get a logger by an alias:
$logger = $loggers->get('alias');
// returns the 'daily' logger if exists.

use Psr\Log\LoggerInterface;

// get the default logger:
$logger = $loggers->logger();

// get a named logger:
$logger = $loggers->logger(name: 'daily');

// get an aliased logger:
$logger = $loggers->logger(name: 'alias');

var_dump($logger instanceof LoggerInterface);
// bool(true)

use Psr\Log\LoggerInterface;

// get a named logger:
$logger = $loggers->get(name: 'daily');

// get an aliased logger:
$logger = $loggers->get(name: 'alias');

var_dump($logger instanceof LoggerInterface);
// bool(true) or NULL if not exist

$has = $loggers->has(name: 'daily');

// with an alias:
$has = $loggers->has(name: 'alias');

$names = $loggers->names();

var_dump($names);
// array(1) {[0]=> string(5) "daily"}

use Psr\Log\LoggerInterface;

$loggers = $loggers->created();
// array<string, LoggerInterface>

use Psr\Log\LoggerInterface;

$logger = $loggerFactory->createLogger(
    name: 'stacked', // a logger name
    config: [], // any data for creating the logger
);

var_dump($logger instanceof LoggerInterface);
// bool(true)
app/config/logging.php