Centralise PHP logs using log.jod.li

log.jod.li

It often happens that we need to gather logs from several sources. Centralising them in one simple server place is the purpose of the log.jod.li initiative. The logs are then visible almost instantaneously on the log.jod.li page or can be retrieved in a format similar to CSV (raw logs). For more details, please visit https://log.jod.li .

PHP situation

This page aims at showing how to use log.jod.li logs centralisation solution using PHP code.

Let’s say you have some PHP code that may not always run as expected. You want to track the unwanted outcomes and centralise their reading on log.jod.li . You could read your PHP logs directly from your server hosting your PHP scripts but you may have several servers running PHP scripts. In that case, you can add a call to log the situations. This is a simple GET call to a URL containing:

  • The server name to send the log to the right location. This will be en.log.jod.li .
  • The user name you created on log.jod.li .
  • The user key you received in your email box when you created your user. Make sure nobody else gets access to your key.
  • The log message that you want to record on the log.jod.li server. The maximum length of the message is 1700 characters. It must be passed url encoded so that the string is interpreted correctly by the server.
  • Optionally the application name so that you can filter your logs more easily. Max 32 characters.
  • Optionally the client guid so that you can filter on a given client. Max 32 characters.

Here is an example of URL:

https://en.log.jod.li/?api=a&u=toto&k=1234567890123456&a=phpapp&c=123456789&m=testing

Here, the user is “toto”, the key is “1234567890123456”, the application name is “phpapp”, the client guid is “123456789” and the log message is “testing”.

Let’s consider the following PHP code:

<html>
<header>
<title>
PHP Exception log.jod.li example
</title>
</header>
<body>
<h2>PHP Exception log.jod.li example</h2>
<?php
try{
        echo("Throwing an Exception");
        throw new Exception("Testing log.jod.li");
}
catch(Exception $e){ 
        echo($e->getMessage());
}
?>
</body>
</html>

If you are not reading the content displayed by the web page, you will not see that there was an Exception. This is why we will add a call to a logjodli function as shown below.

Send log with file_get_contents

What we can do to make use of log.jod.li in this case is using the file_get_contents function for a call to a URL similar to the one mentioned above. Here is the result:

<html>
<header>
<title>
PHP Exception log.jod.li example
</title>
</header>
<body>
<h2>PHP Exception log.jod.li example</h2>
<?php
function logjodli($message){
        try{
                $client = substr(gethostname(),0,32);
                $message = urlencode(substr($message,0,1700));
                $appname = "phpfilegetcontents";
                $url = "https://en.log.jod.li/?api=a&u=toto&k=1234567890123456&a=".$appname."&c=".$client."&m=".$message;
                file_get_contents($url);
                echo("<br/>Log sent");
        } 
        catch(Exception $e){ 
                //echo("<font color='red'>Failed to log with error: ".$e->getMessage()."</font>");
        }
}

try{
        echo("Throwing an Exception");
        throw new Exception("Testing log.jod.li");
}
catch(Exception $e){ 
        logjodli($e->getMessage());
}
?>
</body>
</html>

Note that:

  • $client can be something else than the host name. It could for example give an anonymous indication on the PHP script user.
  • $appname can be set using the URL called to reach this PHP script.
  • file_get_contents() is not the only way to call a GET url from PHP. curl and http_get are good alternatives.

Here is what this PHP script displays:

Output of the PHP Exception example

In general, the “Log sent” line is not needed.

Here is the outcome of the call on log.jod.li web page:

2018-09-12 12:36:04	hostname.example.com	phpfilegetcontents	Testing log.jod.li

Here is the outcome in the raw CSV format:

2018-09-12 12:36:04,hostname.example.com,phpfilegetcontents,Testing log.jod.li

Conclusion

This is a demonstration that you can log your messages by simply calling a url from a PHP script. This is especially useful if you have several servers hosting PHP scripts. You can then follow your logs from the https://log.jod.li page.