PHP code example of sugiphp / logger

1. Go to this page and download the library: Download sugiphp/logger 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/ */

    

sugiphp / logger example snippets


use SugiPHP\Logger\FileLogger;

$logger = new FileLogger([
    'filename' => '/var/log/app.log',
    'level' => 'info',
    'format' => '[{datetime}] [{level}] {message} {context}',
    'dateFormat' => 'Y-m-d H:i:s',
]);

$logger->info('User logged in', ['userId' => 42]);
$logger->error('Payment failed for order {orderId}', ['orderId' => 99]);

use SugiPHP\Logger\StdoutLogger;

$logger = new StdoutLogger([
    'level' => 'debug',
    'format' => '[{level}] {message} {context}',
]);

$logger->warning('Disk space low');

use Psr\Log\NullLogger;

$logger = new NullLogger(); // accepts calls, writes nothing

use SugiPHP\Logger\LoggerTrait;
use Psr\Log\LoggerInterface;

class MyService
{
    use LoggerTrait;

    public function run(): void
    {
        $this->info('Starting job');
    }
}

$service = new MyService();
$service->setLogger($logger);
$service->run();