Search code examples
joomlajoomla2.5joomla-extensionsjoomla3.0

Joomla >1.7 hide log messages from browser


I'm developing an extension for Joomla!; at the moment I'm trying to make it 3.0 compatible - as with 3.0 the logging changed a little (*). Building on the answer from this related question, my current code looks like this:

JLog::addLogger(array(
    'text_file' => 'plg_system_myplg.log.php'
)); 
JLog::add('blah blah log msg');

The problem is that the log also goes to the messages which are shown to the user - this I want to prevent, I want the log msg only to go to the log file. I think it has to do with the "category" that JLog::add takes as a 3rd (optional) parameter, but I have no idea what to pass there?

Can anybody tell me how to hide the messages / or tell me if I'm on the right way with the categories and what value I should use?

Thanks!

(*) It actually changed already with 1.7 as far as I gathered so far, but the old method of calling addEntry on the return of JLog::getInstance(...) seems to have been removed from 2.5 to 3.0.

Edit: Think I found a way now; using:

JLog::addLogger(array(
    'text_file' => 'plg_system_myplg.log.php',
    JLog::ALL,
    'myplg'
)); 

JLog::add('blah blah log msg', JLog::INFO, 'myplg');

all my log entries go only into my log file (and not to the messages shown to the user). However, I also get a few deprecation warnings - one about my code, but also some unrelated ones:

WARNING deprecated  JAccess::getActions is deprecated. Use JAccess::getActionsFromFile or JAcces::getActionsFromData instead.
WARNING deprecated  JSubMenuHelper::getEntries() is deprecated. Use JHtmlSidebar::getEntries() instead.
WARNING deprecated  JSubMenuHelper::getFilters() is deprecated. Use JHtmlSidebar::getFilters() instead.
WARNING deprecated  JSubMenuHelper::getAction() is deprecated. Use JHtmlSidebar::getAction() instead.

Not sure what to make of those - why do they appear in my log file, shouldn't they go to the default error.log file instead of my file ?


Solution

  • This is what I am using, works for Joomla 1.5 - 3.2:

    if(version_compare(JVERSION,'1.7.0','ge')) {
       jimport('joomla.log.log'); // Include the log library (J1.7+)   
       $priorities = JLog::ALL ^ JLog::WARNING; // exclude warning (because of deprecated)
       // In J3.0 we need to ensure that log messages only go to our file, thus use the categories (already supported in J2.5)      
       if(version_compare(JVERSION,'2.5.0','ge')) {
          $logCategory = 'com_mycomponent';
          JLog::addLogger(array('text_file' => $logFileName), $priorities, $logCategory);
          JLog::add($msg, JLog::INFO, $logCategory);
       }else{
          JLog::addLogger(array('text_file' => $logFileName), $priorities);
          JLog::add($msg, JLog::INFO);
       }
    } else {
       // Joomla! 1.6 and 1.5
       jimport('joomla.error.log'); // Include the log library
       $log = &JLog::getInstance($logFileName);
       $log->addEntry(array('comment' => $msg, 'level' => 'INFO'));   
    }
    

    This shows the trick for gettring of the deprecated messages.

    And yes, you have to include a category for your messages to ensure they are not showing up as system messages.