Starting a Website with Phplogger

Developing a new website is very exciting endeavor. Unfortunately during that development many things can go wrong. Debugging errors and finding logically wrong behavior is the key to developing good software, this is where Phplogger comes to help. By incorporating data collection right away, you can ensure a steady development pace and a stress-free deployment of you project.

Setting up your environment

Easiest development of PHP systems usually happens in a unix-like development environment. We personally use Docker for this, which helps isolate your project development from the rest of the host system. A fairly simple Docker setup with Ubuntu, PHP and Apache server can be found in an example docker gist.

First we should create a project folder

mkdir example-phplogger-project
cd example-phplogger-project

Then copy the docker file to use in our project. On Linux you can copy using WGET directly from Gist. On windows just do it manually.

Now we need to create a Docker image and run the container from it.

docker build -t apache2-php .

Running docker from the container is, as well, a single command

docker run -d -p 8080:80 --name example-phplogger-project -v ${PWD}:/var/www/html apache2-php

Now we have a running apache2 with PHP on our machine, and can start actually creating the project. Opening http://localhost:8080/ in a browser should open a website that is running docker.

To connect to you docker container, use the following command

docker exec -it example-phplogger-project bash

Setting up Phplogger

This part is the easiest in the bunch. Phplogger client installation is a single line command

composer require phplogger/phplogger

Then let’s a simple file and try if the PHP actually runs our files properly. Create index.php file and let’s add some starter code lines.

# include project autoloading
require __DIR__ . '/vendor/autoload.php';
# create logger object
$logger = new \Phplogger\Logger('authentication token');
# write the code
$logger->debug('first logs of my project');

Make sure to replace authentication token with an actual token from product setup page. Now open http://localhost:8080/index.php and it should be completely blank. By opening the page you have sent your first logs to Phplogger server! The logs should appear shortly at the Log Stream page.

Creating restricted pages

Now let us create restricted pages and log all the progress we’re doing into Phplogger tool. First let’s create a simple bootstrap.php file, which we will re-use for logger object creation. It should look like this.

# File: bootstrap.php
# include project autoloading
require __DIR__ . '/vendor/autoload.php';
# create logger object
$logger = new \Phplogger\Logger('authentication token');

Then we will have 2 simple pages – public page (index.php) and restricted page (private.php).

Let’s change the contents of index.php to a simple message “Hello, world!” with a little bit of logging. Not only will we be showing the message, but also sending that information to Phplogger.

# File: index.php
require __DIR__ . '/bootstrap.php';
# log the page view
$logger->debug('Page view');
# show the message
echo 'Hello, world!';

Now with every page view of index.php you will be getting an event in your Phplogger app console.


Now let’s get down to private.php file. What we want there is to create a PHP session and when there’s no user, require authentication.

# File: private.php
require __DIR__ . '/bootstrap.php';
# start session
# save the session
$logger->debug('Session', $_SESSION ?: []);
# check sessions
if (empty($_SESSION['authenticated']) === true) {
    $logger->info('Session empty. Showing login form');
    echo "
        <form action=\"/authenticate.php\" method=\"POST\">
          <input type=\"text\" name=\"login\" value=\"\">
          <input type=\"password\" name=\"password\" value=\"\">
          <input type=\"submit\" value=\"Submit\">
# log the page view
$logger->debug('Page view Restricted', ['user' => $_SESSION['login']]);
# show the message
echo 'Hello, world! Restricted.';

There are few points worth noting. First – we add a bootstrap file and start a PHP session. We log that session right away, because it’s always useful to have user session at your fingertips. If the person is not authenticated, we show them a login form. This form is a very simple one, but we log that action right away. If the user is authenticated, we log that we showed him/her a restricted page, and additionally save the login of that user. Neat!

The next part is authenticate.php file, which actually does the login action. We only have one user, so we’ll do the simplest variation of authentication.

# File: authenticate.php
require __DIR__ . '/bootstrap.php';
# start session
# save the session
$logger->debug('Session', $_SESSION ?: []);

$adminLogin = 'John';
$adminPassword = 'qwerty';

if ($_POST['login'] === $adminLogin && $_POST['password'] == $adminPassword) {
    $_SESSION['authenticated'] = true;
    $_SESSION['login'] = $adminLogin;
    $logger->info('Authentication successfull.', ['user' => $_SESSION['login']]);
    header('Location: private.php');
} else {
    $_SESSION['authenticated'] = false;
    $_SESSION['login'] = null;
    $logger->info('Authentication failed.', ['user' => $_POST['login']]);
    echo 'Invalid credentials';

We create the PHP session and log it right away (yes, again) and now we check the parameters we received from the form. In case the login and password are correct, we note that the user is logged in, save some information to the session, we send an info event that the user is logged n (including his/her login) and redirect to a private page. In case of authentication failure, clear the session and show and error message. Also a neat peace of information is which user failed to login, let’s log that.

That’s it! You have a basic private / public pages and lots of information to play with in your logs.

What can I do with logs?

Well, lots of thing. First of all let’s take a look at what the Log Stream of our application should look like

Log Stream with application data

What is that? Well, this is every event and piece of information in the application, available as it’s happening. You can check that the application is actually doing what you programmed it to do, by following the flow of data in the Log Stream. This is great for development! You can check some processes in depth, to make sure that they actually work correctly. An example of single PHP page view could look like this

Single Restricted page view

Further exploration

The messages in our log entries are very short, have you noticed? This is generally a good idea to keep your messages short, and your context data informative. You can use that later to do deeper analysis of your application workflow. Check out the exploration section, and see the summary of your page views

Our, you can actually see, how many actual public / restricted page views you had, how many login forms you had, how many authentication failures were there.

Or check which users actually failed to authenticate

who failed to authenticate

In conclusion

The code of this example project is available on GitHub.
Try it out Now! 🙂