PHP code example of contaoblackforest / contao-logger

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

    

contaoblackforest / contao-logger example snippets


global $container;

/** @var \Psr\Log\LoggerInterface */
$logger = $container['logger'];
$logger->emergency('Some extreme critical message');

global $container;

// receive default log level
$level = $container['logger.default.level'];

// change default log level
$container['logger.default.level'] = \Psr\Log\LogLevel::WARNING;

global $container;

// receive the default log handlers array (its an ArrayObject instance)
$handlers = $container['logger.default.handlers'];

// remove the contao syslog handler
foreach ($handlers as $index => $serviceKey) {
	if ($serviceKey == 'logger.handler.contao') {
		unset($handlers[$index]);
		break;
	}
}

// add a custom handler
$container['logger.handler.custom'] = function($container) {
	$factory = $container['logger.factory.handler.stream'];
	// store in /var/log/critical.log
	return $factory('/var/log/critical.log', \Psr\Log\LogLevel::CRITICAL);
}
$handlers->append('logger.handler.custom');

global $container;

// register a handler
$container['logger.handler.custom'] = function($container) {
	$factory = $container['logger.factory.handler.stream'];
	// store in system/logs/critical.log
	return $factory('critical.log', \Monolog\Logger::CRITICAL);
}

// register your logger
$container['logger.custom'] = function($container) {
	// using the logger factory
	$factory = $container['logger.factory'];
	$logger = $factory('contao', array('logger.handler.custom'));

	return $logger;
};

// receive your logger
$logger = $container['logger.custom'];

/**
 * @param int    $level    The minimum logging level at which this handler will be triggered
 * @param bool   $bubble   Whether the messages that are handled can bubble up the stack or not
 * @param string $function The function name in the contao syslog (use channel name by default)
 * @param string $action   The action name in the contao syslog (use simplified log level name by default)
 */
function($level = null, $bubble = true, $function = null, $action = null)

/**
 * @param string|callable|Monolog\Handler\HandlerInterface $handler         Service name, callable or handler object.
 * @param int                                              $bufferSize      How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
 * @param int                                              $level           The minimum logging level at which this handler will be triggered
 * @param bool                                             $bubble          Whether the messages that are handled can bubble up the stack or not
 * @param bool                                             $flushOnOverflow If true, the buffer is flushed when the max size has been reached, by default oldest entries are discarded
 */
function function($handler, $bufferSize = 0, $level = null, $bubble = true, $flushOnOverflow = false)

/**
 * @param int  $level  The minimum logging level at which this handler will be triggered
 * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
 */
function function($level = null, $bubble = true)

/**
 * @param string|callable|Monolog\Handler\HandlerInterface $handler            Service name, callable or handler object.
 * @param int|ActivationStrategyInterface                  $activationStrategy The minimum logging level at which this handler will be triggered
 * @param int                                              $bufferSize         How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
 * @param bool                                             $bubble             Whether the messages that are handled can bubble up the stack or not
 * @param bool                                             $stopBuffering      Whether the handler should stop buffering after being triggered (default true)
 */
function function($handler, $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true)

/**
 * @param int  $level  The minimum logging level at which this handler will be triggered
 * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
 */
function function($level = null, $bubble = true)

/**
 * @param array $handlers List of services, callbacks or handlers.
 * @param bool  $bubble   Whether the messages that are handled can bubble up the stack or not
 */
function function(array $handlers, $bubble = true)

/**
 * @param string $filename Absolute filename or single name (stored in system/logs/)
 * @param int    $maxFiles The maximal amount of files to keep (0 means unlimited)
 * @param int    $level  The minimum logging level at which this handler will be triggered
 * @param bool   $bubble Whether the messages that are handled can bubble up the stack or not
 */
function function($filename, $maxFiles = null, $level = null, $bubble = true)

/**
 * A handler using swift to send entries as emails.
 *
 * @param string $to      The email recipient address
 * @param string $subject The email subject
 * @param string $from    The email sender address
 * @param int    $level   The minimum logging level at which this handler will be triggered
 * @param bool   $bubble  Whether the messages that are handled can bubble up the stack or not
 */
function function($to = null, $subject = null, $from = null, $level = null, $bubble = true)

/**
 * @param string $uri    Stream uri
 * @param int    $level  The minimum logging level at which this handler will be triggered
 * @param bool   $bubble Whether the messages that are handled can bubble up the stack or not
 */
function function($uri, $level = null, $bubble = true)

/**
 * @param string $name     The channel name
 * @param array  $handlers List of services or handlers.
 */
function function($name, array $handlers = array())