I’m not sure if this is a bug, but I suspect it may be a bug (or missing feature, depending on your point of view).
I’m logging in FireBug with CWebLogRoute and showInFireBug enabled - it’s working nicely, except for AJAX requests where the script is expecting text or HTML as the reply…
For example, the tags autocompleter in the blog-demo displays JavaScript code (console.log statements) in the dropdown.
This kind of JS-based logger output will probably have other unintended side-effects on various other AJAX functions, too.
Is there a way to turn off the logging for AJAX requests, as a temporary workaround?
Of course not a good long-term solution, since log output is just as important (if not more) for AJAX as for regular page requests…
<?php
class JavaScript {
/**
* Makes request ajax safe. Usefull when returning json data, and yii wants to spit out debug info
*/
public static function makeAjaxSafe() {
Yii::app()->log->routes['web']->enabled = false;
}
}
Maybe it would go better in a some sort of request layer like CHttpRequest
I would override beforeAction() and test if this is an ajax request. Can be implemented as a parent controller so you have to write the code only once.
I seem to recall having seen in another framework that it’s possible to look at the headers and determine if the request is coming from the browser itself or via AJAX…
I think whatever the best solution, it should be applied to the framework - this problem is too common to ignore.
Log messages really should be returned some other way that doesn’t interfere with either AJAX or regular HTML, and doesn’t inhibit logging under any conditions.
The best way would be to return the log entries as headers, which would not interfere with any type of content, even images and other binaries.
The question is whether it’s possible to pick up these headers client-side and display them.
I know it’s possible with FirePHP, for example. But really, another add-on, on top of FireBug, just for that - and only works in FF, it would be better to have a simpler and cross-browser solution somehow…
Deleted the issue. I guess we don’t need to bloat yii with a built-in option to turn off logging on ajax requests… Seems like the pro is about the same as the con to me
I’ve needed to handle this problem on several different occasions already - having to address this problem again and again is getting tedious.
Dynamically generated images and PDFs, for example. Tomorrow I have to do some protected downloads as well, and will need the same fix again. This seems like a very common problem.
Most of the time I end up with something along the lines of this:
foreach (Yii::app()->log->routes as $route)
if ($route instanceof CWebLogRoute)
$route->enabled = false;
Well, maybe I’ll write up my own logging class at some point, using headers, so I can debug my AJAX and other actions properly…
You could put this logic in beforeAction() as Y!! described if you want it to be automatic. It’s pretty easy to implant on the application side, but adding it to the core would just be a complication to a lot of people I think.
Well the point it that the html log does not help anything for ajax requests. firebug logs DO however work with ajax requests, at least some of the time. If you load something with ajax, and you have firebug logging on, you will notice that the logs come up on firebug
in my opinion, complications will inevitably arise - the question is whether you have to find the solution in some obscure forum post or unsupported extension, or whether the framework provides a well-documented official solution.
You will run into these issues, unless you’re programming something completely trivial and ultra simple… but then why would you be using a complete MVC framework?
Anyways, FirePHP, that idea had crossed my mind… but I would prefer a simpler (cross-browser) solution - I’ll be thinking about possible ideas for a different approach
@@ -25,6 +25,10 @@
* @var boolean whether the log should be displayed in FireBug instead of browser window. Defaults to false.
*/
public $showInFireBug=false;
+ /**
+ * @var boolean if the log should be ignored in FireBug for Ajax requests. Defaults to false.
+ */
+ public $ignoreAjaxInFireBug=false;
/**
* Displays the log messages.
@@ -44,7 +48,7 @@
{
if($this->showInFireBug)
$view.='-firebug';
- else
+ if(!$this->showInFireBug || ($this->showInFireBug && $this->ignoreAjaxInFireBug))
{
$app=Yii::app();
if(!($app instanceof CWebApplication) || $app->getRequest()->getIsAjaxRequest())