PHP code example of flytachi / winter-logger

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

    

flytachi / winter-logger example snippets


use Monolog\Level;
use Flytachi\Winter\Logger\LoggerManager;
use Flytachi\Winter\Logger\Context\ProcessContext;   // FPM / CLI
// use Flytachi\Winter\Logger\Context\CoroutineContext; // Swoole

$manager = new LoggerManager(
    contextStorage: new ProcessContext(),
    channels: [
        'http' => [
            'level'        => Level::Info,
            'format'       => 'line',       // 'line' | 'json'
            'output'       => 'stderr',     // 'stdout' | 'stderr' | 'syslog' | 'file' | 'null'
            'file_path'    => null,
            'file_max'     => 30,
            'syslog_ident' => 'winter',
        ],
        'cli' => [
            'level'        => Level::Debug,
            'format'       => 'line',
            'output'       => 'stdout',
            'file_path'    => null,
            'file_max'     => 30,
            'syslog_ident' => 'winter',
        ],
    ],
);

use Flytachi\Winter\Logger\LoggerFactory;

LoggerFactory::setManager($manager);

use Flytachi\Winter\Logger\LoggerFactory;

// Per-class logger — Java-style:
LoggerFactory::getLogger(UserService::class, 'http')->info('user created', ['id' => 42]);
LoggerFactory::getLogger($this, 'cli')->debug('job started');

// Raw channel:
LoggerFactory::channel('http')->warning('rate limit hit');

// Bound context — fields appear in every subsequent call:
$log = LoggerFactory::getLogger(self::class, 'http')->withContext(['request_id' => $id]);
$log->info('processing');
$log->info('done');

// At request start (FPM middleware / Swoole onRequest):
$manager->contextStorage()->set('request_id', $requestId);
$manager->contextStorage()->set('user_id',    $userId);

// At request end — must clear in long-running processes to prevent leaks:
$manager->contextStorage()->clear();

use Flytachi\Winter\Logger\Processor\SensitiveMaskingProcessor;

$monolog = $manager->channel('http')->monolog();
$monolog->pushProcessor(new SensitiveMaskingProcessor(['my_secret']));

[2024-01-01 12:00:00] [INFO ] [UserService]: user created {"id":42,"class":"App\\Service\\UserService"}
[2024-01-01 12:00:00] [WARN ] [http]: rate limit hit