定制 metarush/log-once 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

metarush/log-once

最新稳定版本:v1.1.0

Composer 安装命令:

composer require metarush/log-once

包简介

Log messages, usually error messages, only once and optionally get notified. No more flood of error logs with the same message.

README 文档

README

Log messages, usually error messages, only once and optionally get notified. No more flood of error logs with the same message.

Install

Install via composer as metarush/log-once

Sample usage

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();

Log adapters

File system

use MetaRush\LogOnce\FileSystem\Adapter as FileSytemLogger;

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

$adapter = new FileSytemLogger($logDir);

Note: To mark a log file as read, rename it with a __ALREADYREAD suffix e.g., 2021-01-01_00-00-00_+0000__12345__ALREADYREAD.log or you can simply delete the file.

PDO database (e.g., MySQL, PostgreSQL, SQLite)

Create a table with ff. fields:

  • id INTEGER PRIMARY KEY AUTOINCREMENT,
  • createdOn DATETIME, // must use YYYY-MM-DD HH:MM:SS
  • hash TEXT, // make length as long as your hash function's output e.g., if \md5(), must be 32
  • message TEXT, // make length as long your log messages
  • alreadyRead INTEGER // will have 1 or 0 value, can be ENUM or UNSIGNED TINY INT if you want
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');

Note: To mark a log row as read, set the alreadyRead column to 1 or you can simply delete the row.

Notifiers

We use the package metarush/notifier as 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];

Inject $notifiers in ->setNotifiers($notifiers) to the sample usage above.

For more info on how to use other available notifiers such as email visit metarush/notifier

Viewing logs

A UI to view the logs is not included in this package. You can simply use whatever database admin tool you're using if you're using PDO logger, or manually view the file system, if you're using the File system logger.

统计信息

  • 总下载量: 145
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 6
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-13