nti / log-bundle
Symfony NTILogBundle
v3.0.4
2024-07-18 15:13 UTC
Requires
- symfony/serializer: >=2.0
README
安装
- 使用composer安装包
$ composer require ntidev/log-bundle "dev-master"
- 将包配置添加到AppKernel
public function registerBundles()
{
$bundles = array(
...
new NTI\LogBundle\NTILogBundle(),
...
);
}
- 在
config.yml中设置配置
# NTI
nti_log:
exclude: [ 'JMose\CommandSchedulerBundle\Entity\ScheduledCommand' ] # default: []
exclude允许您排除快速变化的特定实体(例如,用户实体通常在用户登录时注册更改)的日志记录
- 更新数据库模式
$ php app/console doctrine:schema:update
使用方法
- 获取日志服务
$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个事件订阅者:DoctrineEventSubscriber 和 KernelExceptionListerner。
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]"
待办事项
- 使实体可配置,并配置用于获取用户名的属性