CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 商品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与进来
    • 问题 (Github)
    • 烘焙坊
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 时事通讯
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
    • 数据源
    • 错误
    • 事件
    • 表单
    • Http
    • 国际化
    • 日志
      • 引擎
      • 格式化器
    • 邮件
    • 网络
    • ORM
    • 路由
    • 测试套件
    • 实用程序
    • 验证
    • 视图

类 Log

将消息记录到配置的日志适配器。可以使用 Cake Logs 的方法配置一个或多个适配器。如果你没有配置任何适配器,并且写入 Log,这些消息将被忽略。

配置日志适配器

你可以在应用程序的 config/app.php 文件中配置日志适配器。一个示例配置如下

Log::setConfig('my_log', ['className' => 'FileLog']);

你可以将 className 定义为任何完全限定的类名,或使用简短的类名来使用 App\Log\Engine 和 Cake\Log\Engine 命名空间中的记录器。你也可以使用插件简写来使用插件提供的日志记录类。

日志适配器需要实现 Psr\Log\LoggerInterface,并且有一个内置的基类 (Cake\Log\Engine\BaseLog) 可用于自定义记录器。

除了 className 键之外,所有其他配置值都将作为数组传递给日志适配器的构造函数。

日志级别

在配置记录器时,你可以设置记录器将处理哪些级别。例如,这使你能够在生产中禁用调试消息

Log::setConfig('default', [
    'className' => 'File',
    'path' => LOGS,
    'levels' => ['error', 'critical', 'alert', 'emergency']
]);

上面的记录器只会记录错误消息或更高级别的消息。任何其他日志消息将被丢弃。

日志范围

在配置记录器时,你可以定义记录器所针对的活动范围。如果已定义,只有列出的范围将由记录器处理。如果你没有定义任何范围,适配器将捕获与处理级别匹配的所有范围。

Log::setConfig('payments', [
    'className' => 'File',
    'scopes' => ['payment', 'order']
]);

上面的记录器只会捕获在 payment 和 order 范围内进行的日志条目。所有其他范围(包括未定义的范围)将被忽略。

写入日志

你可以使用 Log::write() 写入日志。有关更多信息,请参阅其文档。

日志级别

默认情况下,Cake Log 支持 RFC 5424 中定义的所有日志级别。在记录消息时,你可以使用命名方法,也可以使用 write() 中的正确常量

Log::error('Something horrible happened');
Log::write(LOG_ERR, 'Something horrible happened');

日志范围

在记录消息和配置日志适配器时,你可以指定记录器将处理的“范围”。你可以将范围视为应用程序中的子系统,它们可能需要不同的日志记录设置。例如,在电子商务应用程序中,你可能希望以不同于应用程序其他部分的方式处理购物车和订购子系统中的记录错误。通过使用范围,你可以控制应用程序每个部分的日志记录,还可以使用标准日志级别。

命名空间: Cake\Log

属性摘要

  • $_config protected static
    array<string|int, array<string, mixed>>

    配置集。

  • $_dirtyConfig protected static
    bool

    用于跟踪配置是否已更改的内部标志。

  • $_dsnClassMap protected static
    array<string, string>

    一个将 url 方案映射到完全限定的日志引擎类名的数组

  • $_levelMap protected static
    array<string, int>

    RFC 5424 中详细说明的日志级别 https://tools.ietf.org/html/rfc5424

  • $_levels protected static
    list<string>

    处理的日志级别

  • $_registry protected static
    Cake\Log\LogEngineRegistry

    LogEngineRegistry 类

方法摘要

  • alert() public static

    用于记录警报消息的便利方法

  • configured() public static

    返回一个包含命名配置的数组

  • critical() public static

    用于记录严重消息的便利方法

  • debug() public static

    用于记录调试消息的便利方法

  • drop() public static

    删除构造的适配器。

  • emergency() public static

    用于记录紧急消息的便利方法

  • engine() public static

    获取日志记录引擎。

  • error() public static

    用于记录错误消息的便利方法

  • getConfig() public static

    读取现有配置。

  • getConfigOrFail() public static

    读取特定键的现有配置。

  • getDsnClassMap() public static

    返回此类的 DSN 类映射。

  • getRegistry() protected static

    如果不存在,则创建注册表,如果未加载配置,则创建所有定义的日志记录适配器。

  • info() public static

    用于记录信息消息的便利方法

  • levels() public static

    获取日志级别

  • notice() public static

    用于记录通知消息的便利方法

  • parseDsn() public static

    将 DSN 解析为有效的连接配置

  • reset() public static

    重置所有已连接的记录器。这在更改日志记录配置或在测试时(你想要重置 Log 类的内部状态时)非常有用。

  • setConfig() public static

    此方法可用于为应用程序定义日志记录适配器或读取现有配置。

  • setDsnClassMap() public static

    更新此类的 DSN 类映射。

  • warning() public static

    用于记录警告消息的便利方法

  • write() public static

    将给定的消息和类型写入所有配置的日志适配器。配置的适配器将同时传递 $level 和 $message 变量。$level 是以下字符串/值之一。

方法详细说明

alert() ¶ public static

alert(Stringable|string $message, array|string $context = []): bool

用于记录警报消息的便利方法

参数
Stringable|string $message

日志消息

array|string $context optional

用于记录消息的附加数据。特殊 scope 键可以传递用于进一步过滤要使用的日志引擎。如果传递字符串或按数字索引的数组,它将被视为 scope 键。有关日志范围的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool

configured() ¶ public static

configured(): list<string>

返回一个包含命名配置的数组

返回值
list<string>

critical() ¶ public static

critical(Stringable|string $message, array|string $context = []): bool

用于记录严重消息的便利方法

参数
Stringable|string $message

日志消息

array|string $context optional

用于记录消息的附加数据。特殊 scope 键可以传递用于进一步过滤要使用的日志引擎。如果传递字符串或按数字索引的数组,它将被视为 scope 键。有关日志范围的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool

debug() ¶ public static

debug(Stringable|string $message, array|string $context = []): bool

用于记录调试消息的便利方法

参数
Stringable|string $message

日志消息

array|string $context optional

用于记录消息的附加数据。特殊 scope 键可以传递用于进一步过滤要使用的日志引擎。如果传递字符串或按数字索引的数组,它将被视为 scope 键。有关日志范围的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool

drop() ¶ public static

drop(string $config): bool

删除构造的适配器。

如果你希望修改现有配置,你应该删除它,更改配置,然后重新添加它。

如果实现的对象支持 $_registry 对象,则命名配置也将从注册表中卸载。

参数
string $config

你希望删除的现有配置。

返回值
bool

emergency() ¶ public static

emergency(Stringable|string $message, array|string $context = []): bool

用于记录紧急消息的便利方法

参数
Stringable|string $message

日志消息

array|string $context optional

用于记录消息的附加数据。特殊 scope 键可以传递用于进一步过滤要使用的日志引擎。如果传递字符串或按数字索引的数组,它将被视为 scope 键。有关日志范围的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool

engine() ¶ public static

engine(string $name): Psr\Log\LoggerInterface|null

获取日志记录引擎。

参数
string $name

要获取的配置适配器的键名。

返回值
Psr\Log\LoggerInterface|null

error() ¶ public static

error(Stringable|string $message, array|string $context = []): bool

用于记录错误消息的便利方法

参数
Stringable|string $message

日志消息

array|string $context optional

用于记录消息的附加数据。特殊 scope 键可以传递用于进一步过滤要使用的日志引擎。如果传递字符串或按数字索引的数组,它将被视为 scope 键。有关日志范围的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool

getConfig() ¶ public static

getConfig(string $key): mixed|null

读取现有配置。

参数
string $key

配置名称。

返回值
mixed|null

getConfigOrFail() ¶ public static

getConfigOrFail(string $key): mixed

读取特定键的现有配置。

此键的配置值必须存在,绝不能为 null。

参数
string $key

配置名称。

返回值
mixed
抛出
InvalidArgumentException
如果值不存在。

getDsnClassMap() ¶ public static

getDsnClassMap(): array<string, class-string>

返回此类的 DSN 类映射。

返回值
array<string, class-string>

getRegistry() ¶ protected static

getRegistry(): Cake\Log\LogEngineRegistry

如果不存在,则创建注册表,如果未加载配置,则创建所有定义的日志记录适配器。

返回值
Cake\Log\LogEngineRegistry

info() ¶ public static

info(Stringable|string $message, array|string $context = []): bool

用于记录信息消息的便利方法

参数
Stringable|string $message

日志消息

array|string $context optional

用于记录消息的附加数据。可以传递特殊的 scope 键以用于进一步过滤要使用的日志引擎。如果传递字符串或数字索引的数组,它将被视为 scope 键。有关日志作用域的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool

levels() ¶ public static

levels(): list<string>

获取日志级别

调用此方法以获取当前级别配置。

返回值
list<string>

notice() ¶ public static

notice(Stringable|string $message, array|string $context = []): bool

用于记录通知消息的便利方法

参数
Stringable|string $message

日志消息

array|string $context optional

用于记录消息的附加数据。特殊 scope 键可以传递用于进一步过滤要使用的日志引擎。如果传递字符串或按数字索引的数组,它将被视为 scope 键。有关日志范围的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool

parseDsn() ¶ public static

parseDsn(string $dsn): array<string, mixed>

将 DSN 解析为有效的连接配置

此方法允许使用类似于 PEAR::DB 中使用的格式设置 DSN。以下是它的使用示例

$dsn = 'mysql://user:pass@localhost/database?';
$config = ConnectionManager::parseDsn($dsn);

$dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS';
$config = Log::parseDsn($dsn);

$dsn = 'smtp://user:secret@localhost:25?timeout=30&client=null&tls=null';
$config = Email::parseDsn($dsn);

$dsn = 'file:///?className=\My\Cache\Engine\FileEngine';
$config = Cache::parseDsn($dsn);

$dsn = 'File://?prefix=myapp_cake_translations_&serialize=true&duration=+2 minutes&path=/tmp/persistent/';
$config = Cache::parseDsn($dsn);

对于所有类,scheme 的值都设置为 className 的值,除非它们已被另行指定。

请注意,查询字符串参数也会被解析并设置为返回配置中的值。

参数
string $dsn

要转换为配置数组的 DSN 字符串

返回值
array<string, mixed>
抛出
InvalidArgumentException
如果没有传递字符串,或者传递了无效字符串

reset() ¶ public static

reset(): void

重置所有已连接的记录器。这在更改日志记录配置或在测试时(你想要重置 Log 类的内部状态时)非常有用。

重置配置的日志适配器,以及任何自定义日志级别。这也会清除配置数据。

返回值
void

setConfig() ¶ public static

setConfig(array<string, mixed>|string $key, Psr\Log\LoggerInterfaceClosure|array<string, mixed>|null $config = null): void

此方法可用于为应用程序定义日志记录适配器或读取现有配置。

要更改适配器的运行时配置,首先删除适配器,然后重新配置它。

日志记录器将在写入第一个日志消息之前不会被构造。

用法

设置缓存引擎。

Log::setConfig('default', $settings);

注入已构造的适配器

Log::setConfig('default', $instance);

使用工厂函数获取适配器

Log::setConfig('default', function () { return new FileLog(); });

一次配置多个适配器

Log::setConfig($arrayOfConfig);
参数
array<string, mixed>|string $key

日志记录器配置的名称,或多个配置的数组。

Psr\Log\LoggerInterfaceClosure|array<string, mixed>|null $config optional

适配器的名称 => 配置数据的数组。

返回值
void
抛出
BadMethodCallException
尝试修改现有配置时。

setDsnClassMap() ¶ public static

setDsnClassMap(array<string, string> $map): void

更新此类的 DSN 类映射。

参数
array<string, string> $map

要应用的类映射的添加/编辑。

返回值
void

warning() ¶ public static

warning(Stringable|string $message, array|string $context = []): bool

用于记录警告消息的便利方法

参数
Stringable|string $message

日志消息

array|string $context optional

用于记录消息的附加数据。特殊 scope 键可以传递用于进一步过滤要使用的日志引擎。如果传递字符串或按数字索引的数组,它将被视为 scope 键。有关日志范围的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool

write() ¶ public static

write(string|int $level, Stringable|string $message, array|string $context = []): bool

将给定的消息和类型写入所有配置的日志适配器。配置的适配器将同时传递 $level 和 $message 变量。$level 是以下字符串/值之一。

级别

  • LOG_EMERG => 'emergency',
  • LOG_ALERT => 'alert',
  • LOG_CRIT => 'critical',
  • LOG_ERR => 'error',
  • LOG_WARNING => 'warning',
  • LOG_NOTICE => 'notice',
  • LOG_INFO => 'info',
  • LOG_DEBUG => 'debug',

基本用法

将“警告”消息写入日志

Log::write('warning', 'Stuff is broken here');

使用作用域

在写入日志消息时,可以为消息定义一个或多个作用域。这允许您根据应用程序部分/功能以不同方式处理消息。

Log::write('warning', 'Payment failed', ['scope' => 'payment']);

在配置日志记录器时,可以配置特定日志记录器将处理的作用域。使用作用域时,必须确保消息的级别和消息的作用域与日志记录器的定义级别和作用域相交。

未处理的日志消息

如果没有任何配置的日志记录器可以处理日志消息(由于级别或作用域限制),则日志消息将被忽略并静默删除。可以通过检查 write() 的返回值来检查是否发生了这种情况。如果为 false,则表示消息未被处理。

参数
string|int $level

正在写入的消息的严重级别。该值必须是整数或字符串,与已知级别匹配。

Stringable|string $message

要记录的消息内容

array|string $context optional

用于记录消息的附加数据。特殊 scope 键可以传递用于进一步过滤要使用的日志引擎。如果传递字符串或按数字索引的数组,它将被视为 scope 键。有关日志范围的更多信息,请参阅 {@link \Cake\Log\Log::setConfig()}。

返回值
bool
抛出
InvalidArgumentException
如果传递了无效级别。

属性详细信息

$_config ¶ protected static

配置集。

类型
array<string|int, array<string, mixed>>

$_dirtyConfig ¶ protected static

用于跟踪配置是否已更改的内部标志。

类型
bool

$_dsnClassMap ¶ protected static

一个将 url 方案映射到完全限定的日志引擎类名的数组

类型
array<string, string>

$_levelMap ¶ protected static

RFC 5424 中详细说明的日志级别 https://tools.ietf.org/html/rfc5424

类型
array<string, int>

$_levels ¶ protected static

处理的日志级别

类型
list<string>

$_registry ¶ protected static

LogEngineRegistry 类

类型
Cake\Log\LogEngineRegistry
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与进来
  • 问题 (Github)
  • 烘焙坊
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 时事通讯
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成