-
Notifications
You must be signed in to change notification settings - Fork 40
Logging
The Util class includes a static property named logLevel which can be used to control the sending of log messages to a log. This property should be given one of four values defined by the ceLTIc\LTI\Enum\LogLevel enumeration (or constants in the Util class for version 4 or earlier). One of four values should be used:
| Enumeration value | Description |
|---|---|
| LogLevel::None | No log messages are recorded (this is the default setting) |
| LogLevel::Error | Only logs of errors are recorded |
| LogLevel::Info | All error and information log messages are recorded |
| LogLevel::Debug | All log messages are recorded |
In version 4 (and earlier), use one of the constants defined in the ceLTIc\LTI\Util class:
| Constant name | Description |
|---|---|
| LOGLEVEL_NONE | No log messages are recorded (this is the default setting) |
| LOGLEVEL_ERROR | Only logs of errors are recorded |
| LOGLEVEL_INFO | All error and information log messages are recorded |
| LOGLEVEL_DEBUG | All log messages are recorded |
By default, no messages are logged. To change this include a line such as the following before any messages or service requests are processed:
use ceLTIc\LTI\Enum\LogLevel;
...
Util::$logLevel = LogLevel::Debug;
or, for version 4 and earlier:
use ceLTIc\LTI\Util;
...
Util::$logLevel = Util::LOGLEVEL_DEBUG;
The following methods include logging events:
-
Tool->handleRequest:- details of the request received (
LogLevel::Info) - reason for any error with the request (
LogLevel::Error)
- details of the request received (
- 'Http\HttpMessage->send' (
LogLevel::Infowhen successful,LogLevel::Errorwhen unsuccessful):- details of the request being sent (URL and POST parameters or message body)
- the response received (headers and any message body)
-
DataConnector\DataConnectorsub-classes:- SQL statements executed (
LogLevel::Debugwhen successful,LogLevel::Errorwhen unsuccessful)
- SQL statements executed (
Debug-level logging is also enabled when the debugMode property of the Tool or Platform object in use is set to true. When enabled, this setting will override the currently defined log level. See the Usage section for more information.
By default log messages are sent to the PHP error log (using the error_log function). However, an alternative logger client can be specified by calling the Util::setLoggerClient method. The alternative logger client must be PSR-3 compliant. For example, the following code illustrates how Monolog could be used to send logs to a specified file:
use ceLTIc\LTI\Util;
...
$logFile = ini_get('error_log'); // Specify location of log file
$loggerClient = new Monolog\Logger('MyLogger');
$streamHandler = new Monolog\Handler\StreamHandler($logFile);
$streamHandler->setFormatter(new Monolog\Formatter\LineFormatter("[%datetime%] [%context.ip%] [%level_name%] %message%\n", 'd-M-Y H:i:s T', true));
$loggerClient->pushHandler($streamHandler);
Util::setLoggerClient($loggerClient);
...
An ip property of the logger context is available for inclusion in the formatting of a log entry (as in the above example); it is set to the IP address of the user.
(See the documentation for the logging client being used for further information.)
© 2025 Stephen P Vickers. All Rights Reserved.