Purge old logs

[MAILPOET-570]
This commit is contained in:
Pavel Dohnal
2018-08-28 10:42:44 +02:00
parent 031125517e
commit 60ce501c20
2 changed files with 93 additions and 3 deletions

View File

@@ -2,11 +2,22 @@
namespace MailPoet\Logging; namespace MailPoet\Logging;
use Carbon\Carbon;
use MailPoet\Dependencies\Monolog\Handler\AbstractProcessingHandler; use MailPoet\Dependencies\Monolog\Handler\AbstractProcessingHandler;
use MailPoet\Models\Log; use MailPoet\Models\Log;
class LogHandler extends AbstractProcessingHandler { class LogHandler extends AbstractProcessingHandler {
/**
* Percentage value, what is the probability of running purge routine
* @var int
*/
CONST LOG_PURGE_PROBABILITY = 5;
/**
* Logs older than this many days will be deleted
*/
CONST DAYS_TO_KEEP_LOGS = 30;
protected function write(array $record) { protected function write(array $record) {
$model = Log::create(); $model = Log::create();
@@ -17,6 +28,15 @@ class LogHandler extends AbstractProcessingHandler {
'created_at' => $record['datetime']->format('Y-m-d H:i:s'), 'created_at' => $record['datetime']->format('Y-m-d H:i:s'),
]); ]);
$model->save(); $model->save();
if(rand(0, 100) <= self::LOG_PURGE_PROBABILITY) {
$this->purgeOldLogs();
}
}
private function purgeOldLogs() {
Log::whereLt('created_at', Carbon::create()->subDays(self::DAYS_TO_KEEP_LOGS)->toDateTimeString())
->deleteMany();
} }
} }

View File

@@ -4,6 +4,76 @@ namespace MailPoet\Models;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
/**
* @method static array|string getConfig($key = null, $connection_name = self::DEFAULT_CONNECTION)
* @method static null resetConfig()
* @method static \ORM forTable($table_name, $connection_name = self::DEFAULT_CONNECTION)
* @method static null setDb($db, $connection_name = self::DEFAULT_CONNECTION)
* @method static null resetDb()
* @method static null setupLimitClauseStyle($connection_name)
* @method static \PDO getDb($connection_name = self::DEFAULT_CONNECTION)
* @method static bool rawExecute($query, $parameters = array())
* @method static \PDOStatement getLastStatement()
* @method static string getLastQuery($connection_name = null)
* @method static array getQueryLog($connection_name = self::DEFAULT_CONNECTION)
* @method array getConnectionNames()
* @method $this useIdColumn($id_column)
* @method \ORM|bool findOne($id=null)
* @method array|\IdiormResultSet findMany()
* @method \IdiormResultSet findResultSet()
* @method array findArray()
* @method $this forceAllDirty()
* @method $this rawQuery($query, $parameters = array())
* @method $this tableAlias($alias)
* @method int countNullIdColumns()
* @method $this selectExpr($expr, $alias=null)
* @method \ORM selectMany($values)
* @method \ORM selectManyExpr($values)
* @method $this rawJoin($table, $constraint, $table_alias, $parameters = array())
* @method $this innerJoin($table, $constraint, $table_alias=null)
* @method $this leftOuterJoin($table, $constraint, $table_alias=null)
* @method $this rightOuterJoin($table, $constraint, $table_alias=null)
* @method $this fullOuterJoin($table, $constraint, $table_alias=null)
* @method $this whereEqual($column_name, $value=null)
* @method $this whereNotEqual($column_name, $value=null)
* @method $this whereIdIs($id)
* @method $this whereAnyIs($values, $operator='=')
* @method array|string whereIdIn($ids)
* @method $this whereLike($column_name, $value=null)
* @method $this whereNotLike($column_name, $value=null)
* @method $this whereGt($column_name, $value=null)
* @method static $this whereLt($column_name, $value=null)
* @method $this whereGte($column_name, $value=null)
* @method $this whereLte($column_name, $value=null)
* @method $this whereIn($column_name, $values)
* @method $this whereNotIn($column_name, $values)
* @method $this whereNull($column_name)
* @method $this whereNotNull($column_name)
* @method $this whereRaw($clause, $parameters=array())
* @method $this deleteMany()
* @method $this orderByDesc($column_name)
* @method $this orderByAsc($column_name)
* @method $this orderByExpr($clause)
* @method $this groupBy($column_name)
* @method $this groupByExpr($expr)
* @method $this havingEqual($column_name, $value=null)
* @method $this havingNotEqual($column_name, $value=null)
* @method $this havingIdIs($id)
* @method $this havingLike($column_name, $value=null)
* @method $this havingNotLike($column_name, $value=null)
* @method $this havingGt($column_name, $value=null)
* @method $this havingLt($column_name, $value=null)
* @method $this havingGte($column_name, $value=null)
* @method $this havingLte($column_name, $value=null)
* @method $this havingIn($column_name, $values=null)
* @method $this havingNotIn($column_name, $values=null)
* @method $this havingNull($column_name)
* @method $this havingNotNull($column_name)
* @method $this havingRaw($clause, $parameters=array())
* @method static $this clearCache($table_name = null, $connection_name = self::DEFAULT_CONNECTION)
* @method bool setExpr($key, $value = null)
* @method bool isDirty($key)
*/
class Model extends \Sudzy\ValidModel { class Model extends \Sudzy\ValidModel {
const DUPLICATE_RECORD = 23000; const DUPLICATE_RECORD = 23000;