Skip to content

Commit d76e5b3

Browse files
committed
Merge branch 'f/watchdog_logging'
* f/watchdog_logging: Adding watchdog logging control
2 parents 33d2611 + d55c0cf commit d76e5b3

File tree

6 files changed

+261
-8
lines changed

6 files changed

+261
-8
lines changed

os2web_logging.links.task.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ os2web_logging.settings:
22
title: Settings
33
route_name: os2web_logging.settings
44
base_route: os2web_logging.settings
5+
os2web_logging.watchdog_settings:
6+
title: Watchdog
7+
route_name: os2web_logging.watchdog_settings
8+
base_route: os2web_logging.settings
59
os2web_logging.status:
610
title: Status
711
route_name: os2web_logging.status

os2web_logging.routing.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,18 @@ os2web_logging.settings:
1616
_permission: 'administer os2logging settings'
1717
options:
1818
_admin_route: TRUE
19+
os2web_logging.watchdog_settings:
20+
path: '/admin/config/system/os2logging/watchdog-settings'
21+
defaults:
22+
_form: '\Drupal\os2web_logging\Form\WatchdogSettingsForm'
23+
_title: 'OS2Web watchdog logging settings'
24+
requirements:
25+
_permission: 'administer os2logging settings'
26+
options:
27+
_admin_route: TRUE
28+
os2web_logging.logfile.download:
29+
path: '/os2web_logging/log/{filename}'
30+
defaults:
31+
_controller: '\Drupal\os2web_logging\Controller\LoggingController::logfileExport'
32+
requirements:
33+
_permission: 'administer os2logging settings'

src/Controller/LoggingController.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\Core\Controller\ControllerBase;
66
use Drupal\Core\File\FileSystemInterface;
77
use Drupal\os2web_logging\Form\SettingsForm;
8+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
89

910
/**
1011
* Provides status page.
@@ -203,4 +204,31 @@ public static function getCheckedRequirements() {
203204
return $requirements;
204205
}
205206

207+
/**
208+
* Download file.
209+
*
210+
* @param string $filename
211+
* The filename.
212+
*/
213+
public function logfileExport($filename) {
214+
$config = $this->config(SettingsForm::$configName);
215+
216+
// Do some file validation here, like checking for extension.
217+
218+
// File lives in /files/downloads.
219+
$logPath = $config->get('files_log_path');
220+
221+
$uri = $logPath . '/'. $filename;
222+
223+
$headers = [
224+
'Content-Type' => 'text/csv', // Would want a condition to check for extension and set Content-Type dynamically
225+
'Content-Description' => 'File Download',
226+
'Content-Disposition' => 'attachment; filename=' . $filename
227+
];
228+
229+
// Return and trigger file donwload.
230+
return new BinaryFileResponse($uri, 200, $headers, true );
231+
232+
}
233+
206234
}

src/Form/SettingsForm.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
132132
$form['file_logs_detail']['files_log_path'] = [
133133
'#type' => 'textfield',
134134
'#title' => $this->t('Store log files directory'),
135-
'#description' => $this->t('Logs will be saved in this path'),
135+
'#description' => $this->t('Log file will be stored for the selected number of days, after that they will be automatically deleted'),
136136
'#default_value' => $config->get('files_log_path') ? $config->get('files_log_path') : '../logs',
137137
];
138138

@@ -142,19 +142,23 @@ public function buildForm(array $form, FormStateInterface $form_state) {
142142
->t('Logs import'),
143143
];
144144

145-
$nodeTypeOptions = [];
145+
$options = [];
146146
if ($config->get('files_log_path')) {
147-
$storedLogFiles = \Drupal::service('file_system')->scanDirectory($config->get('files_log_path'), '/os2web_logging_access_log-\d{4}-\d{2}-\d{2}\.(log|gz)/');
147+
/** @var FileSystemInterface $fileSystem */
148+
$fileSystem = \Drupal::service('file_system');
149+
$storedLogFiles = $fileSystem->scanDirectory($config->get('files_log_path'), '/os2web_logging_node_access-\d{4}-\d{2}-\d{2}\.(log|gz)/');
148150

149151
foreach ($storedLogFiles as $file) {
150-
$nodeTypeOptions[$file->uri] = $file->filename;
152+
$url = Url::fromRoute('os2web_logging.logfile.download', ['filename' => $file->filename]);
153+
$link = Link::fromTextAndUrl(t('[Download]'), $url);
154+
$options[$file->uri] = $file->filename . ' ' . $link->toString();
151155
}
152-
arsort($nodeTypeOptions);
156+
arsort($options);
153157
}
154158

155159
$form['logs_import_file_detail']['logs_import_files_select'] = [
156160
'#type' => 'checkboxes',
157-
'#options' => $nodeTypeOptions,
161+
'#options' => $options,
158162
'#title' => $this->t('Import from existing files'),
159163
'#description' => $this->t('Archived log files will be automatically extracted'),
160164
];

src/Form/WatchdogSettingsForm.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
namespace Drupal\os2web_logging\Form;
4+
5+
use Drupal\Component\Serialization\Json;
6+
use Drupal\Core\File\FileSystemInterface;
7+
use Drupal\Core\Form\ConfigFormBase;
8+
use Drupal\Core\Form\FormStateInterface;
9+
use Drupal\Core\Link;
10+
use Drupal\Core\Url;
11+
use Drupal\node\Entity\NodeType;
12+
use Drupal\os2web_logging\Entity\AccessLog;
13+
14+
/**
15+
* Watchdog Logging settings form.
16+
*/
17+
class WatchdogSettingsForm extends ConfigFormBase {
18+
19+
/**
20+
* Name of the config.
21+
*
22+
* @var string
23+
*/
24+
public static $configName = 'os2web_logging_watchdog.settings';
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function getEditableConfigNames() {
30+
return [WatchdogSettingsForm::$configName];
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function getFormId() {
37+
return 'os2web_logging_watchdog_settings_form';
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function buildForm(array $form, FormStateInterface $form_state) {
44+
$config = $this->config(WatchdogSettingsForm::$configName);
45+
46+
$form['watchdog_db_details'] = [
47+
'#type' => 'details',
48+
'#title' => $this
49+
->t('Watchdog DB logs'),
50+
'#open' => TRUE,
51+
];
52+
53+
$form['watchdog_db_details']['dblog_enabled'] = [
54+
'#type' => 'checkbox',
55+
'#title' => $this->t('DB Log enabled'),
56+
'#description' => $this->t('If logs are to be stored in the database as dblog entries.'),
57+
'#default_value' => $config->get('dblog_enabled') ?? TRUE,
58+
];
59+
60+
// Set up the link.
61+
$url = Url::fromUri('internal:/admin/reports/dblog');
62+
$link = Link::fromTextAndUrl('Recent log messages', $url);
63+
64+
$form['watchdog_db_details'][] = [
65+
'#markup' => $this->t('DB Logs messages can be seen on the %reports page', ['%reports' => $link->toString()]),
66+
];
67+
68+
$form['watchdog_files_details'] = [
69+
'#type' => 'details',
70+
'#title' => $this
71+
->t('Watchdog File logs'),
72+
];
73+
74+
$form['watchdog_files_details']['files_store_period'] = [
75+
'#type' => 'number',
76+
'#title' => $this->t('Store log files for this period'),
77+
'#field_suffix' => $this->t('days'),
78+
'#size' => 5,
79+
'#min' => 180,
80+
'#description' => $this->t('Log file will be stored for the selected number of days, after that they will be automatically deleted'),
81+
'#default_value' => $config->get('files_store_period') ?? 180,
82+
];
83+
84+
$form['watchdog_files_details']['files_log_path'] = [
85+
'#type' => 'textfield',
86+
'#title' => $this->t('Store log files directory'),
87+
'#description' => $this->t('Log file will be stored for the selected number of days, after that they will be automatically deleted'),
88+
'#default_value' => $config->get('files_log_path') ?? '../logs',
89+
];
90+
91+
$options = [];
92+
if ($config->get('files_log_path')) {
93+
/** @var FileSystemInterface $fileSystem */
94+
$fileSystem = \Drupal::service('file_system');
95+
$storedLogFiles = $fileSystem->scanDirectory($config->get('files_log_path'), '/os2web_logging_watchdog-\d{4}-\d{2}-\d{2}\.(log|gz)/');
96+
97+
foreach ($storedLogFiles as $file) {
98+
$url = Url::fromRoute('os2web_logging.logfile.download', ['filename' => $file->filename]);
99+
$link = Link::fromTextAndUrl($file->filename, $url);
100+
$options[$file->uri] = $link;
101+
}
102+
arsort($options);
103+
}
104+
105+
$watchdog_logs_build = [
106+
'#theme' => 'item_list',
107+
'#title' => t('Watchdog logs'),
108+
'#items' => $options,
109+
];
110+
$watchlog_logs_rendered = \Drupal::service('renderer')->renderPlain($watchdog_logs_build);
111+
112+
$form['watchdog_files_details'][] = [
113+
'#markup' => $watchlog_logs_rendered
114+
];
115+
116+
return parent::buildForm($form, $form_state);
117+
}
118+
119+
/**
120+
* {@inheritdoc}
121+
*/
122+
public function validateForm(array &$form, FormStateInterface $form_state) {
123+
parent::validateForm($form, $form_state);
124+
125+
$files_log_path = $form_state->getValue('files_log_path');
126+
127+
$exists = \Drupal::service('file_system')->prepareDirectory($files_log_path, FileSystemInterface::MODIFY_PERMISSIONS);
128+
if (!$exists) {
129+
$form_state->setErrorByName('files_log_path', t('Directory does not exist or is not writable %dir', ['%dir' => $files_log_path]));
130+
}
131+
}
132+
133+
/**
134+
* {@inheritdoc}
135+
*/
136+
public function submitForm(array &$form, FormStateInterface $form_state) {
137+
// Saving values.
138+
$config = $this->config(WatchdogSettingsForm::$configName);
139+
$old_files_store_period = $config->get('files_store_period');
140+
$old_files_store_path = $config->get('files_log_path');
141+
$old_dblog_enabled = $config->get('dblog_enabled');
142+
143+
$values = $form_state->getValues();
144+
foreach ($values as $key => $value) {
145+
$config->set($key, $value);
146+
}
147+
$config->save();
148+
149+
// Rebuilding cache only if 'files_store_period' or 'files_log_path'
150+
// changed. New setting requires cache to be rebuilt.
151+
if ($old_files_store_period != $config->get('files_store_period') ||
152+
$old_files_store_path != $config->get('files_log_path') ||
153+
$old_dblog_enabled != $config->get('dblog_enabled')) {
154+
155+
// Rebuild module and theme data.
156+
$module_data = \Drupal::service('extension.list.module')->getList();
157+
158+
$files = [];
159+
foreach ($module_data as $name => $extension) {
160+
if ($extension->status) {
161+
$files[$name] = $extension;
162+
}
163+
}
164+
\Drupal::service('kernel')
165+
->updateModules(\Drupal::moduleHandler()
166+
->getModuleList(), $files);
167+
}
168+
169+
parent::submitForm($form, $form_state);
170+
}
171+
172+
173+
}

src/Os2webLoggingServiceProvider.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use Drupal\Core\DependencyInjection\ContainerBuilder;
66
use Drupal\Core\DependencyInjection\ServiceProviderBase;
77
use Drupal\os2web_logging\Form\SettingsForm;
8+
use Drupal\os2web_logging\Form\WatchdogSettingsForm;
89

910
/**
10-
* Overrides the monolog.handler.os2web_logging_access_log_file service.
11+
* Overrides the monolog service.
1112
*/
1213
class Os2webLoggingServiceProvider extends ServiceProviderBase {
1314

@@ -22,8 +23,8 @@ public function alter(ContainerBuilder $container) {
2223
return;
2324
}
2425

26+
// Updating OS2Logger Node Access.
2527
$config = \Drupal::config(SettingsForm::$configName);
26-
2728
$logger = $container->getDefinition('monolog.handler.os2web_logging_access_log_file');
2829

2930
// Updating store path for logger.
@@ -38,6 +39,34 @@ public function alter(ContainerBuilder $container) {
3839
if ($store_period) {
3940
$logger->replaceArgument(1, $store_period);
4041
}
42+
43+
// Updating OS2Logger Watchdog.
44+
$watchdogConfig = \Drupal::config(WatchdogSettingsForm::$configName);
45+
$watchdogLogger = $container->getDefinition('monolog.handler.os2web_logging_watchdog');
46+
47+
// Updating store path for watchdog logger.
48+
$logs_path = $watchdogConfig->get('files_log_path');
49+
if (!empty($logs_path)) {
50+
$logs_path .= '/os2web_logging_watchdog.log';
51+
$watchdogLogger->replaceArgument(0, $logs_path);
52+
}
53+
54+
// Updating store period for logger.
55+
$store_period = $watchdogConfig->get('files_store_period');
56+
if ($store_period) {
57+
$watchdogLogger->replaceArgument(1, $store_period);
58+
}
59+
60+
// Updating monolog.channel_handlers.
61+
$channel_handlers = $container->getParameter('monolog.channel_handlers');
62+
$channel_handlers['default'] = [
63+
'os2web_logging_watchdog',
64+
];
65+
// Only add if DB Log enabled.
66+
if ($watchdogConfig->get('dblog_enabled')) {
67+
$channel_handlers['default'][] = 'drupal.dblog';
68+
}
69+
$container->setParameter('monolog.channel_handlers', $channel_handlers);
4170
}
4271

4372
}

0 commit comments

Comments
 (0)