_logging and debugging in general


#1

Greetings,

I am working on a plugin but my complete "noobiness" is getting in the way. In general, where should I be looking to see error messages when the plugin code is not working properly (e.g. when there is a syntax error or perhaps a knockout binding is not working as expected?) It does not seem to be the bash shell nor the browser console.

In the past (non-octoPrint related projects), I have debugged my code via the console output using little messages to say where the code fails or to see a variable value. I must be missing the proper syntax . I tried the following in my 'plugin.js' file but nothing shows up in shell or in browser

self._logger.info("Hello World! Up button pressed! message: %s" % self.someVariable);

and the javascript console on the browser indicates it does not know what self._logger is (from the error message I see.

image

Any help would be appreciated.

Thanks,

Dave


#2

Your problem is that you are trying to use backend code in the frontend.

self._logger is injected into your plugin's implementation by OctoPrint, on the Python side of things. It doesn't exist in your frontend (= JS, Knockout view model).

To log in the frontend, use log.debug, log.info, log.error... OctoPrint's web interface includes this loglevel library as dependency and also takes care to configure it correctly (e.g. log.debug won't show up when not running in --debug mode).


#3

Thanks again,

I think I am getting close. Is it possible to communicate between the two? Share/access variables? I have a value being chosen by user thru the web interface (a ko.observable value) that then has to be available to the gcode handler/hook. Not sure how that happens.

Thanks,
Dave


#4

The two are two separate applications if you will. One running in the backend, the other running in your browser. They communicate with each other over a RESTful API (and a websocket, but that is mostly to push data from backend to frontend). So if you want to have information flow from your browser to your server in order to react to things the user does, you'll have to expand the RESTful API accordingly.

Take a look at the SimpleApiPlugin mixin or - if you need more control - the BlueprintPlugin mixin. To access your plugin's API provided through the SimpleApiPlugin mixin, also take a look at what the included JS client library offers you for that, specifically OctoPrint.simpleApiGet and OctoPrint.simpleApiCommand.


#5

Thanks! I was able to get the two working together.