Integrate monolog in codeigniter

Default codeigniter log package is good but sometime you need to extend default codeigniter logging system. Then you can simply extend by using core classes. I have a requirement to integrate monolog in codeigniter. You can check more about monolog package. I am using Codeigniter-Monolog-Plus to integrate monolog in codeigniter site. This package i found to work with my site and updated recently. And also i added some fixed. Let’s see how we can integrate monolog in codeigniter.

Monolog Codeigniter

1. First download the package Codeigniter-Monolog-Plus form github.

2. Add the below code in your root composer.json

{
    "require": {
        "monolog/monolog": "^1.22"
    }
}

3. Now run below composer command to get the package. To run command grab the path of your site root. like in CMD or Terminal c:/xampp/htdocs/mysite Then run below command. Composer must be installed on your system.

composer require monolog/monolog

4. Now after execute command successfully you will get a newly created vendor directory on your site root. Move this directory to under /application/. So your vendor directory will be at /application/vendor.

5. Now edit your application/config/config.php and set “composer_autoload” setting to TRUE. Then This codeigniter will auto load application/vendor/autoload.php.

$config['composer_autoload'] = TRUE;

6. Now copy application/core/Log.php and application/config/monolog.php into your codeigniter application.

Now we need to change some default settings. open application/config/monolog.php and replace the default and set below settings

// Remove ci_file else will create two files in log.
$config['handlers'] = array('file');
// Set log path to application/logs/
$config['file_logfile'] = APPPATH.'logs/ci.log';

Now you are all set with monolog codeigniter. Use log_message() as normal in codeigniter to log error, debug and info messages. Log files will be stored in the application/logs folder.

Conclusion

There are many packages included in monolog package. But i used only log package which i am sharing, also you customize monolog log messages but you need to customize package files. There are many function to make codeigniter logs beautiful. Hope this is extensive info to done codeigniter monolog logs.

Cheerss! happy coding!

Adam Brown
Please follow and like us:

Leave a Comment