Everywhere in the code, you do this:
watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
Which is wrong because this:
nl2br(check_plain($e->getMessage()))
Will get displayed through t() function. See watchdoc PHPdoc:
/**
* Log a system message.
* ..
*
* @param $message
* The message to store in the log. See t() for documentation
* on how $message and $variables interact. Keep $message
* translatable by not concatenating dynamic values into it!
* @param $variables
* Array of variables to replace in the message on display or
* NULL if message is already translated or not possible to
* translate.
* ..
*/
You should write your watchdog events like this:
watchdog('Apache Solr', "!e", array("!e" => nl2br(check_plain($e->getMessage()))), WATCHDOG_ERROR);
Everywhere, this should avoid the locale table oversizing because of exceptions messages getting translated. May be you want the exceptions messages to be translated, but you have to ensure those message comes from your own exceptions doing better exception catching than "Exception $e".
Other exception messages should remain in their original language, debugging wrong translated messages is the hell in person.
This is just an opinion, but you should think about it.
Comments
Comment #1
robertdouglass commentedYes, agree. Can you help us patch?
Comment #2
pounardI can, expect patches until tonight.
Comment #3
pounardHere is some patches, please review do not commit without further testing. You may not like the function I created.
Comment #4
Scott Reynolds commentedI think creating a function that expects a Exception or a String is pretty confusing. Can we set this up as two functions?
Comment #5
pounardI'm ok with it.
EDIT: despite the fact this is how polymorphism can be implemented in PHP :D (just a joke, btw this is true).
Comment #6
Scott Reynolds commentedActually, after giving it more thought, I don't like a one line function.
I think I would prefer the pattern
In this way, there is one function to log messages, and it accepts string, array(), boolean.
This of course brings up the question as to why we need to put it through one function? We already have watchdog, which adds the hook_watchdog, so that you can log messages where you want them. Seems like maybe we should just fix the 'bad' watchdog logs and not introduce a new function.
What is the advantage of this new abstraction?
Comment #7
pounardThe advantage was to dispense the developer writing the n2lbr(check_plain()) everywhere. It's not really needed, it's just more convenient to write. And also it gives only one entry point for all error messages, which could eventually allow, if needed, to put a debug mode setting which could allow, for example, to store exception full stack trace, or in the opposite, do not log anything when not in debug mode.
Comment #8
pounardIf you say no to this abstraction, tell me, I'll re-do my patch without it.
Comment #9
robertdouglass commentedIt's too bad that Drupal doesn't have exception handling of any sort. I'd love to catch more specific exceptions (instead of Exception $e all the time), and then throw new DrupalExceptions that have watchdog and string cleaning built in.
I think the function in the example is an unneeded layer of complexity. I like the convenience that it brings, but I don't like the idea of having a special custom error handling mechanism just for this module. Let's start with a patch that just fixes the original problem everywhere it occurs. We can come back to the idea of better error handling later.
Comment #10
pounardRe-done the patch. I noticed that sometime, watchdog type was 'Apache Solr', and sometime it was 'apachesolr'. I did put 'apachesolr' everywhere.
If you prefer 'Apache Solr' (which looks like quite inconsistent with other module usage of watchdog), you can still do:
Comment #11
robertdouglass commentedComment #12
robertdouglass commentedMade all watchdog calls consistent (not just the ones with $e->getMessage()).
Comment #13
robertdouglass commented#664818 by pounard, robertDouglass | Scott Reynolds: Fixed Wrong watchdog() usage.
Committing to 6.2.
Comment #14
robertdouglass commentedHere's the 6.1 patch.
Comment #15
robertdouglass commentedcommitting to 6.1.
Comment #16
pwolanin commentedAbout to roll this patch back. It's an incorrect use of the APi.
http://api.drupal.org/api/function/watchdog/6
Note the special use of NULL - this patch in fact makes the message go through t() when it was NOT previously.
Comment #17
pwolanin commentedsee: http://api.drupal.org/api/function/_dblog_format_message/6
If $variables is NULL (serializes to
N;) then t() is NOT called. What was the basis for this original report of these messages going into the translation table?Comment #18
pwolanin commentedLooks like there was a single wrong call to be corrected.
Comment #19
pwolanin commentedcommitting this - I suggest #12 be rolled back and any actual incorrect calls be fixed also in 6.x-2.x.
Given that all but the one call used 'Apache Solr', I'm sticking with that as the consistent $type.
Comment #20
robertdouglass commentedCommitting attached to 6.2 after rolling #12 back, so now the branches are at least in sync.
Comment #21
robertdouglass commented@pounard - does @pwolanin's API clarification satisfy your concern here? If not, please re-open.
Comment #22
pounardOk, I misread watchdog() doc.
EDIT: BTW watchdog() function usage is weird because of this special 'NULL' variable array.
Comment #23
pwolanin commentedYes, this is a weird/stupid api in D6+, but with that said, I think we are using it as it is expected to be used.
Comment #25
claudiu.cristeaFixed also in 5.2