跳转到内容

Phalcon日志记录器

注意

所有类都以前缀命名Phalcon

日志记录器\AbstractLoggerAbstract

GitHub上的源码

  • 命名空间

    • Phalcon\Logger
  • 使用

    • DateTimeImmutable
    • DateTimeZone
    • Exception
    • Phalcon\Logger\Adapter\AdapterInterface
    • Phalcon\Logger\Exception
  • 继承

  • 实现

抽象日志记录器类

抽象日志记录器类,提供通用功能。提供了格式化程序接口和适配器接口。可以使用内置的AdapterFactory轻松创建适配器。还提供了一个LoggerFactory,允许开发人员从配置文件中创建Logger的新实例或加载它们(参见Phalcon\Config\Config对象)。

@property AdapterInterface[] $adapters @property array $excluded @property int $logLevel @property string $name @property string $timezone

常量

const ALERT = 2;
const CRITICAL = 1;
const CUSTOM = 8;
const DEBUG = 7;
const EMERGENCY = 0;
const ERROR = 3;
const INFO = 6;
const NOTICE = 5;
const WARNING = 4;

属性

/**
 * The adapter stack
 *
 * @var AdapterInterface[]
 */
protected $adapters;

/**
 * The excluded adapters for this log process
 *
 * @var array
 */
protected $excluded;

/**
 * Minimum log level for the logger
 *
 * @var int
 */
protected $logLevel = 8;

/**
 * @var string
 */
protected $name = ;

/**
 * @var DateTimeZone
 */
protected $timezone;

方法

public function __construct( string $name, array $adapters = [], DateTimeZone $timezone = null );
构造函数。

public function addAdapter( string $name, AdapterInterface $adapter ): AbstractLogger;
将适配器添加到堆栈中。对于处理我们使用FIFO

public function excludeAdapters( array $adapters = [] ): AbstractLogger;
排除某些适配器。

public function getAdapter( string $name ): AdapterInterface;
从堆栈中返回一个适配器

public function getAdapters(): array;
返回适配器堆栈数组

public function getLogLevel(): int;
返回日志级别

public function getName(): string;
返回日志记录器的名称

public function removeAdapter( string $name ): AbstractLogger;
从堆栈中移除一个适配器

public function setAdapters( array $adapters ): AbstractLogger;
设置适配器堆栈并覆盖已有的内容

public function setLogLevel( int $level ): AbstractLogger;
设置适配器堆栈并覆盖已有的内容

protected function addMessage( int $level, string $message, array $context = [] ): bool;
将消息添加到每个处理器进行处理

protected function getLevelNumber( mixed $level ): int;
将级别从字符串/单词转换为整数

protected function getLevels(): array;
返回一个包含整数到字符串转换的日志级别数组

日志记录器\适配器\AbstractAdapterAbstract

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Adapter
  • 使用

    • Phalcon\Logger\Exception
    • Phalcon\Logger\Formatter\FormatterInterface
    • Phalcon\Logger\Formatter\Line
    • Phalcon\Logger\Item
  • 继承

  • 实现

    • AdapterInterface

类 AbstractAdapter

@property string $defaultFormatter @property FormatterInterface $formatter @property bool $inTransaction @property array $queue

属性

/**
 * Name of the default formatter class
 *
 * @var string
 */
protected $defaultFormatter = Phalcon\\Logger\Formatter\\Line;

/**
 * Formatter
 *
 * @var FormatterInterface|null
 */
protected $formatter;

/**
 * Tells if there is an active transaction or not
 *
 * @var bool
 */
protected $inTransaction = false;

/**
 * Array with messages queued in the transaction
 *
 * @var array
 */
protected $queue;

方法

public function __destruct();
析构函数清理

@throws Exception

public function __serialize(): array;
防止序列化

public function __unserialize( array $data ): void;
防止反序列化

public function add( Item $item ): AdapterInterface;
将消息添加到队列

public function begin(): AdapterInterface;
开始事务

public function commit(): AdapterInterface;
提交内部事务

public function getFormatter(): FormatterInterface;

public function inTransaction(): bool;
返回记录器当前是否处于活动事务中

abstract public function process( Item $item ): void;
在适配器中处理消息

public function rollback(): AdapterInterface;
回滚内部事务

public function setFormatter( FormatterInterface $formatter ): AdapterInterface;
设置消息格式化程序

protected function getFormattedItem( Item $item ): string;
返回格式化的项目

日志记录器\适配器\AdapterInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Adapter
  • 使用

    • Phalcon\Logger\Formatter\FormatterInterface
    • Phalcon\Logger\Item
  • 继承

  • 实现

Phalcon\Logger\AdapterInterface

Phalcon\Logger适配器的接口

方法

public function add( Item $item ): AdapterInterface;
将消息添加到队列

public function begin(): AdapterInterface;
开始事务

public function close(): bool;
关闭记录器

public function commit(): AdapterInterface;
提交内部事务

public function getFormatter(): FormatterInterface;
返回内部格式化程序

public function inTransaction(): bool;
返回记录器当前是否处于活动事务中

public function process( Item $item ): void;
在适配器中处理消息

public function rollback(): AdapterInterface;
回滚内部事务

public function setFormatter( FormatterInterface $formatter ): AdapterInterface;
设置消息格式化程序

日志记录器\适配器\Noop

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Adapter
  • 使用

    • Phalcon\Logger\Item
  • 继承

    AbstractAdapter

  • 实现

类 Noop

@package Phalcon\Logger\Adapter

方法

public function close(): bool;
关闭流

public function process( Item $item ): void;
处理消息即写入文件

日志记录器\适配器\Stream

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Adapter
  • 使用

    • LogicException
    • Phalcon\Logger\Exception
    • Phalcon\Logger\Item
  • 继承

    AbstractAdapter

  • 实现

Phalcon\Logger\Adapter\Stream

用于将日志存储在纯文本文件中的适配器

$logger = new \Phalcon\Logger\Adapter\Stream('app/logs/test.log');

$logger->log('This is a message');
$logger->log(\Phalcon\Logger::ERROR, 'This is an error');
$logger->error('This is another error');

$logger->close();

@property string $mode @property string $name @property array $options

属性

/**
 * The file open mode. Defaults to 'ab'
 *
 * @var string
 */
protected $mode = ab;

/**
 * Stream name
 *
 * @var string
 */
protected $name;

/**
 * Path options
 *
 * @var array
 */
protected $options;

方法

public function __construct( string $name, array $options = [] );
Stream构造函数。

public function close(): bool;
关闭流

public function getName(): string;
流名称

public function process( Item $item ): void;
处理消息即写入文件

protected function phpFopen( string $filename, string $mode );
@todo 当我们获取 traits 时将被移除

日志记录器\适配器\Syslog

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Adapter
  • 使用

    • LogicException
    • Phalcon\Logger\Item
    • Phalcon\Logger\Logger
  • 继承

    AbstractAdapter

  • 实现

类 Syslog

@property string $defaultFormatter @property int $facility @property string $name @property bool $opened @property int $option

属性

/**
 * @var int
 */
protected $facility = ;

/**
 * @var string
 */
protected $name = ;

/**
 * @var bool
 */
protected $opened = false;

/**
 * @var int
 */
protected $option = ;

方法

public function __construct( string $name, array $options = [] );
Syslog构造函数。

public function close(): bool;
关闭记录器

public function process( Item $item ): void;
处理消息即写入syslog

protected function openlog( string $ident, int $option, int $facility ): bool;
打开系统日志连接

@link https://php.net/manual/en/function.openlog.php

日志记录器\AdapterFactory

GitHub上的源码

  • 命名空间

    • Phalcon\Logger
  • 使用

    • Phalcon\Factory\AbstractFactory
    • Phalcon\Logger\Adapter\AdapterInterface
    • Phalcon\Logger\Exception
  • 继承

    AbstractFactory

  • 实现

用于创建日志记录适配器的工厂

方法

public function __construct( array $services = [] );
AdapterFactory构造函数。

public function newInstance( string $name, string $fileName, array $options = [] ): AdapterInterface;
创建适配器的新实例

protected function getExceptionClass(): string;

protected function getServices(): array;
返回可用的适配器

日志记录器\Enum

GitHub上的源码

  • 命名空间

    • Phalcon\Logger
  • 使用

  • 继承

  • 实现

日志级别枚举常量

常量

const ALERT = 2;
const CRITICAL = 1;
const CUSTOM = 8;
const DEBUG = 7;
const EMERGENCY = 0;
const ERROR = 3;
const INFO = 6;
const NOTICE = 5;
const WARNING = 4;

日志记录器\Exception

GitHub上的源码

  • 命名空间

    • Phalcon\Logger
  • 使用

  • 继承

    \Exception

  • 实现

Phalcon\Logger\Exception

Phalcon\Logger中抛出的异常将使用此类

日志记录器\格式化程序\AbstractFormatterAbstract

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Formatter
  • 使用

    • DateTimeImmutable
    • Phalcon\Logger\Item
    • Phalcon\Support\Helper\Str\AbstractStr
  • 继承

    AbstractStr

  • 实现

    • FormatterInterface

类 AbstractFormatter

属性

/**
 * Default date format
 *
 * @var string
 */
protected $dateFormat = c;

/**
 * @var string
 */
protected $interpolatorLeft = %;

/**
 * @var string
 */
protected $interpolatorRight = %;

方法

public function getDateFormat(): string;
public function setDateFormat( string $format ): void;

protected function getFormattedDate( Item $item ): string;
返回日期格式化以供记录器使用。

protected function getInterpolatedMessage( Item $item, string $message ): string;

日志记录器\格式化程序\FormatterInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Formatter
  • 使用

    • Phalcon\Logger\Item
  • 继承

  • 实现

Phalcon\Logger\FormatterInterface

此接口必须由Phalcon\Logger中的格式化程序实现

方法

public function format( Item $item ): string;
应用格式到项目

日志记录器\格式化程序\Json

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Formatter
  • 使用

    • JsonException
    • Phalcon\Logger\Item
  • 继承

    AbstractFormatter

  • 实现

使用JSON编码格式化消息

方法

public function __construct( string $dateFormat = string, string $interpolatorLeft = string, string $interpolatorRight = string );
Json构造函数。

public function format( Item $item ): string;
在发送到内部记录器之前应用格式到消息

日志记录器\格式化程序\Line

GitHub上的源码

  • 命名空间

    • Phalcon\Logger\Formatter
  • 使用

    • Exception
    • Phalcon\Logger\Item
  • 继承

    AbstractFormatter

  • 实现

类 Line

属性

/**
 * Format applied to each message
 *
 * @var string
 */
protected $format;

方法

public function __construct( string $format = string, string $dateFormat = string, string $interpolatorLeft = string, string $interpolatorRight = string );
Line构造函数。

public function format( Item $item ): string;
在发送到内部记录器之前应用格式到消息

public function getFormat(): string;
返回应用于每条消息的格式

public function setFormat( string $format ): Line;
设置应用于每条消息的格式

日志记录器\Item

GitHub上的源码

  • 命名空间

    • Phalcon\Logger
  • 使用

    • DateTimeImmutable
  • 继承

  • 实现

Phalcon\Logger\Item

表示日志记录事务中的每个项目

@property array $context @property string $message @property int $level @property string $levelName @property DateTimeImmutable $datetime

属性

/**
 * @var array
 */
protected $context;

/**
 * @var DateTimeImmutable
 */
protected $dateTime;

/**
 * @var string
 */
protected $message;

/**
 * @var int
 */
protected $level;

/**
 * @var string
 */
protected $levelName;

方法

public function __construct( string $message, string $levelName, int $level, DateTimeImmutable $dateTime, array $context = [] );
Item构造函数。

public function getContext(): array;
public function getDateTime(): DateTimeImmutable;
public function getLevel(): int;
public function getLevelName(): string;
public function getMessage(): string;

日志记录器\Logger

GitHub上的源码

  • 命名空间

    • Phalcon\Logger
  • 使用

    • Exception
    • Phalcon\Logger\Exception
  • 继承

    AbstractLogger

  • 实现

    • LoggerInterface

Phalcon日志记录器。

带有各种适配器和格式化程序的日志记录器。提供了一个格式化程序接口和适配器接口。可以使用内置的AdapterFactory轻松创建适配器。还提供了一个LoggerFactory,允许开发人员从配置文件中创建Logger的新实例或加载它们(参见Phalcon\Config\Config对象)。

方法

public function alert( string $message, array $context = [] ): void;
必须立即采取行动。

示例:整个网站宕机,数据库不可用等。这应该触发 SMS 警报并将你叫醒。

public function critical( string $message, array $context = [] ): void;
严重条件。

示例:应用程序组件不可用,意外异常。

public function debug( string $message, array $context = [] ): void;
详细的调试信息。

public function emergency( string $message, array $context = [] ): void;
系统无法使用。

public function error( string $message, array $context = [] ): void;
不需要立即处理但在通常情况下应被记录和监控的运行时错误。

public function info( string $message, array $context = [] ): void;
有趣的事件。

示例:用户登录,SQL 日志。

public function log( mixed $level, string $message, array $context = [] ): void;
使用任意级别进行记录。

public function notice( string $message, array $context = [] ): void;
正常但重要的事件。

public function warning( string $message, array $context = [] ): void;
并非错误的异常情况。

示例:使用已弃用的API,API使用不当,虽然不一定错误但不理想的情况。

日志\日志工厂

GitHub上的源码

  • 命名空间

    • Phalcon\Logger
  • 使用

    • DateTimeZone
    • Phalcon\Config\ConfigInterface
    • Phalcon\Factory\AbstractConfigFactory
  • 继承

    AbstractConfigFactory

  • 实现

用于创建日志对象的工厂

属性

/**
 * @var AdapterFactory
 */
private $adapterFactory;

方法

public function __construct( AdapterFactory $factory );

public function load( mixed $config ): Logger;
从Config对象创建实例的工厂

public function newInstance( string $name, array $adapters = [], DateTimeZone $timezone = null ): Logger;
返回一个日志对象

protected function getArrVal( array $collection, mixed $index, mixed $defaultValue = null ): mixed;
@todo 当我们获得特性时,删除这个

protected function getExceptionClass(): string;

日志\日志接口Interface

GitHub上的源码

  • 命名空间

    • Phalcon\Logger
  • 使用

    • Phalcon\Logger\Adapter\AdapterInterface
  • 继承

  • 实现

基于 Phalcon 的日志对象接口。

方法

public function alert( string $message, array $context = [] ): void;
必须立即采取行动。

示例:整个网站宕机,数据库不可用等。这应该触发 SMS 警报并将你叫醒。

public function critical( string $message, array $context = [] ): void;
严重条件。

示例:应用程序组件不可用,意外异常。

public function debug( string $message, array $context = [] ): void;
详细的调试信息。

public function emergency( string $message, array $context = [] ): void;
系统无法使用。

public function error( string $message, array $context = [] ): void;
不需要立即处理但在通常情况下应被记录和监控的运行时错误。

public function getAdapter( string $name ): AdapterInterface;
从堆栈中返回一个适配器

public function getAdapters(): array;
返回适配器堆栈数组

public function getLogLevel(): int;
返回日志级别

public function getName(): string;
返回日志记录器的名称

public function info( string $message, array $context = [] ): void;
有趣的事件。

示例:用户登录,SQL 日志。

public function log( mixed $level, string $message, array $context = [] ): void;
使用任意级别进行记录。

public function notice( string $message, array $context = [] ): void;
正常但重要的事件。

public function warning( string $message, array $context = [] ): void;
正常但重要的事件。

无噪 Logo
无噪文档
25 年 6 月翻译
文档源↗