-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog_prune.module
69 lines (61 loc) · 2.51 KB
/
watchdog_prune.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* @file
* Description: watchdog_prune.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function watchdog_prune_help($route_name, $route_match) {
if ($route_name == 'help.page.watchdog_prune') {
$output = file_get_contents(drupal_get_path('module', 'watchdog_prune') . '/README.txt');
$output = '<pre>' . $output . '</pre>';
return [
'#type' => 'markup',
'#markup' => $output,
];
}
}
/**
* Implements hook_cron().
*/
function watchdog_prune_cron() {
$prune_type = \Drupal::config('watchdog_prune.settings')->get('watchdog_prune_age_type', '');
$database = \Drupal::service('database.replica');
if (!empty($prune_type)) {
$prune_type = explode("\n", $prune_type);
$prune_type_list = [];
if (is_array($prune_type)) {
foreach ($prune_type as $value) {
$watchdog_prune_settings = explode("|", $value);
$prune_query = $database->delete('watchdog');
// Check if the user has entered the correct squence of settings to prune watchdog messages by type.
// Starting with the watchdog type.
if (isset($watchdog_prune_settings[0]) || !empty($watchdog_prune_settings[0])) {
$prune_query->condition('type', trim($watchdog_prune_settings[0]), '=');
$prune_type_list[] = $watchdog_prune_settings[0];
// Check if age is entered as we cannot delete wathdog entries if the age limit entered is incorrect.
if (isset($watchdog_prune_settings[1]) || !empty($watchdog_prune_settings[1])) {
$ts = strtotime($watchdog_prune_settings[1], time());
$prune_query->condition('timestamp', $ts, '<');
// Execute the SQL query to Delete.
$prune_query->execute();
}
}
}
}
}
// Should we delete from the watchdog table based on age?. After deleting all the entries by type let us delete other remaining watchdog entries by excluding the type of entries which were deleted above.
$prune_age = \Drupal::config('watchdog_prune.settings')->get('watchdog_prune_age', '-18 MONTHS');
if ($prune_age != "") {
$ts = strtotime($prune_age, time());
// Now, simply delete anything from watchdog which is *less than* than our $ts value (meaning, the entry is OLDER than our age limit).
$prune_age = $database->delete('watchdog');
$prune_age->condition('timestamp', $ts, '<');
if (count($prune_type_list) > 0) {
$prune_age->condition('type', $prune_type_list, 'NOT IN');
}
$prune_age->execute();
}
}