What libraries can log javascript usage at runtime? - javascript

We have a web application, is there any library that we can add to the code base in order to log any method invocation at run-time?
EDIT
More details..
1) The application is running in production and we need to send the logs back to the server once in a while
2) I can't wrap any function invocation, that's why I am looking for a library that will do some sort of instrumentation or another work around..so in essence I am looking for an auto-logger

The JavaScript runs on the client computer, so any logging that you could see would have to
Be on a client computer that you control (e.g. for debugging purposes), OR
Send the log file back to the server at some point for you to analyze.
If you are asking about the first, FireBug can log function calls.
If you are after more general logging that could also send results back to the server, log4javascript is worth looking at. Have a look specifically at the AjaxAppender.

Related

How much network troubleshooting could be done with a JavaScript AJAX script?

Sometimes it could be useful for a web application to be able to determine whether a seeming slowness in the application is due to network conditions or something else.
To make it easier to determine the cause in this kind of situation, I was thinking about creating something like a "check the connectivity" client-side script makes e.g. an AJAX call to the server and returns some useful metrics that make it easier to determine if the cause lies with the network.
But how much useful network troubleshooting could be done using a JavaScript that calls the server?
It sounds like you need a way to keep an eye on your site's performance from the end-user's perspective.
One way to do this is to have your client-side scripts include a way to log to a log aggregation site like SumoLogic. Here is a doc to reference about using client-side JavaScript to log to SumoLogic.
On your server side, you could implement a /ping API endpoint that would just immediately return true so you know how long it takes your user to at least reach your site. You can then log to SumoLogic how long that request took. You could do this with other requests as well to see which APIs are slower than others.
If you include geo-location when logging to SumoLogic, you can see how well your site performs around the world.
And if you want to get really fancy, then you should implement a custom header that your APIs understand which is a transaction token of some sort for all requests. When your server receives that header, it should use that token throughout the request's logs so you can see where things go wrong and what to do about them.
Another good site to check out for this sort of thing is New Relic - Browser Monitoring. This is much more performance-centric and you don't get the insights of your own logs, but it's an awesome app in its own right.
EDIT
As mentioned in the comments by #Bergi, you could also have your server respond with the headers immediately and measure performance that way.

In Wordpress, how do I stream updated status information in real time?

I have a custom back end program that puts out status messages a few times a minute. Currently, these are saved to a file. However, I can save only the latest status message to a file if that helps. I can also format the status as an XML or json message.
These status messages are intended to update a member that is logged in to a WordPress membership site. In other words, each member would receive status messages that are unique to them. I'm running a instance of the back end program for each member and the status file is unique for each member.
Is there a way to display these status messages without reloading the WordPress page? Or, is there a way to get the page to reload ONLY when a new status message is ready to be displayed? There are a lot of Javascript frameworks that may be useful. Any recommendations about which ones work well for this situation?
the term your are looking for is WebSocket, you'll have a program keeps running at server and it will keep connections between browsers and server.
However, since you are using WordPress which is PHP based, things may be a little bit harder.
PHP does not native have API for WebSocket, so implementing it is not as easy as other server side language like NodeJS.
But there are still libraries like Ratchet you may use.
Once you've done the server side, browser side is much easier. you can use either native JavaScript or many libraries that have better implementation.
Other than WebSocket, the old way to implement this is called long polling, which request server once in a while to see if there's anything new. It maybe easier to implement but is not recommended.

Extracting (scraping) data from the web app which requires to log in [Node.js]

I am in position where I need to get some data from a page which does not provide any API.
Situation is such:
To get to the page data firstly you must log in (this normally
creates some session storage / cookies in browser)
Then there get endpoint to get auth key (it checks if user is authenticated using the session storage / cookies which were created during login)
When you have auth key there are plenty of get endpoints which can be accessed with it. That's what I need!
The actual login page is: https://w2.hronline.co.uk/account/login
Could I login just by creating https post to forms url when passing parameters with login credentials? If yes - would node create some session cookies / storage values, so later on I could make another get request to get auth key? If no, is there another way to do that?
You can't scrape directly in nodejs. You have to use some tools which provide you a web-browser in which you can execute js code/actions. Currenlty, I recommend you either Selenium, either PhantomJS.
PhantomJS is usable in nodejs, but it is limited since you can't use the require('') for all the nodejs libraries.
You can use some libraries which use PhantomJS and let you write the scraper in node.js. You should try first CasperJS which is made to do some unit tests like you want.
If you want to go further, you can try phantomjs-node which allows you to instanciate a phantomjs application in parallel, and to send it the js calls you need it to execute (requires to be used to phantomjs).
PhantomJS Is Dead, Use Chrome Headless [...] (https://semaphoreci.com/blog/2018/03/27/phantomjs-is-dead-use-chrome-headless-in-continuous-integration.html):
In April 2017, Vitaliy Slobodin who was at the time the sole
maintainer of PhantomJS, announced that he’s stepping down as
maintainer, leaving the project effectively abandoned:
I think people will switch to it, eventually. Chrome is faster and
more stable than PhantomJS. And it doesn’t eat memory like crazy.
Since you're scraping, Chrome Headless is even more relevant than for CI/testing.
Chrome Headless is a real browser and behaves accordingly.
Here's how to do it:
https://www.sitepoint.com/headless-chrome-node-js/
https://blog.logrocket.com/how-to-set-up-a-headless-chrome-node-js-server-in-docker/

Integrate Node.js with Symfony2 or PHP

I'm developing a web application with Symfony2. I need to create a push notifications sysmte (like Facebook). When an user publish something, I need some of another users receive a notification.
I saw that Node.js it's the easiest manner to do this. But, I did some simple examples and all works fine, but I don't know how can I integrate this node.js application with my Symfony2 application, or really with a PHP application.
Anybody can help me?
Thanks in advance!
Please note that you've not given enough details, so I will respond as a front-end developer and not as a mobile developer
Integrating NodeJS and PHP (in general) is not a good way since you need to launch both servers separatly, create the websocket server in JS while your application is in PHP and finally create a request (GET or POST) from your PHP to you JS server. Well, a big mess, so I'll expose my solution here under.
Quick insight for mobile apps. Well, technically, there's no easy way. You can use the Push "protocol" (http://www.wikiwand.com/en/Push_technology) with NotificationPusher (https://github.com/Ph3nol/NotificationPusher). I didn't used it before so I can't help you with it.
In general.
Most of the time when people thinks of Push, long polling will do the trick. For starters it means that the request is made client-side and the server don't send data & close connection until there's new data.
How do you implement this ?!?
Basically, you change the max_execution_time using ini_set or set_time_limit to a very long time for the current script and launch a loop (like a do..while) with a sleep and the check to your data inside. From your Javascript just make an Ajax call, for example with jquery: $.get. Just remember to remove the timeout and stay in asynchronous mode.
The only drawback of this solution is that you will always have a connection opened to your server which will consume a bit more of battery on a mobile device. If you have multiple types of data to receive do not hesitate to merge the calls and publish a type in your response data, since most of the browsers allows only 2 or 3 simultaneous connections to the same server.
I sounds like your describing WebSockets.
Take a look at Socket.io, its a module for node.js.
Also there is a example at GitHub https://github.com/Automattic/socket.io/tree/master/examples/chat
Interesting files for you should be the index.js and the public/main.js.
You can see the example live at http://socket.io/demos/chat/

log4javascript web.config

I use log4net in my ASP.NET MVC sites, and want to start using a similar package in my JavaScript (such as log4javascript) to do logging there. Especially logging of exceptions, but also debug logging if a user reports that something went wrong but it isn't readily reproducable.
I now have these questions about integrating log4javascript with ASP.NET MVC:
1) I want to send log messages to the server using log4javascript's AjaxAppender. Is there a package that will implement the web service that receives log messages and logs them server side?
2) I want to be able to set the level of client side loggers in the server side web.config, just as with log4net. This way I could switch on debug logging when needed without hacking JavaScript code (all JS will be combined and minified on production).
I guess on each page load the server can read the web.config and generate a bit of JavaScript that creates the client side loggers. Is there a package that does this?
log4javascript author here.
I don't know of a service that does this, but it's pretty trivial to do with a web handler and log4net.
Nothing I know of. I've considered writing an XML configurator for log4javascript but no-one's ever asked.

Categories

Resources