easyconn / phalcon-loggers
Phalcon 用于 Slack 和 Sentry 的日志适配器。
5.0.2
2024-04-02 06:44 UTC
Requires
- php: >=8.0
- ext-phalcon: ^5.0
Requires (Dev)
- mockery/mockery: @stable
- phpunit/phpunit: ^5.7.0
- sentry/sdk: ^3.0.0
Suggests
- sentry/sentry: To be able to use the Sentry logging.
README
一组可配置的日志适配器,具有与 PSR 兼容的日志功能,适用于 phalcon 3.x 和 PHP 7.x。目前实现了以下适配器
Sentry
- 依赖于 sentry/sentry 包。
- 您必须运行
composer require sentry/sentry
- 您可能需要像这里解释的那样要求
Raven_Autoloader
(请参阅下面的 #installation 部分) - 默认情况下禁用,但如果定义了类
Raven_Client
则激活。
Slack
- 自包含且开箱即用。
... 我们计划再添加几个!
安装
这是一个完全兼容 composer 的库。只需运行
$ composer require crazyfactory/phalcon-loggers
如果您没有使用 composer 的自动加载(即 require 'vendor/autoload.php'
)&/或只想使用 phalcon 的本地自动加载器
$loader = new Phalcon\Loader; $loader->registerNamespaces([ 'CrazyFactory\\PhalconLogger' => '/path/to/vendor/crazyfactory/phalcon-loggers/src/', ])->register();
如果您还想使用 Sentry
$ composer require sentry/sentry
如果您没有使用 composer 的自动加载,则设置 Raven_
类的自动加载如下
require_once '/path/to/vendor/sentry/sentry/lib/Raven/Autoloader.php'; Raven_Autoloader::register();
使用方法
使用此库的最简单快捷方法是使用提供的 Service
$id = (new Phalcon\Security\Random)->uuid(); $di = new Phalcon\Di\FactoryDefault; (new CrazyFactory\PhalconLogger\Service)->register(null, $di); // If you already have `requestId` in config array/object you don't need to set it again. $di->getShared('logger')->setRequestId($id); $di->getShared('logger')->info('some text'); // Supports interpolation for keys wrapped in curly brace. $di->getShared('logger')->critical('some text {key}', ['key' => 'val']); // // Below examples assume that info level is allowed in config->slack->levels array. // // Mention an user in slack: $context = ['mentions' => 'slackbot', 'a' => 10]; $di->getShared('slack')->info('some text {a}', $context); // Customize channel, username, icon_emoji, icon_url via context: $context += [ 'username' => 'bot', 'channel' => '#general', 'icon_emoji' => ':monkey_face:', ]; $di->getShared('slack')->info('some other text {a}', $context); // Attachment: $context += [ 'attachment' => [ 'title' => 'Attachment title', 'text' => 'Attachment text', 'color' => 'good', ], ]; $di->getShared('slack')->info('yet other text {a} with attachment', $context);
有关详细信息和各种集成示例,请参阅 示例。还可以查看 API 部分。
配置
您可以将配置作为数组或文件传入。日志记录器有自己的配置选项,但共享公共参数 requestId
和 environment
。以下是 Slack 和 Sentry 的典型配置示例
use Phalcon\Logger; $config = [ // The application name used in logs. Helps to distinguish &/or search. 'appName' => '', // Current application environment. 'environment' => 'prod', 'requestID' => null, 'sentry' => [ // The login information for Sentry. If one of the values is empty the logging is suppressed silently. 'credential' => [ 'key' => '', 'secret' => '', 'projectId' => '', ], // The options for Raven_Client. See https://docs.sentry.io/clients/php/config/#available-settings 'options' => [ 'curl_method' => 'sync', 'prefixes' => [], 'app_path' => '', 'timeout' => 2, ], // Sentry will log errors/exceptions when the application environment set above is one of these. 'environments' => ['prod', 'staging'], // The log levels which are forwarded to sentry. 'levels' => [Logger::EMERGENCY, Logger::CRITICAL, Logger::ERROR], // These exceptions are not reported to sentry. 'dontReport' => [], ], 'slack' => [ // If webhook url is missing the logging is suppressed silently. 'webhookUrl' => '', // Slack will log messages when the application environment set above is one of these. 'environments' => ['prod', 'dev'], // Curl method can be 'sync' or 'exec' (sync uses php curl_*, exec runs in background). 'curlMethod' => 'sync', // HTTP headers to be appended to request. 'headers' => [], // The log levels which are forwarded to slack. 'levels' => [Logger::SPECIAL, Logger::CUSTOM], ], ]; $di = new Phalcon\Di\FactoryDefault; // Register the loggers with the config: $di->setShared('sentry', function () use ($config) { return new CrazyFactory\PhalconLogger\Adapter\Sentry($config); }); $di->setShared('slack', function () use ($config) { return new CrazyFactory\PhalconLogger\Adapter\Slack($config); }); // OR you could just use supplied `Service`: (new CrazyFactory\PhalconLogger\Service)->register($config, $di);
API
所有日志记录器都继承自基础 phalcon 日志记录器 适配器,因此它们自动继承基础的可调用的 API。此外,日志记录器还可以公开一些可用于直接调用的 API。
Sentry
Sentry 日志记录器有以下公共 API
- addCrumb(string $message, string $category = 'default', array $data = [], int $type = null)
$di->getShared('sentry')->addCrumb('User has just logged in');
- setTag(string $key, string $value)
$di->getShared('sentry')->setTag('name', 'value')->setTag('another', 'another-value');
- setUserContext(array $context)
// you can also use current user from DI if you have one $di->getShared('sentry')->setUserContext(['id' => 1, 'username' => 'someone', 'email' => 'bob@example.com']);
- setExtraContext(array $context)
$di->getShared('sentry')->setExtraContext(['arbitrary_key' => 'arbitrary_value', 'arbitrary_key_2', 'arbitrary_value_2']);
- getLastEventId()
$di->getShared('sentry')->getLastEventId();
- logException(\Throwable $exception, array $context = [], int $type = null)
try { $app->handle(); } catch (\Throwable $e) { $di->getShared('sentry')->logException($e); // However it is advisable to just use logException of the logger collection // so that all the registered loggers are notified of the exception to do the needful. $di->getShared('logger')->logException($e); }