phalcon/incubator-logger

Phalcon Incubator Logger 适配器和格式化工具

v2.0.0 2023-07-13 21:42 UTC

This package is auto-updated.

Last update: 2024-09-05 21:01:22 UTC


README

Discord Packagist Version PHP from Packagist codecov Packagist

Phalcon Logger 组件的额外适配器。

安装

composer require phalcon/incubator-logger

Amazon CloudWatch

适配器用于将日志存储在 AWS CloudWatch 中

注意:在调用任何日志之前必须创建组名和流名。您可以在 AWS 控制台中完成此操作。

use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Phalcon\Incubator\Logger\Adapter\CloudWatch;
use Phalcon\Logger\Logger;

$di->set(
    'logger',
    function () {
        $client = new CloudWatchLogsClient([
            'credentials' => [
                'key' => 'AMAZON_CLOUDWATCH_KEY',
                'secret' => 'AMAZON_CLOUDWATCH_SECRET',
            ],
            'region' => 'AMAZON_CLOUDWATCH_REGION',
            'version' => 'latest', // Or any specific
        ]);

        $adapter = new CloudWatch($client, '/group/name', 'stream-name');

        return new Logger('messages', ['main' => $adapter]);
    }
);

数据库

适配器用于将日志存储在数据库表中

use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Incubator\Logger\Adapter\Database as DbLogger;

$di->set(
    'logger',
    function () {
        $connection = new Mysql(
            [
                'host'     => 'localhost',
                'username' => 'root',
                'password' => 'secret',
                'dbname'   => 'audit',
            ]
        );

        $logsName  = 'errors';
        $tableName = 'logs';

        return new DbLogger($connection, $logsName, $tableName);
    }
);

以下表用于存储日志

CREATE TABLE IF NOT EXISTS `logs` (
  `id` INT(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(32) DEFAULT NULL,
  `type` INT(3) NOT NULL,
  `content` text,
  `created_at` BIGINT unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

UDP 日志记录器

适配器通过 UDP 协议将消息发送到外部服务器

use Phalcon\Incubator\Logger\Adapter\Udp as UdpLogger;

$di->set(
    'logger',
    function () {
        $host = '192.168.1.2';
        $port = 65000;

        return new UdpLogger('errors', $host, $port);
    }
);

Slack 日志记录器

适配器将消息写入 Slack 频道

slack-log

$adapter = new \Phalcon\Incubator\Logger\Adapter\Slack('api-token','channel-name');

$logger = new \Phalcon\Logger\Logger('logger-name', ['main-slack' => $adapter]);
$logger->info('Information message to log in the channel channel-name');

在 Slack API 中创建应用程序

访问此 页面 以创建一个 应用程序(在选择/创建工作区后)

授予发布消息的权限

转到 功能 > OAuth & 权限,在 作用域 部分 -> 添加一个 OAuth 作用域 并选择以下权限

  • chat:write
  • chat:write.public

获取令牌

现在您可以在 安装应用程序 部分为您的机器人/用户生成一个令牌,其外观如下

xoxp-1111111-22222222-33333333-aaaaaaaaaaaaaaaaaaaaaa