General developer forum

Simple logging from a question type plugin

 
Picture of Damion Young
Simple logging from a question type plugin
 

Can anyone give me a quick pointer on how to simply log to a file (or anywhere) from within question type .php files so I can get a handle on what's going on - print_r's only so useful in this sort of environment and I'm not sure I can face getting to grips with XDebug.

 
Average of ratings: -
Picture of Sam Chaffee
Re: Simple logging from a question type plugin
Group Core developers

You can use Moodle's debugging() function with debug display off which should send it to the error log. Or you could directly use PHP's error_log() function which should do the same thing.

See https://docs.moodle.org/32/en/Debugging for more info on Moodle debugging.

 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: Simple logging from a question type plugin
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

There are some other tricks, in addition to debugging and print_object, depending on what exactly you are trying to do.

Don't overlook the power of unit tests. If you are trying to understand, for example, what the grade_response function in your question type is doing in certain situations, then writing unit tests that set up a question then call that method directly with various inputs can be the best way to verify what is going on. For example https://github.com/moodleou/moodle-qtype_pmatch/blob/master/tests/question_test.php#L107, though for pmatch, most of the detailed testing happens at an even lower level - https://github.com/moodleou/moodle-qtype_pmatch/blob/master/tests/pmatch_test.php#L83.

Also, I assume you have already read this docs pages: https://docs.moodle.org/dev/Overview_of_the_Moodle_question_engine#Key_processes. It is also worth understanding https://docs.moodle.org/dev/Using_the_question_engine_from_module.

For almost all key actions (e.g. submitting a response) the flow is

Activity (e.g. Quiz) <-> Question engine <-> Question behaviour <-> Question type

So, the question behaviour is in control of exactly what happens in any given question_attempt, and the question type does what it is told. The question engine orchestrates that, and provides a consistent API to other parts of the system that want to use questions.

I hope that is some help.

 
Average of ratings: -