Post Thumbnail

How to View Remote CodeIgniter Log Files in the Browser and Postman

Introduction

In this article, we’re going to look at how to access the log files of a CodeIgniter application via a web browser or Postman (API calls). This is a follow-up article to this first article that I published on medium.

Being able to easily read log files on a live server is a vital tool in a developer’s toolbox. It can boost the quality of emergency readiness and response.

We’ll be achieving our aim with the help of the codeigniter-log-viewer library. Let’s add it to our existing CodeIgniter project, via composer:

1
composer require seunmatt/codeigniter-log-viewer 

This goes without saying but this post assumes you have a deployed CodeIgniter project with log files that you want to access.

Viewing Log Files in the Browser

Following the installation of the library, we need to create a controller that will be mapped to a route on our web application. Let’s call the controller LogViewerController.php.

In the controller, let’s instantiate the CILogViewer class and make use of it in the index() function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
private $logViewer;

public function __construct() {
    $this->logViewer = new \CILogViewer\CILogViewer();
    //...
}

public function index() {
    echo $this->logViewer->showLogs();
    return;
}

Finally, we’re going to map the controller function to a route by adding the following entries to application/config/routes.php:

1
$route['logs'] = "logViewerController/index";

Please note that the /logs route should be protected. That’s it! A visit to /logs on our application will produce a view like this:

screenshot of the logs page

From the view, it’s possible to delete the current log file or all the log files.

You can also download the log file to your local system. Note that if the file is too large for displaying, you’ll have to download it locally for viewing.

Now, let’s get to an even more interesting part!

Viewing the Log files in Postman

So with v1.1.0 of codeigniter-log-viewer, we can actually make API calls to access our logs remotely. The controller set up is just the same as above.

How is this possible? It is, because the library uses query parameters as commands that are interpreted internally to provide the appropriate JSON response.

To get all the log files that are available, we need to make an API call to this endpoint via Postman or other API clients:

1
/logs?api=list

This will return a JSON response containing the list of log files:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "status": true,
    "log_files": [
        {
            "file_b64": "bG9nLTIwMTgtMDgtMjQucGhw",
            "file_name": "log-2018-08-24.php"
        },
        {
            "file_b64": "bG9nLTIwMTgtMDgtMjMucGhw",
            "file_name": "log-2018-08-23.php"
        }
        
]}

Furthermore, to view the content of a log file, we need to make a call this endpoint:

1
/logs?api=view&f=bG9nLTIwMTgtMDEtMTcucGhw

The name of the file is obtained from the previous call and is set as the value of the f parameter (f stands for file). This will return a JSON response containing the logs as an array and a list of available log files as well:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
    "log_files": [
        {
            "file_b64": "bG9nLTIwMTgtMDEtMTkucGhw",
            "file_name": "log-2018-01-19.php"
        },
        {
            "file_b64": "bG9nLTIwMTgtMDEtMTcucGhw",
            "file_name": "log-2018-01-17.php"
        }
    ],
    "status": true,
    "logs": [
        "ERROR - 2018-01-23 07:12:31 --> 404 Page Not Found: admin/Logs/index",
        "ERROR - 2018-01-23 07:12:37 --> 404 Page Not Found: admin//index",
        "ERROR - 2018-01-23 15:23:02 --> 404 Page Not Found: Faviconico/index"
    ]
}

We can delete a single log file by calling this endpoint:

1
/logs?api=delete&f=bG9nLTIwMTgtMDEtMTkucGhw

or this endpoint to delete all the log files:

1
/logs?api=delete&f=all

Conclusion

Now, that we’ve seen how good the library is and what it can offer. It’s time to incorporate it into our existing projects and give it a star on GitHub.

Seun Matt

Results-driven Engineer, dedicated to building elite teams that consistently achieve business objectives and drive profitability. With over 8 years of experience, spannning different facets of the FinTech space; including digital lending, consumer payment, collections and payment gateway using Java/Spring Boot technologies, PHP and Ruby on Rails