metarush/log-once

仅记录一次日志消息,通常是错误消息,并可选地接收通知。不再有相同消息的错误日志洪水。

v1.0.3 2023-09-07 08:06 UTC

This package is auto-updated.

Last update: 2024-09-07 10:17:02 UTC


README

仅记录一次日志消息,通常是错误消息,并可选地接收通知。不再有相同消息的错误日志洪水。

安装

使用composer以metarush/log-once的方式安装

示例用法

use MetaRush\LogOnce\LogOnce;

$message = 'This is my log';

$hash = \crc32($message); // you can use any hash function of your choice e.g., \md5(), \sha1(), etc

(new LogOnce($adapter)) // see adapter examples below
    ->setTimeZone('UTC')
    ->setHash($hash)
    ->setLogMessage($message)
    ->setNotifiers($notifiers) // optional, see below
    ->log();

日志适配器

文件系统

use MetaRush\LogOnce\FileSystem\Adapter as FileSytemLogger;

$logDir = '/path/to/logs';

$adapter = new FileSytemLogger($logDir);

注意:要将日志文件标记为已读,请将其重命名为带有__ALREADYREAD后缀的名称,例如:2021-01-01_00-00-00_+0000__12345__ALREADYREAD.log,或者您可以简单地删除该文件。

PDO数据库(例如,MySQL、PostgreSQL、SQLite)

创建以下字段的表

  • id INTEGER PRIMARY KEY AUTOINCREMENT,
  • createdOn DATETIME, // 必须使用 YYYY-MM-DD HH:MM:SS
  • hash TEXT, // 使长度与哈希函数的输出相同,例如,如果使用 \md5(),则必须为32
  • message TEXT, // 使长度与日志消息相同
  • alreadyRead INTEGER // 将具有1或0的值,如果想要,可以是ENUM或UNSIGNED TINY INT
use MetaRush\DataMapper\Builder as DataMapperBuilder;
use MetaRush\LogOnce\Pdo\Adapter as PdoLogger;

$dataMapper = (new DataMapperBuilder)
    ->setDsn('mysql:host=localhost;dbname=yourLogDb')
    ->setDbUser('user')
    ->setDbPass('pass');
    ->build();

$adapter = new PdoLogger($dataMapper, 'yourLogTable');

注意:要将日志行标记为已读,将alreadyRead列设置为1或您可以直接删除该行。

通知器

我们使用包metarush/notifier作为通知器

use MetaRush\Notifier\Pushover\Builder as PushoverNotifier;

// define a Pushover notifier

$pushoverNotifier = (new PushoverNotifier)
                        ->addAccount('pushover_app_key', 'pushover_user_key')
                        ->setSubject('test subject')
                        ->setBody('test body')
                        ->build();

// $emailNotifier = (new EmailNotifier)...; // optionally add other notifiers

$notifiers = [$pushoverNotifier, $emailNotifier];

在上面的示例用法中将$notifiers注入到->setNotifiers($notifiers)中。

有关如何使用其他可用通知器(如电子邮件)的更多信息,请访问metarush/notifier

查看日志

此包不包括查看日志的用户界面。如果您使用PDO日志记录器,则可以使用您正在使用的任何数据库管理工具,如果您使用文件系统日志记录器,则可以手动查看文件系统。