Update: You can find KLogger on github.

Project Update: See the end of this post for details about KLogger’s current status


KLogger is a simple-to-use PHP text file logging class. It was made to have basic logging functionality, be lightweight, and be safe to use (permissions-wise). The reason for its creation is two-fold: I needed a simple logging class with priority hierarchies ( Debug, Info, Warn, etc .. ), and I couldn’t find it on the internet.

For small web applications or development sites, the use of logging libraries isn’t necessary. All you need is something like KLogger, a simple logging class.

KLogger supports:

  • Safe logging (no PHP errors due to permissions)
  • Smart file management (closing up resources)
  • Priority heirarchies (OFF, DEBUG, INFO, WARN, ERROR, and FATAL)

Basic usage would look like this:

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");

Using KLogger will eventually generate a log file that looks something like this one, which is the actual output from wpSearch, the Lucene based search plugin for wordpress. Tracking down a couple outstanding bugs became much easier once I started using KLogger.

KLogger in Action

If you happen to be wondering what that log reader is, it’s KTail, a yet-to-be-released log “tailer” written in C#. It is multithreaded, tabbed, and super-useful for reading log files which get updated frequently. It displays changes to almost instantly. (It has yet to be tested on a gigantic log file, though). It’s a side project that doesn’t get a whole lot of attention.

If you think KLogger is what you need, download it (below), and let me know your thoughts by sending an email to katzgrau@gmail.com or leaving a comment on CodeFury.

Update: The KLogger project has moved to GitHub as of June 22, 2010. The original file download will remain intact below. The GitHub equivalent (a tagged release of version 0.1) is available at github too.



To check out the GitHub project, go to:


  • Pingback: KLogger: A Simple Logging Class for PHP

  • Pingback: Processing recordings with PHP « The Tropo Blog

  • Matt

    I’m planning to use KLogger (http://codefury.net/projects/klogger/) for logging financial data and I’m wondering if it can handle race conditions properly.

    There would be instances where multiple users would be accessing the site simultaneously and I’m wondering if the logger will mess up in those situations.

  • http://rocastraja.blogspot.com javier del castillo

    really thank you very much it’s exactly what i need,

  • Pingback: Making development in PHP less painful with logging

  • http://www.josscrowcroft.com Joss Crowcroft

    Amazing stuff, wish I’d found this at the beginning! Finally can stop wasting time writing logging functionality that never works like I need.

  • http://www.mkfmn.com/?codefury.net Matt Kaufman / mkfmn

    Great post! I am extremely surprised at how many people don’t even use monitoring, tracing/debug and or log or timing tools for their applications that are out-grown and they don’t know why their sites are no longer working/ (scaling or able (scalable)). It almost makes me laugh, so thank you for the wonderful and sensible post ……………. :).

    XDEBUG is great too as well as ettercap.

  • Pingback: 7 Librerie PHP per Creare Applicazioni Web senza Usare un Framework | PillolHacking.Net

  • Josh

    What is the license for this? I would like to use it in a project at work, but didn’t see a license listed anywhere. Public domain? BSD?

  • WebBird

    I am using the good ol’ KLogger v0.1 (with some additions/changes I made), and I’m really happy to see that work is going on. :) One suggestion: I’d like to have different output files for different levels (.err for fatal, warn, …, .log for complete debug tracing, for example). Maybe this can be achieved by creating different object instances (didn’t take a deeper look into the new class yet), but I think this would be a nice feature. What do you think?

  • http://aretimes.com Arelowo

    Thanks for the code my friend. Your going to make my project a whole lot better.

  • http://blog.opensourceopportunities.com James

    Author of KLogger,

    Great tool, It’s been a great help in reverse engineering some PHP code, but can you please slap an Apache 2.0 license on this thing so that we can use your plugin without worrying about copyright issues? I’d hate to have to find/replace it with something else when it’s working so well.

    We’d really appreciate it! Thank you again!


    P.S. (In case it’s not clear to you, some people won’t use your plug-in if there is no license).

  • http://teamnirvana.com/blog Suneel

    Can’t thank enough for sharing such a simple logging class. I was wondering if logging classes need to be written again. Thanks to you I don’t have to reinvent the wheel.

  • http://phpflow.com/ Phpflow

    PHP flow

  • Darkmoluy

    please accept

  • Rick

    Perfect.  Exactly what I needed – saved me time and some brain cells.


  • Jerdumz

    wonderful security

  • Subs

    Thanks mate!

  • Dexterparayno06


  • Julien

    I just started using this class and like it very much. Since my app will be under pretty high load, is KLogger handling well multiple threads logging to the same file at the same time? (any lock issue or risk the file not written)

  • katzgrau

    Typically in production environments you’re only logging errors, so hopefully your app doesn’t have so many errors you hit file locking issues :)

    But in reality, if you think this is an issue it might be wiser to use php’s native error_log function which can avoid those issues (but you lose logging levels). Filelocking is an issue any PHP-based logging library will have.

  • Julien

    One of the needs is to trace every user’s interaction. So I’ll have to log a line for every single hit. 
    A way to manage it, is to open the file for every logging action. This will happen a lot in debug mode, but only at the end of the hit in production, which will reduce the risk.
    What happens if a thread wants to write while an other one is writting? An error, shall I wait and retry in that case?
    I am surprised this kind of issue is not addressed in PHP …

  • katzgrau

    It’s really the same sort of issue you’d get with any language, you just need a way to handle it. KLogger doesn’t try too hard to deal with that issue, mainly because it introduces dogpiles and other things a script-based  logging class shouldn’t worry about.

    You might want to consider using another mechanism to log user interactions though. If you plan on having lots of interactions, maybe Riak or Cassandra. If not, maybe a table in the DB.

  • http://salvatorecapolupo.it/ Salvatore Capolupo

    Thank you very much for this useful class :)

  • Joshuadimacali50

    Thaksss .. 

  • Joshuadimacali50


  • Max

    I added following class my copy of you nice code.
    It is called from the log function to include traceback information.
    The init function of the class has default this turned of, but the number of trace back levels is part of the class initialization.

    private function _format_backtrace($levels=0) {

    print “level $levels”;

    if ($levels == 0) {

    return “”;


    $traceback = debug_backtrace();

    $v = array();

    $traceback = array_reverse($traceback);


    foreach ($traceback as $k => $tb) {

    if (isset($tb[‘class’]) && $tb[‘class’] == “KLogger”) {




    if (isset($tb[‘class’]))


    $out = $tb[‘class’] . $tb[‘type’] . $tb[‘function’];


    elseif (isset($tb[‘function’])) {

    $out = $tb[‘file’] . “->” . $tb[‘function’];


    else {

    $out = $tb[‘file’];


    $out .= ‘ -> ‘ . $tb[‘line’];

    $v[] = $out;

    if ($count >= $levels) {

    return implode(‘:: ‘, $v);



    return implode(‘:: ‘, $v);


  • Kris Brixon

    Simple, clean, worked first time out of the box. Exactly what I needed and nothing more. Thanks for sharing the logger class.

  • Sven

    Nice class. It would be good if you could also add the possibility to specify a special file name. Default should be the date like now but one should be able to change this.

  • jrpinote


  • Sutirtho Sen

    Very useful class.. I was thinking of building one of my own then I found this.. Thanks for putting the hard work and also giving it for free..

  • dedengmusni159


  • VeraGr

    I add 3th (unnecessary) parameter

    public function __construct( $filepath , $priority, $dfmt )

    and use it (after your initialisation):

    $this->log_file = $filepath;
    $this->MessageQueue = array();
    $this->priority = $priority;
    if ($dfmt){
    $this->DateFormat = $dfmt;

  • Ha*cker

    can i join this?

  • Ha*cker

    how can