Skip to content

Commit

Permalink
feat: add dataflow level settings to configure log handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
keevan committed Jul 21, 2023
1 parent 911e7dc commit 4572a22
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 7 deletions.
14 changes: 14 additions & 0 deletions classes/dataflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected static function define_properties(): array {
'enabled' => ['type' => PARAM_BOOL, 'default' => false],
'concurrencyenabled' => ['type' => PARAM_BOOL, 'default' => false],
'vars' => ['type' => PARAM_TEXT, 'default' => ''],
'loghandlers' => ['type' => PARAM_TEXT, 'default' => ''],
'timecreated' => ['type' => PARAM_INT, 'default' => 0],
'userid' => ['type' => PARAM_INT, 'default' => 0],
'timemodified' => ['type' => PARAM_INT, 'default' => 0],
Expand Down Expand Up @@ -711,4 +712,17 @@ public function has_trigger_step(string $classname): bool {
}
return false;
}

/**
* Hook to execute before the persistent validation.
*
* e.g. loghandlers are set as a multiselect (array), and stored as a CSV string.
*/
protected function before_validate() {
// Get the loghandlers value and convert the raw value to a CSV as required.
$loghandlers = $this->raw_get('loghandlers');
if (!empty($loghandlers) && is_array($loghandlers)) {
$this->raw_set('loghandlers', implode(',', $loghandlers));
}
}
}
15 changes: 15 additions & 0 deletions classes/form/dataflow_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use moodle_exception;
use tool_dataflows\local\execution\logging\log_handler;
use tool_dataflows\manager;

/**
Expand Down Expand Up @@ -101,6 +102,20 @@ public function definition() {
$outputsexample['example'] = \html_writer::tag('pre', $examplestring);
$mform->addElement('static', 'vars_help', '', get_string('field_vars_help', 'tool_dataflows', $outputsexample));

// Multi-select element used to enable different logging handlers.
$select = $mform->addElement(
'select',
'loghandlers',
get_string('log_handlers', 'tool_dataflows'),
[
log_handler::BROWSER_CONSOLE => get_string('log_handler_browser_console', 'tool_dataflows'),
log_handler::FILE_PER_DATAFLOW => get_string('log_handler_file_per_dataflow', 'tool_dataflows'),
log_handler::FILE_PER_RUN => get_string('log_handler_file_per_run', 'tool_dataflows'),
],
);
$select->setMultiple(true);
$mform->addElement('static', 'log_handlers_desc', '', get_string('log_handlers_desc', 'tool_dataflows'));

$this->add_action_buttons();
}

Expand Down
6 changes: 5 additions & 1 deletion classes/local/execution/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,12 @@ private function setup_logging() {
// Tweak the default datetime output to include microseconds.
$lineformatter = new LineFormatter(null, 'Y-m-d H:i:s.u');

// Log handlers.
// Log handler settings (prefer dataflow if set, otherwise site level settings).
$loghandlers = array_flip(explode(',', get_config('tool_dataflows', 'log_handlers')));
$dataflowloghandlers = array_flip(explode(',', $this->dataflow->get('loghandlers')));
if (!empty($dataflowloghandlers)) {
$loghandlers = $dataflowloghandlers;
}

// Default Moodle handler. Always on.
$mtracehandler = new mtrace_handler(Logger::DEBUG);
Expand Down
3 changes: 2 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/dataflows/db" VERSION="20220907" COMMENT="XMLDB file for Moodle admin/tool/dataflows"
<XMLDB PATH="admin/tool/dataflows/db" VERSION="20230720" COMMENT="XMLDB file for Moodle admin/tool/dataflows"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -12,6 +12,7 @@
<FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="concurrencyenabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="vars" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="YAML encoded variables"/>
<FIELD NAME="loghandlers" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="CSV handlers enabled for this dataflow"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="The time this record was created"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="The user who created this record"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="The time this record was modified."/>
Expand Down
16 changes: 16 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,21 @@ function xmldb_tool_dataflows_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2023050100, 'tool', 'dataflows');
}


if ($oldversion < 2023072100) {

// Define field loghandlers to be added to tool_dataflows.
$table = new xmldb_table('tool_dataflows');
$field = new xmldb_field('loghandlers', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'vars');

// Conditionally launch add field loghandlers.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Dataflows savepoint reached.
upgrade_plugin_savepoint(true, 2023072100, 'tool', 'dataflows');
}

return true;
}
6 changes: 3 additions & 3 deletions lang/en/tool_dataflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
$string['gpg_key_dir'] = 'Path to keyring directory';
$string['gpg_key_dir_desc'] = 'Path to keyring directory';
$string['log_handlers'] = 'Log handlers';
$string['log_handlers_desc'] = 'Additional log handlers to output dataflow logs to more destinations. The handler for mtrace is always active and cannot be disabled.';
$string['log_handler_file_per_dataflow'] = 'File per dataflow - [dataroot]/tool_dataflows/<id>-Y-m-d.log';
$string['log_handler_file_per_run'] = 'File per run - [dataroot]/tool_dataflows/<dataflowid>/<id>.log';
$string['log_handlers_desc'] = 'Additional log handlers to output dataflow logs to more destinations. The handler for mtrace is always active and cannot be disabled. Applying the settings at the dataflow level will override settings applied at the site admin level.';
$string['log_handler_file_per_dataflow'] = 'File per dataflow - [dataroot]/tool_dataflows/{id}-Y-m-d.log';
$string['log_handler_file_per_run'] = 'File per run - [dataroot]/tool_dataflows/{dataflowid}/{id}.log';
$string['log_handler_browser_console'] = 'Browser Console';
$string['permitted_dirs'] = 'Permitted directories';
$string['permitted_dirs_desc'] = "List directories here to allow them to be read from/written to by dataflow steps.
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023072000;
$plugin->release = 2023072000;
$plugin->version = 2023072100;
$plugin->release = 2023072100;
$plugin->requires = 2017051500; // Our lowest supported Moodle (3.3.0).
$plugin->supported = [35, 401]; // Available as of Moodle 3.9.0 or later.
// TODO $plugin->incompatible = ; // Available as of Moodle 3.9.0 or later.
Expand Down

0 comments on commit 4572a22

Please sign in to comment.