KLogger: A Simple Logging Class for PHP

Update: You can find KLogger on github.

Since the latest release of wpSearch, a couple issues have cropped up and are slated to be fixed shortly. Some of the issues, although, are a bit harder to catch without a good set of debugging tools for PHP. The classic example of such a tool would be a log file logger.

As soon as I realized the need for a logger while developing wpSearch, I decided to check to see if one had already existed on the internet — someone had surely created a simple logging class and made it available before .. I would think. I’m a believer in the C programmer’s motto “build upon the work of others”, so checking to see if someone else has done the same thing prior to starting a project comes naturally.

After a little browsing, I couldn’t find what I was looking for. Put plainly, I wanted a logging class that:

  • Checked permissions prior to logging
  • Had a priority heirarchy built in ( Debug, Info, Error, and Fatal Message Levels)
  • Logged to plain old text files
  • Managed file handling cleanly (Open the file once, close the file once)
  • Managed resources (make sure the file gets closed)

Not too complicated. This logging class would require around 100 lines of code.

Another option involved using logging functions available in Zend, or the logging class provided in PEAR. These libraries were a little overkill for what I needed, so I passed.

I decided to write of the class myself, and it has turned out to be pretty handy. I figured someone else would probably find it useful as well, so I have posted it on it’s own project page. Click here to go to the KLogger project page.

Using KLogger is very straight-forward. Here’s an example:

require_once 'KLogger.php';
$log = new KLogger ( "log.txt" , KLogger::DEBUG );

// Do database work that throws an exception
$log->LogError("An exception was thrown in ThisFunction()");

// Print out some information
$log->LogInfo("Internal Query Time: $time_ms milliseconds");

// Print out the value of some variables
$log->LogDebug("User Count: $User_Count");

Depending on the priority level that is used when instantiating a new KLogger, only certain messages are actually logged to the file. If the most verbose priority level is used, ( KLogger::DEBUG ), all messages are logged. If the least verbose level is used ( KLogger::FATAL ), only Fatal-level errors are logged. Here’s a breakdown of the most verbose level to the least-verbose level:

  • Debug (Most Verbose)
  • Info …
  • Warn …
  • Error …
  • Fatal (Least Verbose)

A sixth level is also available: KLogger::OFF, so if you need to release a site without logging, you can simply set the priority level to Off, and not have a single message logged. (In fact, a log file will never be opened).

There isn’t too much documentation on the class right now, but it shouldn’t be too hard to figure out.

A couple comments on design: A few log class examples I’ve seen implement a singleton pattern, essentially locking the programmer into using one logger at all times. I usually lean towards letting the developer decide those things: If he wants to use a single logger, he can create a global log object in his application.

An screenshot of KLogger:

KLogger in Action

I think at this point I’ve actually done a better job describing KLogger in this post than on the project page. A few more details are listed there, along with the download. If you find it useful, leave a comment and let me know.

Click here to go to the KLogger project page.

This entry was posted in PHP Development, Tools and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Pingback: Updates on KLogger, wpSearch 1.5.6, and More

  • WebBird

    How about the performance using KLogger?

  • http://www.51ajax.net paravoice

    Considering that log4PHP is still in incubation, and I just need a simple logging module,I decide to use it.
    Thank you.
    And I think I need log messages to different logfiles every month or week, since the log file could be too large.
    something like this:
    $log = new KLogger ( “log_200912_01.txt” , KLogger::DEBUG );

  • chrisg

    simple and useful….thx a lot….

  • http://gmail.com nick fox

    On a rather unrelated note, what method would you recommend to use functional logging in a class without tying the class to another external class or function, that would seem to break the OO principle of portable code. Ive seen examples where logging objects are passed to the initialisation of other classes and objects created in other objects. but this all seems to tie one class to another?
    any advice appreciated

  • Pingback: SoftArea51 Blogs » How to integrate your custom shopping cart with PayPal Website Payments Standard using PHP

  • Pingback: Logging Visitor’s IP with PHP « Tournas Dimitrios

  • Some dude!

    Just what I was looking for! Thanks!

  • Jason


  • http://www.diigital.com Mike Healy

    I’ve just spent a few minutes with KLogger so far, but I think we’ll be happy together. It doesn’t seem over-engineered 

  • Suranga Fernanado

    Great Work.  

  • http://blog.chrisdlangton.com/index.php?article=all Christopher D Langton

    the php class i made to log errors, exceptions, info, and intruders will separate each detail per line with a comma so the log file can be viewed as a csv. also in my config i can change the comma to a pipe or whatever your preference is.. 

    Would be a good addition to your script.

  • Pcythe

    Been playing around with KLogger for a few minutes now… its awesome!!! I just added a function to get the  client’s IP and the Logged in User… 

    Pretty simple to use… nice job!!!

  • Bill

    Thanks a LOT Kenny. I’m sort of new to php and this logging class will of course be a big time saver at the moment. 

  • Lolowned

    Hey – is KLogger thread safe, or is that inherently built into PHP? If each http request to my application created a global logger instance (all of which used the same file), would there be race conditions reading/writing to the same file from each concurrent request?

  • Omid

    wonderful !

  • stukerr

    Great class, saved me some work so thanks! I was having a problem getting the file to open more than once, so it would write once then couldn’t open it again, turns out it was SE Linux stopping me, so if anyone has the same problem chcon solved it!

  • Kalaiselvan