nti/log-bundle

Symfony NTILogBundle

安装次数: 3,692

依赖关系: 0

建议者: 0

安全性: 0

星标: 0

关注者: 7

分支: 1

公开问题: 2

类型:symfony-bundle

v3.0.4 2024-07-18 15:13 UTC

README

安装

  1. 使用composer安装包
$ composer require ntidev/log-bundle "dev-master"
  1. 将包配置添加到AppKernel
public function registerBundles()
{
    $bundles = array(
        ...
        new NTI\LogBundle\NTILogBundle(),
        ...
    );
}
  1. config.yml中设置配置
# NTI
nti_log:
    exclude: [ 'JMose\CommandSchedulerBundle\Entity\ScheduledCommand' ]     # default: []

exclude允许您排除快速变化的特定实体(例如,用户实体通常在用户登录时注册更改)的日志记录

  1. 更新数据库模式
$ php app/console doctrine:schema:update

使用方法

  1. 获取日志服务
$logger = $container->get('nti.logger');

以下方法可用于日志记录

logNotice($message, $action = Log::ACTION_INFO, $entity = null)
logSuccess($message, $action = Log::ACTION_INFO, $entity = null)
logWarning($message, $action = Log::ACTION_INFO, $entity = null) 
logDebug($message, $action = Log::ACTION_DEBUG, $entity = null)
logError($message, $action = Log::ACTION_INFO, $entity = null)    
logException(\Exception $ex) 
logSlack($message, $level = Log::LEVEL_NOTICE, $entity = null) 

示例

$service->logDebug("Hello World")

事件监听器

该包包含2个事件订阅者:DoctrineEventSubscriberKernelExceptionListerner

DoctrineEventSubscriber将监听以下事件

  • PostPersist
  • PostUpdate
  • PostRemove

并将更改自动记录到数据库中。

KernelExceptionListener将捕获所有异常并将它们记录到数据库中。但是,如果您捕获了异常,您必须手动使用服务进行记录,例如

try {
    ...
    $em->flush()
} catch(\Exception $ex) {
    $this->get('nti.logger')->logException($ex);
    ...
}

Slack集成

如果使用了NexySlackBundle,您可以将此包集成到向Slack通道发送信息。

如上所示的配置部分用于配置NTILogBundle如何向Slack发送帖子

# NTI
nti_log:
    ...    
    # In case NexySlackBundle is used
    nexy_slack:
        enabled:    # default: false
        replicate_logs: true     # default: false
        replicate_levels: [ERROR, DEBUG]   # default: [ERROR]
        channel: "#alertchannel"  # default: empty, required
        
# NexySlackBundle
nexy_slack:
    # The Slack API Incoming WebHooks URL.
    endpoint: "[see https://api.slack.com/tokens to generate the webhook for this app]"

待办事项

  • 使实体可配置,并配置用于获取用户名的属性