Leafiny Documentation
Logs
The logs are managed by the Leafiny_Log module.
Quick log
Anywhere in the code, use App::log
to log any data in the default log file:
App::log('azerty');
The default log file is var/log/system.log
. The default log level is INFO. Set the log level in the second argument:
App::log('azerty', Core_Interface_Log::DEBUG);
Expected levels:
- CoreInterfaceLog::EMERG : Emergency: system is unusable
- CoreInterfaceLog::ALERT : Alert: action must be taken immediately
- CoreInterfaceLog::CRIT : Critical: critical conditions
- CoreInterfaceLog::ERR : Error: error conditions
- CoreInterfaceLog::WARN : Warning: warning conditions
- CoreInterfaceLog::NOTICE : Notice: normal but significant condition
- CoreInterfaceLog::INFO : Informational: informational messages
- CoreInterfaceLog::DEBUG : Debug: debug messages
Add log in a custom file
Use a custom log file name with Log::file
method:
Log::file('azerty', Core_Interface_Log::INFO, 'hello.log');
Here the log file is var/log/hello.log
.
Note: App::log is a shortcut to Log::file without the possibility to set the file name.
Add log in the database
The Leafiny_Log module allows to add a log message in the database. This is useful for accessing the log in the admin.
Log::db('azerty');
The log messages are stored in the log_message table.
Access the log messages in the Admin > Logs section of Leafiny Admin.
Avoid to log exceptions in the database log system, add only informative messages.
Custom log system
Create your custom log system by overriding the log_file model class. This class is instancied when App::log
or Log::file
are used.
This is useful for adding an email alert or a third party log tool like Sentry or New Relic.
/** modules/Vendor_Module/etc/config.php **/
'model' => [
'log_file' => [
'class' => Vendor_Log_Model_File::class
],
],
<?php
/** modules/Vendor_Module/app/Vendor/Log/Model/File.php **/
declare(strict_types=1);
/**
* Class Vendor_Log_Model_File
*/
class Vendor_Log_Model_File extends Log_Model_File
{
/**
* Add Log
*
* @param mixed $message
* @param int $level
* @param string $logFile
*
* @return int
*/
public function add($message, int $level = self::INFO, ?string $logFile = null): int
{
// Custom code
return parent::add($message, $level, $logFile);
}
}
See Leafiny Sentry Log module for a full example.