2001-03-10 11:07:52 +00:00
< ? php
2000-12-14 14:13:37 +00:00
2004-07-02 07:11:35 +00:00
/**
* @ file
* System monitoring and logging for administrators .
*
2012-08-29 18:15:05 +00:00
* The Database Logging module monitors your site and keeps a list of recorded
* events containing usage and performance data , errors , warnings , and similar
* operational information .
2004-07-02 07:11:35 +00:00
*/
2019-04-16 05:38:27 +00:00
use Drupal\Core\Url ;
2014-07-31 00:50:42 +00:00
use Drupal\Core\Form\FormStateInterface ;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface ;
2015-10-01 23:25:03 +00:00
use Drupal\Core\StringTranslation\TranslatableMarkup ;
Issue #2015149 by dagmar, Jo Fitzgerald, hanoii, pritish.kumar, Pol, Lendude, damiankloip, nicolas.rafaelli, alansaviolobo, Psikik, mondrake, yoroy, dawehner, alexpott, catch, jibran: Replace dblog recent log entries with a view
2017-06-22 10:47:52 +00:00
use Drupal\views\ViewExecutable ;
2012-10-26 16:04:02 +00:00
2004-05-24 18:37:50 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help () .
2004-05-24 18:37:50 +00:00
*/
2014-06-30 03:33:08 +00:00
function dblog_help ( $route_name , RouteMatchInterface $route_match ) {
2014-05-07 02:04:53 +00:00
switch ( $route_name ) {
case 'help.page.dblog' :
2009-12-06 18:11:41 +00:00
$output = '' ;
$output .= '<h3>' . t ( 'About' ) . '</h3>' ;
2017-03-04 01:20:24 +00:00
$output .= '<p>' . t ( 'The Database Logging module logs system events in the Drupal database. For more information, see the <a href=":dblog">online documentation for the Database Logging module</a>.' , [ ':dblog' => 'https://www.drupal.org/documentation/modules/dblog' ]) . '</p>' ;
2009-12-06 18:11:41 +00:00
$output .= '<h3>' . t ( 'Uses' ) . '</h3>' ;
$output .= '<dl>' ;
$output .= '<dt>' . t ( 'Monitoring your site' ) . '</dt>' ;
2019-04-16 05:38:27 +00:00
$output .= '<dd>' . t ( 'The Database Logging module allows you to view an event log on the <a href=":dblog">Recent log messages</a> page. The log is a chronological list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the log on a regular basis to ensure their site is working properly.' , [ ':dblog' => Url :: fromRoute ( 'dblog.overview' ) -> toString ()]) . '</dd>' ;
2009-12-06 18:11:41 +00:00
$output .= '<dt>' . t ( 'Debugging site problems' ) . '</dt>' ;
2019-04-16 05:38:27 +00:00
$output .= '<dd>' . t ( 'In case of errors or problems with the site, the <a href=":dblog">Recent log messages</a> page can be useful for debugging, since it shows the sequence of events. The log messages include usage information, warnings, and errors.' , [ ':dblog' => Url :: fromRoute ( 'dblog.overview' ) -> toString ()]) . '</dd>' ;
Issue #2732113 by tameeshb, selwynpolit, rajeshwari10, ronchica, JacobSanford, rakesh.gectcr, dagmar, dhruveshdtripathi, gaurav.kapoor, arkiii, Dinesh18, ranjith_kumar_k_u, KapilV, mikeohara, leslieg, kiamlaluno, xjm, cilefen, valthebald, longwave, jhodgdon, ChrisDarke, abi_cima, yoroy, ijf8090, catch: Clarify in the dblog_help that the dblog module should not be used for forensic log
2021-04-26 08:17:21 +00:00
$output .= '<dt>' . t ( 'This log is not persistent' ) . '</dt>' ;
$output .= '<dd>' . t ( 'The Database Logging module logs may be cleared by administrators and automated cron tasks, so they should not be used for <a href=":audit_trail_wiki">forensic logging</a>. For forensic purposes, use the Syslog module.' , [ ':audit_trail_wiki' => 'https://en.wikipedia.org/wiki/Audit_trail' ]) . '</dd>' ;
2009-12-06 18:11:41 +00:00
$output .= '</dl>' ;
2010-01-30 07:59:26 +00:00
return $output ;
2014-05-07 02:04:53 +00:00
case 'dblog.overview' :
2015-10-05 01:57:27 +00:00
return '<p>' . t ( 'The Database Logging module logs system events in the Drupal database. Monitor your site or debug site problems on this page.' ) . '</p>' ;
2003-08-19 19:59:33 +00:00
}
2002-06-01 21:57:29 +00:00
}
2014-01-29 08:25:00 +00:00
/**
2014-07-30 12:04:04 +00:00
* Implements hook_menu_links_discovered_alter () .
2014-01-29 08:25:00 +00:00
*/
2014-07-30 12:04:04 +00:00
function dblog_menu_links_discovered_alter ( & $links ) {
2014-01-29 08:25:00 +00:00
if ( \Drupal :: moduleHandler () -> moduleExists ( 'search' )) {
2017-03-04 01:20:24 +00:00
$links [ 'dblog.search' ] = [
2015-10-01 23:25:03 +00:00
'title' => new TranslatableMarkup ( 'Top search phrases' ),
2014-01-29 08:25:00 +00:00
'route_name' => 'dblog.search' ,
2015-10-01 23:25:03 +00:00
'description' => new TranslatableMarkup ( 'View most popular search phrases.' ),
2014-03-24 10:34:32 +00:00
'parent' => 'system.admin_reports' ,
2017-03-04 01:20:24 +00:00
];
2014-01-29 08:25:00 +00:00
}
return $links ;
}
2004-05-24 18:37:50 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_cron () .
2004-05-24 18:37:50 +00:00
*
2012-08-29 18:15:05 +00:00
* Controls the size of the log table , paring it to 'dblog_row_limit' messages .
2004-05-24 18:37:50 +00:00
*/
2007-04-10 10:10:27 +00:00
function dblog_cron () {
2010-10-03 02:14:23 +00:00
// Cleanup the watchdog table.
2013-09-16 03:58:06 +00:00
$row_limit = \Drupal :: config ( 'dblog.settings' ) -> get ( 'row_limit' );
2010-10-03 02:14:23 +00:00
// For row limit n, get the wid of the nth row in descending wid order.
// Counting the most recent n rows avoids issues with wid number sequences,
// e.g. auto_increment value > 1 or rows deleted directly from the table.
if ( $row_limit > 0 ) {
2018-08-31 01:15:01 +00:00
$connection = \Drupal :: database ();
2018-09-21 13:01:49 +00:00
$min_row = $connection -> select ( 'watchdog' , 'w' )
2017-03-04 01:20:24 +00:00
-> fields ( 'w' , [ 'wid' ])
2010-10-03 02:14:23 +00:00
-> orderBy ( 'wid' , 'DESC' )
-> range ( $row_limit - 1 , 1 )
-> execute () -> fetchField ();
// Delete all table entries older than the nth row, if nth row was found.
if ( $min_row ) {
2018-08-31 01:15:01 +00:00
$connection -> delete ( 'watchdog' )
2010-10-03 02:14:23 +00:00
-> condition ( 'wid' , $min_row , '<' )
-> execute ();
}
2009-08-29 03:36:27 +00:00
}
2000-12-16 08:39:01 +00:00
}
2000-12-14 14:13:37 +00:00
2012-08-29 18:15:05 +00:00
/**
* Gathers a list of uniquely defined database log message types .
*
* @ return array
* List of uniquely defined database log message types .
*/
2007-04-10 10:10:27 +00:00
function _dblog_get_message_types () {
2021-01-05 15:02:37 +00:00
return \Drupal :: database () -> query ( 'SELECT DISTINCT([type]) FROM {watchdog} ORDER BY [type]' )
2014-07-10 10:00:19 +00:00
-> fetchAllKeyed ( 0 , 0 );
2004-01-02 16:28:45 +00:00
}
2007-04-10 10:10:27 +00:00
2009-08-24 00:14:23 +00:00
/**
2012-08-29 18:15:05 +00:00
* Implements hook_form_FORM_ID_alter () for system_logging_settings () .
2009-08-24 00:14:23 +00:00
*/
2014-08-04 11:21:15 +00:00
function dblog_form_system_logging_settings_alter ( & $form , FormStateInterface $form_state ) {
2017-03-04 01:20:24 +00:00
$row_limits = [ 100 , 1000 , 10000 , 100000 , 1000000 ];
$form [ 'dblog_row_limit' ] = [
2009-08-24 00:14:23 +00:00
'#type' => 'select' ,
2010-09-01 03:03:05 +00:00
'#title' => t ( 'Database log messages to keep' ),
2015-01-16 10:43:35 +00:00
'#default_value' => \Drupal :: configFactory () -> getEditable ( 'dblog.settings' ) -> get ( 'row_limit' ),
2017-03-04 01:20:24 +00:00
'#options' => [ 0 => t ( 'All' )] + array_combine ( $row_limits , $row_limits ),
2019-04-16 05:38:27 +00:00
'#description' => t ( 'The maximum number of messages to keep in the database log. Requires a <a href=":cron">cron maintenance task</a>.' , [ ':cron' => Url :: fromRoute ( 'system.status' ) -> toString ()]),
2017-03-04 01:20:24 +00:00
];
2012-07-28 16:35:17 +00:00
$form [ '#submit' ][] = 'dblog_logging_settings_submit' ;
}
/**
* Form submission handler for system_logging_settings () .
*
* @ see dblog_form_system_logging_settings_alter ()
*/
2014-07-31 00:50:42 +00:00
function dblog_logging_settings_submit ( $form , FormStateInterface $form_state ) {
2015-01-16 10:43:35 +00:00
\Drupal :: configFactory () -> getEditable ( 'dblog.settings' ) -> set ( 'row_limit' , $form_state -> getValue ( 'dblog_row_limit' )) -> save ();
2009-08-24 00:14:23 +00:00
}
2017-05-08 12:30:36 +00:00
Issue #2015149 by dagmar, Jo Fitzgerald, hanoii, pritish.kumar, Pol, Lendude, damiankloip, nicolas.rafaelli, alansaviolobo, Psikik, mondrake, yoroy, dawehner, alexpott, catch, jibran: Replace dblog recent log entries with a view
2017-06-22 10:47:52 +00:00
/**
* Implements hook_views_pre_render () .
*/
function dblog_views_pre_render ( ViewExecutable $view ) {
if ( isset ( $view ) && ( $view -> storage -> get ( 'base_table' ) == 'watchdog' )) {
$view -> element [ '#attached' ][ 'library' ][] = 'dblog/drupal.dblog' ;
}
}