NTILogBundle
Installation
- Install the bundle using composer:
$ composer require ntidev/log-bundle "dev-master"
- Add the bundle configuration to the AppKernel
public function registerBundles()
{
$bundles = array(
...
new NTI\LogBundle\NTILogBundle(),
...
);
}
- Setup the configuration in the
config.yml
# NTI
nti_log:
exclude: [ 'JMose\CommandSchedulerBundle\Entity\ScheduledCommand' ] # default: []
The exclude
allows you to exclude logging for specific entities that change at a rapid rate (for example the User entity usually registers changes when users log in), (*1)
- Update the database schema
$ php app/console doctrine:schema:update
Usage
- Get the Logging service
$logger = $container->get('nti.logger');
The following methods are available for logging:, (*2)
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)
Example:, (*3)
$service->logDebug("Hello World")
Event Listeners
The bundle comes with 2 event subscribers: DoctrineEventSubscriber
and KernelExceptionListerner
., (*4)
The DoctrineEventSubscriber
will listener for the following events:, (*5)
- PostPersist
- PostUpdate
- PostRemove
And it will log the changes automatically into the database., (*6)
The KernelExceptionListener
will capture all exceptions and log them into the database as well. However, if you capture an exception you must manually log it with the service, for example:, (*7)
try {
...
$em->flush()
} catch(\Exception $ex) {
$this->get('nti.logger')->logException($ex);
...
}
Slack Integration
If the NexySlackBundle is used, you can integrate this bundle to throw the information to a channel in Slack as well., (*8)
The configuration piece as shown above serves to configure how the NTILogBundle should post to Slack:, (*9)
# 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]"
Todo
- Make the entity configurable and also the property used to get the username