PhalconPHP 的 MVS Logger 包

4.0.0 2021-06-04 19:32 UTC

This package is not auto-updated.

Last update: 2024-09-22 09:24:31 UTC


README

Phalcon 4.* 日志扩展和适配器。符合 PSR-3 标准。

要求
需要 PHP 7.2+ 和 Phalcon 4.0+

功能

  • 为 Phalcon Logger 添加 EmailAdapter,通过 sendmail 或 SMTP 发送错误邮件
  • 为 Phalcon Logger 添加 DbAdapter,将错误记录到数据库
  • DbAdapter 和 EmailAdapter 都接受一个 CONFIG_MIN_LOG_LEVEL 配置来指定记录到适配器的最小日志级别
  • 添加静态日志方法,而不是需要将 Phalcon Logger 注入

安装 composer require maxvaluesoftware\logger "^4.0

使用方法

EmailAdapter
邮件适配器可以接受两种邮件发送者;默认的 sendmail 或 SMTP。每种都有自己的配置。邮件发送者配置是公共常量,命名为 CONFIG_

SendmailMailer
配置:

SendmailMailer::CONFIG_RECIPIENT => 'recipient@gmail.com',
SendmailMailer::CONFIG_SENDER_EMAIL => 'sender@gmail.com',
SendmailMailer::CONFIG_SENDER_NAME => 'Sender Name',
SendmailMailer::CONFIG_HEADERS => ['X-Mailer' => PHP_VERSION], // Array of optional headers

示例: $options = []; $adapters[] = new EmailAdapter($configs); $logger = (new Logger\LoggerFactory(new Logger\AdapterFactory()))->newInstance('logger', $adapters);

SmtpMailer
配置:

SmtpMailer::CONFIG_RECIPIENT => ['recipient1@gmail.com', 'recipient2@gmail.com'],
SmtpMailer::CONFIG_SMTP_HOST => 'smtp.gmail.com',
SmtpMailer::CONFIG_SMTP_PORT => 587,
SmtpMailer::CONFIG_SMTP_USER => 'smtp username/email',
SmtpMailer::CONFIG_SMTP_PASS => 'smtp password,
SmtpMailer::CONFIG_SENDER_EMAIL => 'sender@gmail.com',
SmtpMailer::CONFIG_SENDER_NAME => 'Sender Name',
SmtpMailer::CONFIG_SMTP_ENCRYPTION => 'tls', // tls, ssl or null  

$options = []; $adapters[] = new EmailAdapter($configs); $logger = (new Logger\LoggerFactory(new Logger\AdapterFactory()))->newInstance('logger', $adapters);

DbAdapter
数据库适配器允许将日志记录到配置的 Phalcon 数据库表中。

配置

DbAdapter::CONFIG_TABLE_NAME => 'logs',
DbAdapter::CONFIG_COLUMN_ERROR_LEVEL => 'error_level',
DbAdapter::CONFIG_COLUMN_MESSAGE => 'error_msg',
DbAdapter::CONFIG_COLUMN_NAME => 'name',
DbAdapter::CONFIG_COLUMN_TIMESTAMP => 'timesetamp',
DbAdapter::CONFIG_DATE_FORMAT => 'Y-m-d H:i:s',
DbAdapter::CONFIG_MIN_LOG_LEVEL => Logger::ERROR,

示例: `$adapters[] = new DbAdapter($config); $logger = (new Logger\LoggerFactory(new Logger\AdapterFactory()))

        ->newInstance('test-logger', $adapters)`

静态方法日志记录
示例
\Mvs\Logger\Phalcon\Logger::critical('Critical message with {variable}', ['variable' => 'inserted value']);

\Mvs\Logger\Phalcon\Logger::debug('Debug log message with no variables');

单元测试
在运行之前需要编辑 tests/config.php 文件。