Giter Site home page Giter Site logo

log's Introduction

Stefna Logger

Stefna\Logger\ManagerInterface

Class that handles creation of logger classes.

This is where you configure your main logger.

Methods

  • pushProcessor(callable $callback, string $channel = null): Manager If channel is set the process will only be applied to that channel. If no instance of channel exists the process will be silently ignored

Stefna\Logger\MonologManager

Manager that uses Monolog as the main logger.

It has one extra method on the manager and that's pushHandler that is so that you can configure custom handlers for select channels

  • pushHandler(HandlerInterface $handler, $channel = null): Manager If channel is set the handler will only be applied to that channel. If no instance of channel exists the process will be silently ignored

Stefna\Logger\Logger Methods

setManager

Set log manager if this is not done all calls to getLogger will return a NullLogger

getManager

Get manager to add process or handler to main logger

setChannelConfig

Add a channel specific config

setGlobalConfig

Add multiple config options with one call

getLogger

Retrives a logging instance for channel

This method will create the logger if it don't exists and it check for configs for the specified channel

Example

<?php declare(strict_types=1);

use Stefna\Logger\Filters\MinLogLevelFilter;
use Stefna\Logger\Filters\CallbackFilter;
use Stefna\Logger\Filters\TimeLimitFilter;

$monolog = new \Monolog\Logger('main-channel', $handlers, $proccess);
$manager = new \Stefna\Logger\MonologManager($monolog, new \Stefna\Logger\Filters\FilterFactory());

\Stefna\Logger\Logger::setManager($manager);

$filters = [
	[MinLogLevelFilter::KEY, ['level' => \Psr\Log\LogLevel::ALERT]],
	[
		CallbackFilter::KEY,
		[
			'callback' => function(string $level, string $message, array $context) {
				return isset($context['exception']);
			},
		],
	],
	[TimeLimitFilter::KEY, ['cache' => $simpleCache, 'interval' => new DateInterval('P1D')]]
];

\Stefna\Logger\Logger::setChannelConfig(
    'test-channel',
    new Stefna\Logger\Config\Config('test-channel', $filters[[, $proccess], $handlers])
);

$logger = \Stefna\Logger\Logger::getLogger('test-channel');

Setup of a crash logger

<?php declare(strict_types=1);

$logger = new SimpleFileLogger('path/to/save/crash.log');
//or
$logger = new SystemErrorLogger();

$crashLogger = new BufferFilterLogger(
    $logger,
    new ActivateLevelFilter(LogLevel::ERROR)
);

// Will not add to log file
$crashLogger->debug('test');

// Will add all message prior and after this to the log
// This is so that we get a complete story of what happened during the execution
$crashLogger->error('error');

Setup debouncer filter

This filter is meant to be used to prevent logs from being filed with the same errors

Setup

<?php declare(strict_types=1);

use Stefna\Logger\Filters\DebounceFilter;
use Stefna\Logger\Logger\FilterLogger

$debounceFilter = new DebounceFilter(function($level, $message, $context) use ($cache) {
	// create cache key
	$key = md5(serialize([$message, $context]));
	if ($cache->has($key)) {
		return false;
	}
	$debounceInterval = $context[DebounceFilter::DEBOUNCE_INTERVAL];
	$cache->set($key, true, new DateInterval($debounceInterval));
	return true;
});

$logger = new FilterLogger($mainLogger, $debounceFilter);
$logger->alert('Db connect error', [
	DebounceFilter::DEBOUNCE_INTERVAL => 'PT1H', // only log once an hour
]);

log's People

Contributors

sunkan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.