Log4PHP with CodeIgniter

Tudip Technologies

24 June, 2016

You developed that fancy CI website but time and again an ugly notice/error message is displayed which annoys client. As a developer, you know that these messages are harmless but you still want to keep an eye on them and hence do NOT want to suppress them in php.ini. You can use log4php to achieve this objective where you still have access to all the logs but clients do not see them. Please follow the steps below:
1- To use log4php inside your project, you have to download the log4php with CI from the following link

https://github.com/fukata/ci-log4php (You can use SVNGit or simply download it in zip format)

Lets say you extracted the zipped code inside a folder named fukata-ci-log4php
2- Then place fukata-ci-log4php/ci_log4php folder inside the third_party folder.
 /application/third_party/ci_log4php/ (note it is _ and not -)
3- Move the MY_Log.php file from the fukata-ci-log4php/ci_app/application/libraries inside the libraries folder of your application. If you have subclass_prefix specified as APP_ in the config.php of your application, you would have to rename this file as APP_Log.php
4- Move the fukata-ci-log4php/ci_app/application/helpers/log4php_helper.php which is the helper class of log4php, inside the helpers folder of your application
5- Create a log folder named as logs to store all logs at the time of execution. You can create it right inside application folder.
6- Move log4php.properties file from fukata-ci-log4php/ci_app/application/config/ inside your application/config folder. In this file, specify the folder path and do not change anything, unless you know what you are doing 🙂
log4php.appender.default.file = C:/xampp/htdocs/spiderman/application/logs/%s.log
7- Now we edit log_threshold inside your application/config/config.php
 $config['log_threshold'] = 4;
Below is the list of all the log_threshold values and their meanings:
If you have enabled error logging, you can set an error threshold to
determine what gets logged. Threshold options are:
     0 = Disables logging, Error logging TURNED OFF
     1 = Error Messages (including PHP errors)
     2 = Debug Messages
     3 = Informational Messages
     4 = All Messages

Now, how to use this feature inside your project
To use log4php you have to write a line in your controller
 error_reporting(0); //This would ensure that all the errors are not displayed on screen and are instead directed to log4php