Skip to content

Commit

Permalink
feat: add min log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
keevan committed Oct 10, 2024
1 parent 3193f74 commit e9c0230
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion classes/dataflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace tool_dataflows;

use Monolog\Logger;
use core\persistent;
use Symfony\Component\Yaml\Yaml;
use tool_dataflows\local\step\flow_step;
Expand Down Expand Up @@ -63,7 +64,8 @@ protected static function define_properties(): array {
'timemodified' => ['type' => PARAM_INT, 'default' => 0],
'usermodified' => ['type' => PARAM_INT, 'default' => 0],
'confighash' => ['type' => PARAM_TEXT, 'default' => ''],
'notifyonabort' => ['type' => PARAM_TEXT, 'default' => '']
'notifyonabort' => ['type' => PARAM_TEXT, 'default' => ''],
'minloglevel' => ['type' => PARAM_INT, 'default' => Logger::DEBUG],
];
}

Expand Down
13 changes: 13 additions & 0 deletions classes/form/dataflow_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace tool_dataflows\form;

use Monolog\Logger;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use moodle_exception;
Expand Down Expand Up @@ -119,6 +120,18 @@ public function definition() {
$mform->addElement('text', 'notifyonabort', get_string('notifyonabort', 'tool_dataflows'));
$mform->addElement('static', 'notifyonabort_desc', '', get_string('notifyonabort_desc', 'tool_dataflows'));

$mform->addElement('select', 'minloglevel', get_string('minloglevel', 'tool_dataflows'), [
Logger::DEBUG => 'DEBUG',
Logger::INFO => 'INFO',
Logger::NOTICE => 'NOTICE',
Logger::WARNING => 'WARNING',
Logger::ERROR => 'ERROR',
Logger::CRITICAL => 'CRITICAL',
Logger::ALERT => 'ALERT',
Logger::EMERGENCY => 'EMERGENCY',
]);
$mform->addElement('static', 'minloglevel_desc', '', get_string('minloglevel_desc', 'tool_dataflows'));

$this->add_action_buttons();
}

Expand Down
11 changes: 7 additions & 4 deletions classes/local/execution/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,14 +815,17 @@ private function setup_logging() {
$loghandlers = $dataflowloghandlers;
}

// Minimum logging levels - will display this level and above.
$minloglevel = $this->dataflow->get('minloglevel');

// Default Moodle handler. Always on.
$mtracehandler = new mtrace_handler(Logger::DEBUG);
$mtracehandler = new mtrace_handler($minloglevel);
$mtracehandler->setFormatter($lineformatter);
$log->pushHandler($mtracehandler);

// Log to the browser's dev console for a manual run.
if (isset($loghandlers[log_handler::BROWSER_CONSOLE])) {
$log->pushHandler(new BrowserConsoleHandler(Logger::DEBUG));
$log->pushHandler(new BrowserConsoleHandler($minloglevel));
}

// Dataflow run logger.
Expand All @@ -834,7 +837,7 @@ private function setup_logging() {
$this->dataflow->id . DIRECTORY_SEPARATOR .
$rundateformat . '_' . $this->run->name . '.log';

$streamhandler = new StreamHandler($dataflowrunlogpath, Logger::DEBUG);
$streamhandler = new StreamHandler($dataflowrunlogpath, $minloglevel);
$streamhandler->setFormatter($lineformatter);
$log->pushHandler($streamhandler);
}
Expand All @@ -847,7 +850,7 @@ private function setup_logging() {
'tool_dataflows' . DIRECTORY_SEPARATOR .
$this->dataflow->id . '.log';

$rotatingfilehandler = new RotatingFileHandler($dataflowlogpath, 0, Logger::DEBUG);
$rotatingfilehandler = new RotatingFileHandler($dataflowlogpath, 0, $minloglevel);
$dateformat = 'Ymd';
$filenameformat = '{date}_{filename}';
$rotatingfilehandler->setFilenameFormat($filenameformat, $dateformat);
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="20230720" COMMENT="XMLDB file for Moodle admin/tool/dataflows"
<XMLDB PATH="admin/tool/dataflows/db" VERSION="20241010" 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 @@ -19,6 +19,7 @@
<FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Who last modified this record?"/>
<FIELD NAME="confighash" TYPE="char" LENGTH="40" NOTNULL="false" SEQUENCE="false" COMMENT="The config hash of the most recent run"/>
<FIELD NAME="notifyonabort" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Email address to notify aborted runs to."/>
<FIELD NAME="minloglevel" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="100" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
16 changes: 15 additions & 1 deletion db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ function xmldb_tool_dataflows_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2024030201, 'tool', 'dataflows');
}

if ($oldversion < 2024101000) {

// Define field minloglevel to be added to tool_dataflows.
$table = new xmldb_table('tool_dataflows');
$field = new xmldb_field('minloglevel', XMLDB_TYPE_INTEGER, '1', null, null, null, '100', 'notifyonabort');

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

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

return true;
}

Expand Down Expand Up @@ -390,4 +405,3 @@ function xmldb_tool_dataflows_step_config_helper(string $type, array $newfields)
}
$transaction->allow_commit();
}

2 changes: 2 additions & 0 deletions lang/en/tool_dataflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
See the run logs for further details.';
$string['notifyonabort_subject'] = 'Dataflow run aborted: {$a}';
$string['minloglevel'] = 'Minimum Log level';
$string['minloglevel_desc'] = 'This defines the minimum level to log. For example, if set at WARNING, it will only log WARNING messages and above, like ERROR, CRITICAL, ALERT, and EMERGENCY';

// Dataflow import form.
$string['dataflow_file'] = 'Dataflow file';
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 = 2024053100;
$plugin->release = 2024053100;
$plugin->version = 2024101000;
$plugin->release = 2024101000;
$plugin->requires = 2022112800; // Our lowest supported Moodle (3.3.0).
$plugin->supported = [400, 402];
// TODO $plugin->incompatible = ; // Available as of Moodle 3.9.0 or later.
Expand Down

0 comments on commit e9c0230

Please sign in to comment.