PHP code example of flotzilla / logger

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

    

flotzilla / logger example snippets


$logger = new Logger();
$channel = new Channel('logs', [
        new FileHandler( new SimpleLineFormatter())
    ]);

$logger->addChannel($channel);

$logger->info('some data');
$logger->error('some message', ['data' => ['name' => 'John', 'job' => 'night watcher']]);

use \flotzilla\Logger\Channel\Channel;
use \flotzilla\Logger\Formatter\JsonFormatter;
use \flotzilla\Logger\Handler\ConsoleHandler;
use \flotzilla\Logger\Handler\FileHandler;
use \flotzilla\Logger\Logger;
use \flotzilla\Logger\LogLevel\LogLevel;
use \flotzilla\Logger\Formatter\PsrFormatter;
use \flotzilla\Logger\Formatter\SimpleLineFormatter;

$logger = new Logger();

//output will be written to /tmp/main-logs.log
$channel = new Channel('logs-all', [
        new FileHandler(new SimpleLineFormatter(), '/tmp', 'main-logs')
    ]);

// set log levels for this channel
$debugChannel = new Channel('debug', [
        new FileHandler(new SimpleLineFormatter(), 'tmp', 'debug'),
        new FileHandler(new PsrFormatter()) // /tmp dir by default
    ], LogLevel::DEBUG, LogLevel::NOTICE);

$criticalChannel = new Channel('critical-only', [
        new FileHandler(new SimpleLineFormatter(), '/tmp', 'critical'),
        new FileHandler(new JsonFormatter(), '/tmp', 'critical')
    ], LogLevel::CRITICAL, LogLevel::EMERGENCY);

// write output to console
$channel = new Channel('logs', [
        new ConsoleHandler(new SimpleLineFormatter())
    ]);

// add channels 
$logger->addChannel($channel);
$logger->addChannel($debugChannel);
$logger->addChannel($criticalChannel);

// or set them all 
$logger->setChannels([$channel, $debugChannel, $criticalChannel]);

// or pass to constructor
$logger = new Logger([$channel, $debugChannel, $criticalChannel]);

// disable channel for writing 
$logger->getChannel('debug')->setEnabled(false);

// set maximal log level for filtering
$logger->getChannel('test')->setMaxLogLevel('error');

// set minimal log level for filtering
$logger->getChannel('test')->setMinLogLevel('info');

$logger = new Logger($channels, 'Y.j.m', new DateTimeZone('Europe/London'));