Zend Framework 2&3 / Laminas Debugging and Logging Module
This module can send debugging information from your code to either a temporary file, email or permanent error log.
Installation
With composer-
In the root of your application enter:
$ composer require krytenuk/logger
-
Enable it in your `application.config.php` or `modules.config.php` file.
For Zend Framework 2 `application.config.php`:
<?php
return array(
'modules' => array(
// ...
'FwsLogger',
),
// ...
);
For Zend Framework 3 / Laminas `modules.config.php`:
<?php
return [
'FwsLogger',
// ...
]; - Copy `./vendor/krytenuk/logger/config/fwslogger.local.php.dist` to `./config/autoload/fwslogger.local.php`.
-
In `./config/autoload/fwslogger.local.php` change the following.
<?php
return [
'fwsLogger' => [
'infoLogger' => [
'file' => 'logs/variable_event.log', // the log file and path of the info logger. Default to 'logs/variable_event.log' relative to the root folder of your application.
],
'errorLogger' => [
'file' => 'logs/error.log', // the log file and path of the error logger. Default to 'logs/error.log' relative to the root folder of your application.
],
'emailLogger' => [
'to' => 'toEmail@example.com', // the email address to receive log reports.
'from' => 'server@example.com', // the email address of the server, shows as sender of email. If unsure use the same email address as 'to'.
'subject' => 'Debug log message', // the subject line of the email (optional).
],
],
]; - Create the folder(s) for the log files. As mentioned above by default this is a folder called "logs" in the root of your application.
Usage
There are three separate logging classes that can log to either a temporary file "\FwsLogger\InfoLogger", a permanent log file "\FwsLogger\ErrorLogger" or to an email "\FwsLogger\EmailLogger".
As these all use static functions they can be used anywhere in your application. As these loggers don't use php STDOUT nothing is rendered to the browser, ideal for debugging live websites.
-
Logging to a temporary file.
"\FwsLogger\InfoLogger" logs to a temporary file, by default this is "variable_event.log" (see post installation above). This is possibly the most common logger used during development.Usage:
<?php
\FwsLogger\InfoLogger::vardump($variable); // Performs a php var_dump to the log file.
\FwsLogger\InfoLogger::printr($variable); // Performs a php print_r to the log file.
\FwsLogger\InfoLogger::write($variable); // Sends a simple variable to the log file. -
Logging to a permanent file.
"\FwsLogger\ErrorLogger" like "\FwsLogger\InfoLogger" logs to a file, however this file is permanent and is not overwritten. By default this is "error.log" (see post installation above).Usage:
<?php
\FwsLogger\ErrorLogger::vardump($variable); // Performs a php var_dump to the log file.
\FwsLogger\ErrorLogger::printr($variable); // Performs a php print_r to the log file.
\FwsLogger\ErrorLogger::write($variable); // Sends a simple variable to the log file. -
Logging to email.
"\FwsLogger\EmailLogger" as it's name suggests sends log info in an email.Usage:
<?php
\FwsLogger\EmailLogger::vardump($variable); // Performs a php var_dump to the log email.
\FwsLogger\EmailLogger::printr($variable); // Performs a php print_r to the log email.
\FwsLogger\EmailLogger::write($variable); // Sends a simple variable to the log email.