Skip to content

Commit

Permalink
Add Data logging ability
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaboncukcu committed Jan 26, 2018
1 parent 1f78436 commit d4732a4
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 30 deletions.
22 changes: 22 additions & 0 deletions config/Migrations/20180126102815_AddDataLoadToStories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
use Migrations\AbstractMigration;

class AddDataLoadToStories extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('stories');
$table->addColumn('data_load', 'text', [
'default' => null,
'null' => true,
]);
$table->update();
}
}
3 changes: 2 additions & 1 deletion config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
use Cake\Core\Configure;
use Cake\Database\Type;
use Cake\Event\EventManager;
use Cake\Log\Log;
use Stories\Middleware\LoggerMiddleware;


Type::map('json', 'Cake\Database\Type\JsonType');

Log::config('story', [
'className' => 'Stories.Database',
Expand Down
3 changes: 3 additions & 0 deletions config/stories.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
'Plugins' => [
'DebugKit'
]
],
[
'DataLogger' => true
]
]
];
1 change: 1 addition & 0 deletions src/Log/Engine/DatabaseLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private function getDataFromMessage($message)
$data['user_id'] = $message['user_id'];
$data['webroot'] = $message['webroot'];
$data['plugin'] = $message['plugin'];
$data['data_load'] = $message['data_load'];

return $data;
}
Expand Down
68 changes: 40 additions & 28 deletions src/Middleware/LoggerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,57 @@ public function __invoke($request, $response, $next)
// Calling $next() delegates control to the *next* middleware
// In your application's queue.
$response = $next($request, $response);

// Get the URI
$uri = $request->getUri();
$uri = $request->getUri();

// Read data out of the URI.
$path = $uri->getPath();
// Read data out of the URI.
$path = $uri->getPath();

$attributes = $request->getAttributes();
$attributes = $request->getAttributes();

// do not log specified plugins
if(in_array($attributes['params']['plugin'], Configure::read('Stories.DontLog.Plugins'))) {
return $response;
}

//don't log anything if user is not present
if(!$request->getSession()->read('Auth.User.id')) {
return $response;
}

//prepare the message
$message = json_encode([
'ip' => $_SERVER['REMOTE_ADDR'],
'user_id' => @$request->getSession()->read('Auth.User.id'),
'action' => $attributes['params']['action'],
'controller' => $attributes['params']['controller'],
'path' => $path,
'plugin' => $attributes['params']['plugin'],
'webroot' => $attributes['webroot'],
]);

//drop current logs.
$debug = Log::config('debug');
$error = Log::config('error');
Log::drop('debug');
Log::drop('error');

//log to register_user scope
//don't log anything if user is not present
if(!$request->getSession()->read('Auth.User.id')) {
return $response;
}

//prepare the message
$messageLoad = [
'ip' => $_SERVER['REMOTE_ADDR'],
'user_id' => @$request->getSession()->read('Auth.User.id'),
'action' => $attributes['params']['action'],
'controller' => $attributes['params']['controller'],
'path' => $path,
'plugin' => $attributes['params']['plugin'],
'webroot' => $attributes['webroot'],
];

// log data if it is allowed on Configuration
if(Configure::read('Stories.DataLogger') === true) {
$dataLoad = [];
$dataLoad['pass'] = $attributes['params']['pass'];
$dataLoad['query'] = $request->getQuery();
$dataLoad['postData'] = $request->getData();
$messageLoad['data_load'] = $dataLoad;
}

$message = json_encode($messageLoad);

//drop current logs.
$debug = Log::config('debug');
$error = Log::config('error');
Log::drop('debug');
Log::drop('error');

//log to register_user scope
Log::notice($message,[
'scope' => ['registered_user']
'scope' => ['registered_user']
]);

//reset default logs.
Expand Down
14 changes: 13 additions & 1 deletion src/Model/Table/StoriesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
namespace Stories\Model\Table;

use Cake\Core\Configure;
use Cake\Database\Schema\TableSchema;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Log\Log;
use Cake\Log\Log;


/**
* Stories Model
Expand All @@ -16,6 +18,16 @@
*/
class StoriesTable extends Table
{
/**
* @param TableSchema $schema
*
* @return TableSchema
*/
protected function _initializeSchema(TableSchema $schema)
{
$schema->setColumnType('data_load', 'json');
return $schema;
}

/**
* Initialize method
Expand Down
2 changes: 2 additions & 0 deletions src/Template/Stories/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ $this->end();
<th><?= $this->Paginator->sort('level') ?></th>
<th><?= $this->Paginator->sort('webroot') ?></th>
<th><?= $this->Paginator->sort('currentpath') ?></th>
<th><?= $this->Paginator->sort('data_load') ?></th>
<th><?= $this->Paginator->sort('plugin') ?></th>
<th><?= $this->Paginator->sort('created') ?></th>
<th class="actions"><?= __('Actions') ?></th>
Expand All @@ -64,6 +65,7 @@ $this->end();
<td><?= h($story->webroot) ?></td>
<td><?= h($story->currentpath) ?></td>
<td><?= h($story->plugin) ?></td>
<td><?= h($story->data_load) ?></td>
<td><?= h($story->created) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $story->id]) ?>
Expand Down
4 changes: 4 additions & 0 deletions src/Template/Stories/view.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<th><?= __('Ip') ?></th>
<td><?= $this->Number->format($story->ip) ?></td>
</tr>
<tr>
<th><?= __('Data Load') ?></th>
<td><?= h($story->data_load) ?></td>
</tr>
<tr>
<th><?= __('Created') ?></th>
<td><?= h($story->created) ?></td>
Expand Down

0 comments on commit d4732a4

Please sign in to comment.