Log collection – CodeIgniter & Phplogger

Error collection with Phplogger is fairly easy, you would need to install Phplogger client from the official page and a working Phplogger token, which can be found on the Product setup page.

In case you don’t have your development environment set up, here is a nice tutorial on the basic PHP project configuration with Docker – https://blog.phplogger.com/2019/07/11/creating-a-php-project-with-docker/


To install the Phplogger client you would need Composer dependency management. Go to your project folder and run the composer require command in the terminal.

composer require phplogger/phplogger

Next we need to enable CodeIgniter framework to use composer autoloader. Locate the configurations file, which usually can be found at application/config/config.php. And change composer_autoload value to TRUE

$config['composer_autoload'] = TRUE;

CodeIgniter does not provide any functionality to inject custom logger to the system, but allows to override the default one by creating a logger class in core folder. Create a file as follows ./application/code/Log.php and paste the contents. Remember to replace b444ac06613fc8d63795be9ad0beaf550 with your token from Product setup page

# ./application/code/Log.php

defined('BASEPATH') OR exit('No direct script access allowed');

class CI_Log
    private $logger;

    public function __construct()
        $this->logger = new \PhpLogger\Logger(

    public function write_log($level, $msg, array $context = array())
        $this->logger->log($level, $msg, $context);

Note that by default CodeIgniter log_message function supports only 2 arguments: $level and $message. This make it impossible to take advantage of additional context logging. The best approach to enabling this functionality is to create your own global logging function with helpers.

First create a file at ./application/helpers/phplogger_helper.php with the following contents


    function phplogger($level, $message, array $context = array())
        static $_log;

        if ($_log === NULL)
            // references cannot be directly assigned to static variables, so we use an array
            $_log[0] =& load_class('Log', 'core');

        $_log[0]->write_log($level, $message, $context);

Then add this helper to autoload on CodeIgniter start. In ./application/config/autoload.php add ‘phplogger’ to the list of helpers

$autoload['helper'] = array('phplogger');

Now you can log your custom messages with phplogger global function like this

phplogger('error', 'Unexpected error happened', $context);

Good job! Now everything you log will be available to you at phplogger.com.