Created
February 21, 2019 10:21
-
-
Save msamgan/e88175a77e89ad759dc9f91d1bdeb6a5 to your computer and use it in GitHub Desktop.
Log data at event, behaviour CakePhp3.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Behavior; | |
use Cake\Datasource\EntityInterface; | |
use Cake\Event\Event; | |
use Cake\Log\Log; | |
use Cake\ORM\Behavior; | |
use Cake\ORM\Table; | |
/** | |
* Logger behavior | |
*/ | |
class LoggerBehavior extends Behavior | |
{ | |
/** | |
* Default configuration. | |
* | |
* @var array | |
*/ | |
protected $_defaultConfig = []; | |
/** | |
* @param Event $event | |
* @param EntityInterface $entity | |
*/ | |
public function afterSave(Event $event, EntityInterface $entity) | |
{ | |
$message = 'Entity ' . $entity->getSource() . ' Saved : ' . json_encode($entity); | |
if ($entity->isNew()) { | |
$message = 'New '. $message; | |
} | |
log::write('info', $message); | |
} | |
/** | |
* @param Event $event | |
* @param EntityInterface $entity | |
*/ | |
public function afterDelete(Event $event, EntityInterface $entity) | |
{ | |
log::write('info', 'Entity ' . $entity->getSource() . ' Deleted : ' . json_encode($entity)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment