The 2.5.5 introduced a much appreciated log for bad links (#13268), but it's a bit too enthusiastic. Links to a resource within another context are logging an error even though they are valid.
Create a 2nd context, and add a resource in it. Link from web to the resource in that context with the [[~123]] link tag.
Error messages in the log. For example three header links pointing to resources in another context:
[2017-02-08 16:46:39] (ERROR in modContext::makeUrl @ resource 605) Resource with id 322 was not found in context web
[2017-02-08 16:46:39] (ERROR in modContext::makeUrl @ resource 605) Resource with id 101 was not found in context web
[2017-02-08 16:46:39] (ERROR in modContext::makeUrl @ resource 605) Resource with id 493 was not found in context web
[2017-02-08 16:50:48] (ERROR in modContext::makeUrl @ resource 169) Resource with id 322 was not found in context web
[2017-02-08 16:50:48] (ERROR in modContext::makeUrl @ resource 169) Resource with id 101 was not found in context web
[2017-02-08 16:50:48] (ERROR in modContext::makeUrl @ resource 169) Resource with id 493 was not found in context web
Links are valid and functional.
Actually broken links are logged as such:
[2017-02-08 16:50:48] (ERROR in modContext::makeUrl @ resource 169) Resource with id 111 was not found in context web
[2017-02-08 16:50:48] (ERROR in resource 169 @ /path/to/core/model/modx/modparser.class.php : 1353) Bad link tag `[[~111]]` encountered
which makes me believe that we could drop the error triggered in makeUrl. Alternatively, the check in modContext->makeUrl should be moved to modX->makeUrl until after it automatically discovers the context that is being linked to, but Jason mentioned that was troublesome.
No error log entry for valid cross-context links.
MODX Revolution 2.5.5. Not server specific other than the requirement for multiple contexts.
Yes, the 'bad links' error is nice, but the 'not in context' error is just a nuisance.
Maybe make it an info or a debug instead of an error?
I also encountered the same problem
[2017-02-20 17:10:16] (ERROR in modContext::makeUrl @ resource 238) Resource with id 98 was not found in context es
[2017-02-20 17:10:41] (ERROR in modContext::makeUrl @ resource 168) Resource with id 472 was not found in context web
the strange thing is that, I do not have a link from a resource point to other.
And I also found another problem with tagger
[2017-02-20 17:10:42] (ERROR in resource 186 @ ../core/model/modx/modparser.class.php : 1353) Bad link tag[[~]]encountered
In this case i have "Freeze URI" set for the resource to point the category, links are valid and functional.
Is this what changed https://github.com/modxcms/revolution/pull/13268
I've got the same problem on a multi language website using Babel. Each language has a separate context, and there are many pages in one language linking to another.
Every time a page template/chunk tries to create a URL with [[~id]] where the id is in a different context it throws an error.
(ERROR in modContext::makeUrl @ resource X) Resource with id Y was not found in context fr
This is everywhere across the website, so I think my only option is to comment lines out in the core code.
model\modx\modcontent.class.php
Lines 314 to 323
} else {
/*
$this->xpdo->log(
xPDO::LOG_LEVEL_ERROR,
"Resource with id {$id} was not found in context {$this->key}",
'',
__METHOD__,
$this->xpdo->resource ? "resource {$this->xpdo->resource->id}" : __FILE__,
$this->xpdo->resource ? '' : __LINE__
);
*/
}
Is this a sensible solution?
I'm not so sure, but it's stopping the error log getting Huuuuuge :-)
I just changed it from LOG_LEVEL_ERROR to LOG_LEVEL_WARN
That way if you change the log_level in System Settings you'll get the warning message, but with the default setting you won't. Maybe LOG_LEVEL_INFO would be even more appropriate.
I agree with Mark, that it could be enough to log only this error:
[2017-02-08 16:50:48] (ERROR in resource 169 @ /path/to/core/model/modx/modparser.class.php : 1353) Bad link tag `[[~111]]` encountered
So the patch in #13268 should be changed, that only that error will be logged (remove the logging enhancement in modcontext.class.php).
But since calling makeUrl with a wrong ID could not happen only in the MODX parser, I propose an enhancement: If it is possible to check (with a not expensive method) that the resource exists in another context, this should be preferred. And at least, if thats not possible: the MODX debug level setting could trigger an expensive database resource check.
I'm inclined to think that this message could be useful under certain conditions, so maybe just changing it to an INFO or even a DEBUG level would stop if from being a nuisance, while leaving it available.
Somebody went to a lot of trouble to code this, I'd hate to just throw it out when a simple change of the log level would solve the problem.
Pull request created #13305 to lower the log level to INFO.
I thought the ERROR was justified as long as the false positives are removed? I thought there was a problem with the original implementation?
It's not a false positive, it's a perfectly reasonable report that probably has its uses in some circumstances, just not necessarily an error.
I guess that makes sense too. I thought there was a system setting for allowing links across contexts? If there is, enabling that should silence this error either way?
The original ERROR message makes no sense. Even not as INFO. It just floods the error log with not necessary messages.
The logging improvements in #13268 were written to detect bad links (or makeUrl calls) somehow better. A link to a resource in another context with a valid resource ID is not a bad link.
Yeah this error seems to be going nuts.
Problem is, it's logging this even though it's expected.
For example:
href="[[++cultureKey:is=zh:then=[[~711]]:else=[[~1]]]]
I suppose this could be worked around by swapping out chunks but still...
Another way around triggering it is [[~711? &context=`foobar`]], but it's annoying to have to update all of those references. Plus, that's only really manageable in templates..
@Mark-H Yup that's a good point for a new site but as you said for an old larger one, it's a headache.
@OptimusCrime unfortunately that doesn't silence it. :(
On closer inspection something with the error reporting seems off as I'm getting:
(ERROR in modContext::makeUrl @ resource 711) Resource with id 117 was not found in context zh
However resource 711 is definitely in the zh context.
Edit: This only appears when loading the the page on the zh context. The web context gives no errors. (same template of course)
The error is occurring in resource 711, the error being that resource 117 is not in the zh context. This means that the link being generated is in resource 711.
It is in processing resource 711 that the makeUrl() function is called, generating the link to resource 117.
The easiest way to stop this is to simply go to the core/model/modx/modcontext.class.php on line 315, and change it from LOG_LEVEL_ERROR to something else, such as LOG_LEVEL_INFO or LOG_LEVEL_DEBUG. Then it won't be in the error log file unless you change the log_level setting in System Settings.
The more I think about this, it seems that the logging is right there, but the logging message is somehow incomplete. It should be something like this: Resource with ID 472 was not found in context web, please use makeUrl with the right context for efficiency reasons.
Solved by #13305