I've got a client side javascript file that is setup to be called as a modx resource. This has allowed me to successfully reference modx tvs inside of the js.
Now I am looking for a way to write log messages from inside of this js file to assist with debugging a problem. I was thinking of logging info messages to the default modx error log.
But, I can't reference the modx objects directly in js like I would in a snippet. I also can't seem to reference a modx snippet from inside of the js. What's the right approach here? I need to be able to log a message which includes passing some of the js variable contents as text string in the message.
-
☆ A M B ☆
- 191 Posts
Do you have to use the MODX error log? Logging errors to the browser console would be much simpler, with javascript's console.log().
The issue is that the MODX stuff (snippets, etc.) is executed on the server when the page is built but the JS is executed later on the client, so the two parts can't by default communicate through time and space like that.
You could set up a way for them to though. Like create another resource with an error-logging snippet that you can then call via AJAX from the javascript, through a URL, say, like errorlogger.html?msg=Some%20error%20message. The snippet in errorlogger.html would read msg from the query string and log it to the MODX error log.
I don't have to use modx error log, but I do need a server side log of some sort. The modx error log seemed like the easiest thing to use.
I am already logging to the browser console but I can only view the results of my own sessions. I need a historical record for all visitors to really see what is happening.
-
☆ A M B ☆
- 191 Posts
Oh, I see now. I can't think of a ready-made way to do this (maybe somebody else can), but like I was saying it wouldn't be hard to create a special page for error messages with a snippet that'd take them out of GET data and write them to the MODX error log or some file on the server. You could hit this page with an ajax call whenever your javascript needs to log something.