UWiX circle(75x81)First of all, let me introduce myself. My name is Nikolai and I'm a PHP programmer for UWiX. Ofcourse my knowledge reaches a bit further than PHP only. I'm also familiar with HTML, CSS and some other programming languages like Pascal/Delphi.

That said, let me explain what you can expect from me in the programmers blog. I want to write down my experiences during programming for Joomla! (and other types of programming) and show you methods I use while developing. I secretly hope that I will be able to make it a bit easier for other programmers to develop code. :-)

When developing plugins I've ran into the problem of debugging them correctly. Most of the time I want to debug some variable or output results. The print_r function seems to do the trick most of the time but the disadvantage of the print_r function is that it can screw up the page layout when testing. Well, not for a string variable but when outputting an array it can result in a lot of scrolling on the Joomla! page.

And what about methods that work in the background and do not produce any page output? In that case the print_r function is useless and logging to a file would be much easier! Sure, I can use the JLog function but takes several steps and switches before I can implement it in my source code. And sometimes I just want to view the written log data quickly.

To keep it simple I've created a small function (method) to include in the PHP file I'm working on.. Just a copy and paste will do and from within the PHP file I'll write values to a log file by using one line of code. Let me show you the function:

function writeLog( $varToPrint = '', $includeRequestVars = false, $appendToLog = true)
    $logFileName = "C:\\xampp\\htdocs\\debug_triggers.log";
    // Create the log strings
    $logStr  = 'Called from : ' . basename(__FILE__) . "\n";
    $logStr .= '$varToPrint: ' . "\n";
    $logStr .= print_r($varToPrint, true) . "\n";
    // Include $_REQUEST (form submit)?
    if ($includeRequestVars)
    	$logStr .= '$_REQUEST variables: ' . "\n";
	    $logStr .= print_r($_REQUEST, true). "\n";

    // Append to log or overwrite?
    $append = ($appendToLog) ? FILE_APPEND : 0;
    if (file_put_contents($logFileName, $logStr, $append))
        // Write succes message to Joomla!
        \JFactory::getApplication()->enqueueMessage( "Succesfully logged to " . $logFileName, 'notice' );
        // Write error message to Joomla!
        \JFactory::getApplication()->enqueueMessage( 'Cannot write to logfile: ' . $logFileName, 'error' );


 How do you use it? Simply add this line in the function where logging is needed:

$this->writeLog( $variableToDebug );

 Do make sure you adjust $logFileName to a path on your own system.


If you want to know what is submitted by Joomla! when debugging you add 'TRUE' as the second parameter for the _writeLog function so all _REQUEST variables are written to the log file too.

$this->writeLog( $variableToDebug, true );


Last option for this function if the parameter to append your logging to the log file or just overwrite it. When using FALSE as third parameter each new _writeLog call will overwrite the current log file.

$this->writeLog( $variableToDebug, true, false );


When the logging is succesfully completed a nice message will appear at the top of your Joomla! page stating the file is succesfully written. If something went wrong writing to the log file this will also be shown. That way you can always check if the log file is updated or just not written.

Have fun with this function and keep on 'happy coding'!