MDL-37658 add new logging API with basic implemenation

This commit is contained in:
Petr Škoda
2014-02-28 14:52:24 +08:00
parent 76e4de31cc
commit 7eaca5a810
42 changed files with 2168 additions and 172 deletions
+81 -1
View File
@@ -347,6 +347,83 @@ abstract class base implements \IteratorAggregate {
return $event;
}
/**
* Create fake event from legacy log data.
*
* @param stdClass $legacy
* @return base
*/
public static final function restore_legacy($legacy) {
$classname = get_called_class();
$event = new $classname();
$event->restored = true;
$event->triggered = true;
$event->dispatched = true;
$context = false;
$component = 'legacy';
if ($legacy->cmid) {
$context = \context_module::instance($legacy->cmid, IGNORE_MISSING);
$component = 'mod_'.$legacy->module;
} else if ($legacy->course) {
$context = \context_course::instance($legacy->course, IGNORE_MISSING);
}
if (!$context) {
$context = \context_system::instance();
}
$event->data = array();
$event->data['eventname'] = $legacy->module.'_'.$legacy->action;
$event->data['component'] = $component;
$event->data['action'] = $legacy->action;
$event->data['target'] = null;
$event->data['objecttable'] = null;
$event->data['objectid'] = null;
if (strpos($legacy->action, 'view') !== false) {
$event->data['crud'] = 'r';
} else if (strpos($legacy->action, 'print') !== false) {
$event->data['crud'] = 'r';
} else if (strpos($legacy->action, 'update') !== false) {
$event->data['crud'] = 'u';
} else if (strpos($legacy->action, 'hide') !== false) {
$event->data['crud'] = 'u';
} else if (strpos($legacy->action, 'move') !== false) {
$event->data['crud'] = 'u';
} else if (strpos($legacy->action, 'write') !== false) {
$event->data['crud'] = 'u';
} else if (strpos($legacy->action, 'tag') !== false) {
$event->data['crud'] = 'u';
} else if (strpos($legacy->action, 'remove') !== false) {
$event->data['crud'] = 'u';
} else if (strpos($legacy->action, 'delete') !== false) {
$event->data['crud'] = 'p';
} else if (strpos($legacy->action, 'create') !== false) {
$event->data['crud'] = 'c';
} else if (strpos($legacy->action, 'post') !== false) {
$event->data['crud'] = 'c';
} else if (strpos($legacy->action, 'add') !== false) {
$event->data['crud'] = 'c';
} else {
// End of guessing...
$event->data['crud'] = 'r';
}
$event->data['edulevel'] = $event::LEVEL_OTHER;
$event->data['contextid'] = $context->id;
$event->data['contextlevel'] = $context->contextlevel;
$event->data['contextinstanceid'] = $context->instanceid;
$event->data['userid'] = ($legacy->userid ? $legacy->userid : null);
$event->data['courseid'] = ($legacy->course ? $legacy->course : null);
$event->data['relateduserid'] = ($legacy->userid ? $legacy->userid : null);
$event->data['timecreated'] = $legacy->time;
$event->logextra = array('origin'=>$legacy->ip, 'realuserid'=>null);
$event->data['other'] = (array)$legacy;
return $event;
}
/**
* Returns event context.
* @return \context
@@ -504,7 +581,10 @@ abstract class base implements \IteratorAggregate {
if (isset($CFG->loglifetime) and $CFG->loglifetime != -1) {
if ($data = $this->get_legacy_logdata()) {
call_user_func_array('add_to_log', $data);
$manager = get_log_manager();
if (method_exists($manager, 'legacy_add_to_log')) {
call_user_func_array(array($manager, 'legacy_add_to_log'), $data);
}
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Dummy storage manager, returns nothing.
* used when no other manager available.
*
* @package core
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\log;
defined('MOODLE_INTERNAL') || die();
class dummy_manager implements manager {
public function get_readers(\context $context) {
return array();
}
public function dispose() {
}
}
@@ -15,44 +15,38 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A scheduled task.
* Log storage manager interface.
*
* @package core
* @copyright 2013 onwards Martin Dougiamas http://dougiamas.com
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\task;
namespace core\log;
defined('MOODLE_INTERNAL') || die();
/**
* Simple task to delete old log records.
* Interface describing log readers.
*
* This is intended for reports, use get_log_manager() to get
* the configured instance.
*
* @package core\log
*/
class delete_logs_task extends scheduled_task {
interface manager {
/**
* Get a descriptive name for this task (shown to admins).
* Return list of available log readers in given
* context for current user.
*
* @return string
* @param \context $context
* @return \core\log\reader[]
*/
public function get_name() {
return get_string('taskdeletelogs', 'admin');
}
public function get_readers(\context $context);
/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
* Dispose all initialised stores.
* @return void
*/
public function execute() {
global $CFG, $DB;
$timenow = time();
// Delete old logs to save space.
// Value in days.
if (!empty($CFG->loglifetime)) {
$loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
$DB->delete_records_select("log", "time < ?", array($loglifetime));
}
}
public function dispose();
}
+72
View File
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Log storage reader interface.
*
* @package core
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\log;
defined('MOODLE_INTERNAL') || die();
interface reader {
/**
* Localised name of the reader.
*
* To be used in selection for in reports.
*
* @return string
*/
public function get_name();
/**
* Longer description of the log data source.
* @return string
*/
public function get_description();
/**
* Can the current user access this store?
* @param \context $context
* @return bool
*/
public function can_access(\context $context);
/**
* Fetch records using given criteria.
*
* @param string $selectwhere
* @param array $params
* @param string $sort
* @param int $limitfrom
* @param int $limitnum
* @return \core\event\base[]
*/
public function get_events($selectwhere, array $params, $sort, $limitfrom, $limitnum);
/**
* Return number of events matching given criteria.
*
* @param string $selectwhere
* @param array $params
* @return int
*/
public function get_events_count($selectwhere, array $params);
}
+43
View File
@@ -0,0 +1,43 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Log storage sql reader interface.
*
* @package core
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\log;
defined('MOODLE_INTERNAL') || die();
interface sql_reader extends reader {
/**
* Returns name of the table or database view that
* holds the log data in standardised format.
*
* Note: this table must be used for reading only,
* it is strongly recommended to use this in complex reports only.
*
* TODO: define the standard log columns - watch out for "level" reserved word!
*
* @return string
*/
public function get_log_table();
}
+5 -1
View File
@@ -1028,6 +1028,10 @@ class core_plugin_manager {
'local' => array(
),
'logstore' => array(
'database', 'legacy', 'standard',
),
'message' => array(
'email', 'jabber', 'popup'
),
@@ -1117,7 +1121,7 @@ class core_plugin_manager {
'tool' => array(
'assignmentupgrade', 'behat', 'capability', 'customlang',
'dbtransfer', 'generator', 'health', 'innodb', 'installaddon',
'langimport', 'multilangupgrade', 'phpunit', 'profiling',
'langimport', 'log', 'multilangupgrade', 'phpunit', 'profiling',
'qeupgradehelper', 'replace', 'spamcleaner', 'task', 'timezoneimport',
'unittest', 'uploadcourse', 'uploaduser', 'unsuproles', 'xmldb'
),