Skip to content

Commit

Permalink
added cleanUp function for database logs
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-graute committed Jan 21, 2021
1 parent 4cad2a5 commit 7eebfcb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
6 changes: 5 additions & 1 deletion config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
"mode": "ALL",
"storage": "database",
"log_file_path": "APPLICATION_PATH/logs",
"error_email_address": "john.doe@local"
"error_email_address": "john.doe@local",
"lifetime": 86400,
"keep_log_types": [
"ERROR"
]
},
"debug": {
"mode": "on",
Expand Down
48 changes: 47 additions & 1 deletion src/Pressmind/Log/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,56 @@
namespace Pressmind\Log;


use DateTime;
use Exception;
use Pressmind\ORM\Object\Log;
use Pressmind\Registry;

class Service
{
/**
* @var array
*/
private $_config;

/**
* @throws Exception
*/
public function cleanUp()
{
return 'Cleanup has run';
$this->_config = Registry::getInstance()->get('config')['logging'];
switch($this->_config['storage']) {
case 'filesystem':
$this->_cleanUpFilesystem();
break;
case 'database':
$this->_cleanUpDatabase();
break;
}
return 'Log cleanup for ' . $this->_config['storage'] . ' has run';
}

private function _cleanUpFilesystem()
{

}

/**
* @throws Exception
*/
private function _cleanUpDatabase()
{
$date = new DateTime();
$date->modify('-' . $this->_config['lifetime'] . ' seconds');
$filters = [
'date' => ['<', $date->format('Y-m-d H:i:s')]
];
if(is_array($this->_config['keep_log_types'])) {
$filters['type'] = ['not in', implode(',', $this->_config['keep_log_types'])];
}
$logs = Log::listAll($filters);
foreach ($logs as $log) {
$log->delete();
}
}
}

0 comments on commit 7eebfcb

Please sign in to comment.