Skip to content

flotzilla/logger

Repository files navigation

MIT License

Logger

PSR-3 logger interface implementation inspired by monolog

Requirements

php > 7.3

Install

via Composer

$ composer require flotzilla/logger

Description

Basic usage

$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']]);

Advanced usage

Initialization

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]);

Channels

// 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');

Set custom datetime format and timezone

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

Testing

$ composer test
$ composer test_mock

Benchmarks

$ composer bench

License

The MIT License (MIT). Please see License File for more information.

About

PSR-3 logger interface implementation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages