Log Class
The log class allows you to write messages to the log files.
Configuration
The log files are being placed in the folder specified by the attribute 'log_path' in the config.php. You can also set the 'log_threshold' and the 'log_date_format' parameters there.
These settings can also be changed on the fly by using the Config Class.
log_threshold |
constant |
Fuel::L_WARNING
|
Can be any of the following: Fuel::L_NONE, Fuel::L_ERROR, Fuel::L_WARNING, Fuel::L_DEBUG, Fuel::L_INFO or Fuel::L_ALL
|
log_file |
string |
null
|
Name of the log file. If not given, it will be generated in the form "./YYYY/MM/DD.log", with one logfile per day.
|
log_path |
string |
APPPATH.'logs/'
|
Where to put the log file(s). (Folder must be writable)
|
log_date_format |
string |
'Y-m-d H:i:s'
|
The date format for the log entries. This format must follow the PHP date format rules. See http://www.php.net/date for a complete list.
|
Usage
There are four predefined functions for ease of use:
Log::info()
Log::debug()
Log::warning()
Log::error()
They all use the main function Log::write() which requires the $level parameter as the first argument.
Some examples:
// Write a log entry with the level "Info" to the log file for the current day
$var = 1;
Log::info('Application started (with $var = '.$var.')', 'my_init_function()');
// Save the new value of $var to the log file, without the $method parameter
$var = 5;
Log::debug('$var is now '.$var);
// Send a warning log entry
if($var !== 1) Log::warning('Although $var has been changed, we will keep going.');
// Send an error log entry
if($var !== 1) Log::error('We cannot keep going, $var has been changed! :o');
// Finally, try to make a log entry with a custom $level
Log::write('Link', 'More info on https://fuelphp.com/');
Output
All log files are being placed in the defined folder (see above).
If you have configured a filename, all logs will be written to this file. You need to setup your own log rotation mechanism
(for example Linux logrotate) to avoid the file from becoming very large. If you have not configured a filename, log files
are composed in folders named by the current year followed by the month ("2013/06" for example)
with the day of the month as the filename ("15.php" for example).
The complete path to our example log file would be: APPPATH.'logs/2013/06/15.php'
The examples above would write the following code to the log file:
<?php defined('COREPATH') or exit('No direct script access allowed'); ?>
INFO - 2011-01-03 18:44:45 --> my_init_function() - Application started (with $var = 1)
DEBUG - 2011-01-03 18:44:45 --> $var is now 5
WARNING - 2011-01-03 18:44:45 --> Although $var has been changed, we will keep going.
ERROR - 2011-01-03 18:44:45 --> We cannot keep going, $var has been changed! :o
NOTICE - 2011-01-03 18:44:45 --> More info on https://fuelphp.com/
Note that the custom log level "Link" that was used to create the last line was mapped to "Notice". Since Fuel switched to using the Monolog package for logging, custom log levels are no longer supported, the mapping is there to provide backward compatibility with earlier Fuel versions.
info($msg, $method = null)
The info method allows you to write a log entry with the $level "Info".
Static |
Yes |
Parameters |
Param |
Default |
Description |
$msg |
required |
The message for the info log entry. |
$method |
null
|
Information about the method which created the log entry. |
|
Returns |
boolean |
Example |
$var = 1; Log::info('Application started (with $var = '.$var.')', 'my_init_function()');
|
debug($msg, $method = null)
The debug method allows you to write a log entry with the $level "Debug".
Static |
Yes |
Parameters |
Param |
Default |
Description |
$msg |
required |
The message for the info log entry. |
$method |
null
|
Information about the method which created the log entry. |
|
Returns |
boolean |
Example |
$var = 5; Log::debug('$var is now '.$var);
|
warning($msg, $method = null)
The warning method allows you to write a log entry with the $level "Warning".
Static |
Yes |
Parameters |
Param |
Default |
Description |
$msg |
required |
The message for the info log entry. |
$method |
null
|
Information about the method which created the log entry. |
|
Returns |
boolean |
Example |
// we send a warning log entry if($var !== 1) Log::warning('Although $var has been changed, we will keep going.');
|
error($msg, $method = null)
The error method allows you to write a log entry with the $level "Error".
Static |
Yes |
Parameters |
Param |
Default |
Description |
$msg |
required |
The message for the info log entry. |
$method |
null
|
Information about the method which created the log entry. |
|
Returns |
boolean |
Example |
// we send an error log entry if($var !== 1) Log::error('We cannot keep going, $var has been changed! :o');
|
write($level, $msg, $method = null)
The write method allows you to write a log entry with a custom $level.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$level |
required |
A custom Level. |
$msg |
required |
The message for the info log entry. |
$method |
null
|
Information about the method which created the log entry. |
|
Returns |
boolean |
Example |
// and finally, we create a log entry with a custom $level
Log::write('Link', 'More info on https://fuelphp.com/')
|
Procedural helpers
logger($level, $msg, $method = null)
The logger function is an alias for Log::write.
Parameters |
Param |
Default |
Description |
$level |
required |
A custom Level. |
$msg |
required |
The message for the info log entry. |
$method |
null
|
Information about the method which created the log entry. |
|
Returns |
void |
Example |
logger(\Fuel::L_INFO, 'My Message', 'SomeMethod');
|