Joomla >1.7 hide log messages from browser

你说的曾经没有我的故事 提交于 2019-12-12 14:25:43

问题


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 ?


回答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.




回答2:


Use

 new JException('Something happened');

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




回答3:


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.




回答4:


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 :(.




回答5:


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


来源:https://stackoverflow.com/questions/14389103/joomla-1-7-hide-log-messages-from-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!