PHP Logging
- Digital Engineering
PHP Logging
Logging is one of the basic requirements of every projects. Logging needs to manage system level errors, data errors for business logic etc. It can be done easily in most of MVC frameworks. Logging can be save in file or in database based on requirements.
Core PHP Example
Find the attached logger.php file to use in any core php project or custom project. This provide feature to use logging in files. Example to save logs in files by logger class. You can manage logs based on staging or production environment with some global setting or variable. Follow the below steps to use it.
1. Download the logger class and copy in your project folder.
2. In global config file or bootstrap file, Include the logger class and make a instance.
1 2 3 4 5 6 |
$enable_logs = true; include_once 'logger.php'; $log = logger::getInstance(); $log->logfile = '/tmp/errors.log'; |
3. Now you can use logger class function to add log anywhere in your project.
1 |
$log->write('An error has occured', __FILE__, __LINE__); |
Yii2 Framework Logging ( Advance Template )
Yii2 framework provide inbuilt easy way to manage logging based on files, database and email. Follow the below steps to use it.
1. You need to add below settings in common/config/main.php file.
2. Copy the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
return [ // the "log" component must be loaded during bootstrapping time 'bootstrap' => ['log'], 'components' => [ 'log' => [ 'targets' => [ //if logs need to save in files [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning', 'info'], 'categories' => ['application'], 'logVars' => ['_POST','_GET'], ], // if logs need to save in database. You need to create a db table as well. [ 'class' => 'yii\log\DbTarget', 'levels' => ['error', 'warning'], ], //if logs need to send email [ 'class' => 'yii\log\EmailTarget', 'levels' => ['error'], 'categories' => ['yii\db\*'], // all database queries 'message' => [ 'from' => ['log@example.com'], 'to' => ['admin@example.com', 'developer@example.com'], 'subject' => 'Database errors at example.com', ], ], ], ], ], ]; |
3. //log table
1 2 3 4 5 6 7 8 9 10 11 |
CREATE TABLE IF NOT EXISTS `log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `level` int(11) DEFAULT NULL, `category` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `log_time` double DEFAULT NULL, `prefix` text COLLATE utf8_unicode_ci, `message` text COLLATE utf8_unicode_ci, PRIMARY KEY (`id`), KEY `idx_log_level` (`level`), KEY `idx_log_category` (`category`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1; |
4. After this setting, you can simply use functions Yii::trace(), Yii::info(), Yii::error() etc. Default logs saved based
on yii2 advance or basic template. Default location is frontend/runtime/logs/app.log
More details can be found here http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
Download : logger (1)
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s