Software Logging

Logging in PHP with PHPLogger

Logging

Logging is one of the most important parts of software development. When the project getting larger, debugging without a logging system is completely impossible. Developers really need a sufficient logging system!

Logging means store software errors (alerts, warning and so on) with a perfect details. They just could be stored in a directory (In my opinion, the best one), database or even be sent to an email (In my opinion, the worst one!).

Logs illuminates the way of debugging. When error occurs, you won’t tell unknown users the details,  just say “Oops!” or something like that, and log all details you need, then you can find the bug easily…

PSR-3 Standard

There is a good standard for logger systems in PHP at http://www.php-fig.org/psr/psr-3. Many loggers in PHP implements its interfaces and abstract classes. You must read it if you want to implement your own logger.

 

PHPLogger

PHPLogger is a easy-to-use and neat logger you should be familiar with. You may see it or fork it on Github.

Installation using Composer

It’s strongly prefered to install package using composer! If you are not familiar with it, you may read this blog post about How to use composer in PHP projects. Add this package to your composer.json:

"require": {
    "miladrahimi/phplogger": "dev-master"
}

or if you prefer command line, change directory to project root and:

php composer.phar require "miladrahimi/phplogger":"dev-master"

Installation manually

Get a copy of the package source code and PSR-3 Log package, then copy them into your projects. Caution your autoloader has to support PSR-4 Log standard to autoload this two package appropriately.

How to use PHPLogger?

PHPLogger has potential to store logs into different kinds of storage but for now you only can use directoy (filesystem) to store them. Following codes demonstrate how to define directory storage:

$dir = new MiladRahimi\PHPLogger\Directory("./logs/");

Then you can use PHPLogger with the directory storage which is defined above:

$logger = new MiladRahimi\PHPLogger\Logger($dir);

Of course you can inject $dir with setStorage() method. Now you may store your first log:

$logger->alert("This is an alert!");

Alert is not only supported log level, so you may see all supported log level out there in PSR-3 page.

 

Summary

  • Logging means log (store) all errors with perfect details.
  • Logging make debugging dramatically easier.
  • PHPLogger can be a good logger package for PHP developer.

Published by

Milad Rahimi

I’m a software engineer with some big targets, I love what I do and nothing else matters…

Leave a Reply

Your email address will not be published. Required fields are marked *