bit3/contao-logger

此包已被废弃,不再维护。作者建议使用 ContaoBlackForest/contao-logger 包。

Contao开源CMS的PSR-3日志桥接器

安装数: 1,237

依赖项: 1

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 1

类型:contao模块

2.0.1 2015-05-28 11:21 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:25:39 UTC


README

Build Status

Contao开源CMS的日志桥接器

此桥接器为 Contao开源CMS 提供PSR-3日志支持。日志通过 依赖注入容器 可用。

默认情况下,日志使用两个处理器。

  • 一个contao系统日志处理器,将日志条目写入系统日志数据库。
  • 和一个流处理器,将日志条目写入 system/logs/contao.log

默认情况下使用 Monolog 作为实现,但它被设计为可替换为任何PSR-3兼容的日志实现。

访问和使用日志记录器

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;

定义默认日志处理器

默认日志处理器存储在 $container['logger.default.handlers'] 中,包含处理器服务的列表。

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

参考

服务

$container['logger.default.level']

(int) 默认日志级别,默认:Psr\Log\LogLevel::INFO

$container['logger.default.level.contao']

(int) 默认日志级别,从 $container['logger.default.level'] 继承

$container['logger.default.level.buffer']

(int) 默认日志级别,从 $container['logger.default.level'] 继承

$container['logger.default.level.chromePhp']

(int) 默认日志级别,从 $container['logger.default.level'] 继承

$container['logger.default.level.firePhp']

(int) 默认日志级别,从 $container['logger.default.level'] 继承

$container['logger.default.level.rotatingFile']

(int) 默认日志级别,从 $container['logger.default.level'] 继承

$container['logger.default.level.mail']

(int) 默认日志级别,默认:Psr\Log\LogLevel::ERROR

$container['logger.default.level.stream']

(int) 默认日志级别,从 $container['logger.default.level'] 继承

$container['logger.default.rotation']

(int) 日志轮转的天数,默认:28

$container['logger.handler.contao']

(Monolog\Handler\HandlerInterface|Logger\ContaoHandler) 默认Contao系统日志处理器

$container['logger.handler.stream']

(Monolog\Handler\HandlerInterface|Monolog\Handler\RotatingFileHandler) 默认轮转日志文件处理器(系统目录/logs/contao-Y-m-d.log)

$container['logger.default.handlers']

(ArrayObject) 默认日志处理器的列表

$container['logger']

(Psr\Log\LoggerInterface|Monolog\Logger) 默认日志记录器

工厂

$container['logger.factory.handler.contao']

/**
 * @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)

$container['logger.factory.handler.buffer']

/**
 * @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)

$container['logger.factory.handler.chromePhp']

/**
 * @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)

$container['logger.factory.handler.fingersCrossed']

/**
 * @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)

$container['logger.factory.handler.firePhp']

/**
 * @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)

$container['logger.factory.handler.group']

/**
 * @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)

$container['logger.factory.handler.rotatingFile']

/**
 * @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)

$container['logger.factory.handler.mail']

/**
 * 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)

$container['logger.factory.handler.stream']

/**
 * @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)

$container['logger.factory']

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