1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Docs] Logging Results to a File

Discussion in 'Developing Plugins' started by Nick, Jan 18, 2010.

  1. Nick

    Nick Well-Known Member

    Hotaru CMS has a few debugging functions that can help you with testing. This tutorial details how to log results from your plugin to a file in the /cache/debug_logs/ directory.

    Note: To use this feature, enable Debug mode in Admin -> Settings

    1. Open a log file

    PHP:
    $h->openLog('log_test''a+');
    The openLog function takes two parameters: a filename without an extension, and an access mode (list of modes here). Using the 'a+' mode as in this example will create the file if it doesn't exist, open it, and append new data to the end.

    2. Write to the file

    PHP:
    $h->writeLog('log_test''This message will be written to the log file');
    Again, include the filename, and put your message in the second parameter. You can repeat this step for as many messages as you like.

    3. Close the file

    PHP:
    $h->closeLog('log_test');
    Example 1

    This example creates a log file to track admin users:

    PHP:
    if ($h->currentUser->role == 'admin') {
            
    $h->openLog('admin_visits''a+');
            
    $h->writeLog('admin_visits''Admin ' $h->currentUser->name ' visited ' $h->pageName);
            
    $h->closeLog('admin_visits');
    }
    Output:
    Example 2

    This example logs the execution time of a function. Note that the 'w' mode in openLog rewrites the file every time instead of appending the result to the end of the previous results:

    PHP:
        $h->openLog('speed_test''w');
        
    $start microtime(true);
        
    // insert big function here
        
    $end microtime(true);
        
    $time $end $start;
        
    $h->writeLog('speed_test''This function took ' round($time2) . ' seconds to complete.');
        
    $h->closeLog('speed_test');
    Output:
    Notes:

    - You can have more than one log file open at a time.
    - You can delete these logs from Admin -> Maintenance -> Debug.
    - If you leave the filename and mode parameters empty, i.e. $h->openLog(), Hotaru will use the defaults 'debug' and 'a+'.
     

Share This Page