3

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 ?

Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71

5 Answers5

1

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.

hbit
  • 959
  • 2
  • 13
  • 33
  • ok, so by ignoring "WARNING" level messages you get rid of the deprecation messages. but that doesn't really explain why they show up in the log in the first place - and it effectively renders the loglevel WARNING useless, it can't be used for other messages anymore. Is it intended to be like that? I also use the category (now), I don't get why these warnings show up in my log anyway, do you have an idea? – codeling Nov 08 '13 at 11:22
  • That is correct. I fixed this by using the JLog::ALERT instead of JLog::WARNING. My log class simply replaces the one with the other and so I can get rid off the deprecated ones. DEBUG, INFO, ALERT and ERROR are enough levels for my needs. – hbit Nov 08 '13 at 22:20
  • Thanks for the elaborate answer, I will give it a shot one of the nextg days! – codeling Nov 11 '13 at 23:39
  • upon looking more closely into my logfile - I also have NOTICE and INFO messages there ("Can't identify browser version. Agent: ..." and "FinderIndexerAdapter::getTypeId") which I didn't produce (category "-"), so I guess there just is no way to *specifically* disable all messages other than those belonging to my plugin from coming into my logfile; trying by loglevel doesn't really seem to cut it... will create a new question for this, nevertheless much thanks for the effort! – codeling Nov 27 '13 at 19:38
  • will accept this answer as it solved my original problem. Any ideas for the other problem are still more than welcome... – codeling Nov 28 '13 at 18:38
0

Use

 new JException('Something happened');

This will only add it to debug log but will not show anything.

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38
  • maybe a dumb question, but what is the debug log? a separate log file? I wanted to write to my own logfile, actually, seems cleaner to me that way... – codeling Nov 11 '13 at 23:43
0

It seems, that Joomla 3.0 has no default logger enabled. The same in Joomla 3.0.3. Nothing turns logging on by default - even Debug mode.

  • you mean the reason why I have those messages in my log is because there is no default logger, and so everything will go into my log? If that's so, that might be an explanation for the seen behavior, but it is not really helping solving anything, sorry. – codeling Nov 11 '13 at 23:42
  • also, doesn't explain at all why there is a "error.php" in my logs directory (which does receive some INFO messages with category "Joomla", but not those with category "-")... – codeling Nov 27 '13 at 19:42
0

Finally I think I have solved my issue with unrelated log entries showing up.

A close look at the API documentation of the addLogger function revealed that the third parameter, $categories, is supposed to be an array of categories for which this log will be used.

This is in contradiction to the version of http://docs.joomla.org/Using_JLog that is current at the time of this writing, where a single category is given instead of an array.

Changing my call to addLogger to use an array, like this:

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

And keeping my fingers crossed that this will fix the issue!

Edit: unfortunately even this still doesn't solve my issue - still got unrelated entries :(.

codeling
  • 11,056
  • 4
  • 42
  • 71
0

I found the answer.. hope this script make you understand.. I already built as function . this code work on joomla 3. hope work in joomla 2

<?php 
function logWrite($level, $values, $file='%s.php',$path='',$showOnTop=0,
$option='',$component=''){
/****
jlog Joomla 3.4
created by:gundambison (2015.04.26). 
THX: hbit@stackoverflow
****/
    jimport('joomla.log.log'); 
    $level=strtoupper($level);  
//You can change this com_name
    $component= $component==''? 'com_gundambison': $component;
    $date= date("Ymd");
    $filename= sprintf($file, $date);
    $format= $option=='' ?"{TIME}\t{CLIENTIP}\t{CATEGORY}\t{MESSAGE}": $option;

// create options and text
    $txt = is_array($values)? json_encode($values): $values;
    $options = array('text_file' => $filename,'text_entry_format'=>$format );
    $options['text_file_path']=$path==''?'logs': $path; 
    JLog::addLogger ($options);
/*
if you want the error to show in your page. just see the different 
*/  
    if($showOnTop==1){
        JLog::add("$txt");
    }
    else{ 
        JLog::add("$level\t$txt",$level,$component);
    }
}
  • Welcome to StackOverflow and thanks for your answer. I can't however see what information it really adds to what is already there? The necessary additional parameters to the `add` method were already explained in other answers. – codeling Apr 28 '15 at 21:46
  • first I thank to you.. You make me found the answer. thx for the correction – user2905554 May 25 '15 at 13:07