Joomla> 1.7隐藏来自浏览器的日志消息

时间:2013-01-17 22:13:53

标签: joomla joomla2.5 joomla-extensions joomla3.0

我正在为Joomla开发一个扩展程序!目前我正在尝试使其兼容3.0 - 与3.0一样,日志记录稍有变化(*)。在答案from this related question的基础上,我目前的代码如下所示:

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

问题是日志也会转到向用户显示的消息 - 这是我想要阻止的,我希望log msg只能转到日志文件。我认为它与JLog::add作为第3个(可选)参数的“类别”有关,但我不知道该通过什么?

任何人都可以告诉我如何隐藏这些消息/或者告诉我我是否正确地使用类别和我应该使用的价值?

谢谢!

(*)到目前为止,它实际上已经改为1.7,但是在JLog::getInstance(...)返回时调用addEntry的旧方法似乎已从2.5移除到3.0。

编辑:想想我现在找到了一条路;使用:

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

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

我的所有日​​志条目只会进入我的日志文件(而不是显示给用户的消息)。但是,我也得到一些弃用警告 - 一个关于我的代码,但也有一些不相关的警告:

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.

不知道该怎么做 - 为什么它们出现在我的日志文件中,它们不应该转到默认的error.log文件而不是我的文件吗?

5 个答案:

答案 0 :(得分:1)

这就是我正在使用的,适用于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'));   
}

这显示了不推荐使用的消息的gettring技巧。

是的,您必须为邮件添加一个类别,以确保它们不会显示为系统消息。

答案 1 :(得分:0)

使用

 new JException('Something happened');

这只会将其添加到调试日志中,但不会显示任何内容。

答案 2 :(得分:0)

似乎Joomla 3.0没有启用默认记录器。在Joomla 3.0.3中也是如此。默认情况下,没有任何内容可以打开 - 甚至是调试模式。

答案 3 :(得分:0)

最后,我想我已经用不相关的日志条目解决了我的问题。

仔细查看at the API documentation of the addLogger function显示第三个参数$categories应该是将使用此日志的类别数组。

这与撰写本文时最新的http://docs.joomla.org/Using_JLog版本相矛盾,其中给出了单个类别而不是数组。

将我的呼叫更改为addLogger以使用数组,如下所示:

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

保持我的手指交叉,这将解决问题!

编辑:不幸的是,即使这仍然无法解决我的问题 - 仍然有无关的条目:(。

答案 4 :(得分:0)

我找到了答案..希望这个脚本让你明白..我已经建成了函数。这段代码适用于joomla 3.希望在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);
    }
}