How to debug a function Node?

Continuing the discussion from :clipboard: Survey: logging to debug sidebar:

As a (half-beginner) I've started to enhance a node, to make it work with multiple I2C buses.
I'm writing my progress down to show how difficult is it for us, Newbies to get along with version 1.2.6
(Don't get me wrong, I love NR :heart_decoration: from the first screenshot I've seen it! Just some user-help improvements :information_source: would boost a lot on nice experience, spare time and learning would be easier about it.)

1. Examples from github

  • Found code here,
  • copied into a Function,
  • spent a half hour to find out how fix the "required" error,
  • than came the big surprice:
    NOTHING. No error, no warning, no info, no output on the sidebar.

2. global.log(), global.info(),

After wathing 30+ YT videos and reading 1000 pages of documentation over the last 2 month my head is filled totally up to the top. Still, I remembered something like:

  • There was something about a .warn('...') function.
  • code-extension does not work, so typing "global." shows no list of choices
  • RED.warn() did not work either
  • Somehow I remembered it should be used capitalized: ... so tried also ...WARN() variations.
  • ... and many more trials.

3. No info/help on main page

Looking at : logging... (Which is totally deepwater for a beginners anyway.)
or at error handling ...
or even at cookbook error handling
not a single hint is there.

One single line would have had helped a lot!

You can use node.debug('a='+a) in a script inside a Function Node for sidebar-debugging.
See more here (link) ...

... At least the first link logging... helped me to my next discovery:

4. Finding the console

  • So here I am, spent 1.5 hours to find out:
  • $ node-red-log must be running from a console to view the log !!! :grimacing:
  • It hides usually behind the browers, so not too much help exept if you have a 2th monitor attached.
  • :bulb: IMHO there should be an alias too:$ node-red-console to keep naming consecvence.

So basically it wasn't me, not doing the job right, but the output went to a wrong place.

:bulb: One solution could be to place a button on the Debug Tab with a counter :information_source: to show "there is more behind". Something like this:
kép

5. Forum search failures

https://discourse.nodered.org/search?q=debug%20function

My first search ended up at this comment.

  • :slight_smile: Great! Finally I know the KEYWORD IS: node.XYZ()
  • (Still no auto-code-extension does, so typing "node." shows no list of choices. :frowning: )
  • So let's try: node.info('Show Me!');
    (Why I've tried "info()", and not "error()" or "warn()" ?
    Because: I'd like to use warn() for problems. Not for simple infos.)

The Result:
NOTHING. NADA. Emptyness...

... many many other searches, no simple solution.
That's how I've discovered the prev. topic.
My oppinion:

  • there should be MORE ways to do that, not just one. It's better to have 20 alias function for a thing as important than this than one hidden only.

6. SideBar help (Bug?)

Finally I remembered, each node has a built-in help.
And YESSSS, Function has one too, with valuable information:
kép

... So let's try it out:
node.log("My Logggg");
Aaaaaaand Result iiiiiiis:

A big piece of nothing. As usual.
That was the point I gave up and started this topic.

[{"id":"f3b2c9de.38ea7","type":"group","z":"34aad6f7.b85c6a","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["c7bd2502.8cab","fbdac8f7.f88548"],"x":74,"y":639,"w":372,"h":82},{"id":"c7bd2502.8cab","type":"inject","z":"34aad6f7.b85c6a","g":"f3b2c9de.38ea7","name":"Test Log","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":180,"y":680,"wires":[["fbdac8f7.f88548"]]},{"id":"fbdac8f7.f88548","type":"function","z":"34aad6f7.b85c6a","g":"f3b2c9de.38ea7","name":"test debug","func":"\n    console.log('anything happening?');\n\nnode.log(\"My Logggg message\");    \n\n//node.warn('nodeW');\n//node.info('nodeI');\n//node.debug('nodeD');\n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":680,"wires":[[]]}]

If you really want to debug - use a debugger :slight_smile:

Lots of clues on the forum


demo of debugging a function node...

While running with vscode debugger attached, use the keyword debugger to instruct the debugger to stop...

Real debugging...

1 Like

It all depends what you are doing. for example, the title here is "debug a function node" - many of the answers to the questions you ask here can be found in docsuser guidefunctions ...

image

I am not saying that all the info is there but much of it is. It is not perfect but this is a huge program with minimal developers.

Wow!

  • Where did you copy that picture from? Link please!
  • Is that sentence at the bottom marked with red by default? Or did you enhance it?
  • Shouldn't be node.debug("Variable", myVar); also listed?

I totally agree with that!
I know how hard it is for a deep-level / hardcore programmer to look "outside of his box" to see everthing from user-perspective.

Anyway, improving the code with +1 comment would make it much more self-explanatory:

    node.log("Log message"); // Warning: .log() is not visible at the editor's debug-sidebar only at the console.
    node.warn("Warning, both these variables are wrong:", myVar1, myVar2); 
    node.error(["Error " + msg.error.count, myVar1]);

Can you copy this code examples here ?
and to function node help?

Also, as the original topic recommended:
Please enhance NR, so the:

console.log() messages appear at the Debug sidebar too!

IMHO just because "only" 13% of us voted for this, doesn't necessary mean this possibility should be dropped.

And yes, IMHO node.log() should appear at the sidebar too!

  • Why on earth is it disabled? Isn't that a BUG?

... maybe 3-4 tiny "filter buttons" would make it easier to reduce, if not needed:

[ i ] :x: [ w ] :white_check_mark: [ e ] :white_check_mark: [ d ] :x:

3 Likes

It depends how much of the thread you link to you have read. I explain there why the debug sidebar doesn't show log/info level messages.

The Debug sidebar is not intended to show the full log output of Node-RED. It is meant for debugging flows. This is why it doesn't show info level messages.

The log event handler used by the Debug sidebar is set to only display log events that are 'warn' level or higher.

The whole discussion in that other thread is about trying to resolve some of the inconsistencies and to provide a special case to the Function node to provide a way for it to more intuitively log to the Debug sidebar without having to log warn/error level messages - hence why we ended up with node.debug(...).

We have an item on the backlog to provide better log level selection in the debug sidebar.

1 Like

Your flow works perfectly for me. The console.log() output appears in syslog, node.log() displays in syslog and the node red log and node.warn() appears in syslog, node red log and the debug pane.

I agree that debug should go to debug console only.
And the help about using debug() should be at advanced topics.

But If a beginner is copying an example to a Function node,
or we do not want to use red coloured error() and warn() for showing simple informations,
and sees that first line in the help node.log(), he will use it, just like I did.
It it won't "work" like expected, because there is no indicator that would show any sign of a hidden message not visible where this trial happens.

Sorry, you're right, only 70%. Red the half + the end.
But I've simply tried all variants and it's not working the way a beginner would think.

@Colin
node.log() does not show any message at the debug-sidebar for me.
What version are you running?

1 Like

I didn't say it did.

Well the function documentation and help both provide information about logging.

However, I'd agree that node.xxx isn't terribly discoverable otherwise for beginners.

I wonder if something like a RED.show(....) function might be more obvious? Not sure that it would but putting it out there for thoughts. It would be just for the function node and would only output to the debug sidebar. Though perhaps you could have a 2nd param that also sent it to the actual log.

I think that having something that only outputs to the Editors debug sidebar would actually be rather helpful, the log already contains a lot of cruft sometimes.

1 Like

IMHO we should stay with node.xyz() keyword. No need a new. one.
node.show(); sounds nice
node.info(); also

That could be confused with current function. ("text", MyObject)

At the very least read the previous thread to save having exactly the same discussion again.

The reason we went with adding node.debug() is because it sends its output to the debug sidebar.

We could discuss the API endlessly, but the fact remains the user has to know it exists to use it. So whatever it's called, it has to be documented. I can see we didn't update the docs to cover the changes to node.debug() following on from the previous discussion and changes that were made.

So rather than discuss endlessly over how it's spelt, it would be helpful to direct efforts to docs improvements.

2 Likes

I might be suffering from an excess of Christmas spirit(s) :tropical_drink: However node.debug doesn't produce anything in the sidebar (it certainly exists as a property of node).

I can't find any documentation on it either - doubtless I'm being a bit thick today.

Right, Google let me down. I've found the docs now:

Writing Functions : Node-RED

And sure enough:

If there is no logger configured to capture those levels, they will not be seen.

So my takeway from that is that node.debug does not have any output by default? Surely that is not what is being asked for here? The ask was for a simple, discoverable way to output information to the debug sidebar that wasn't coloured for warn/error.

Which is precisely the topic of the thread linked to at the start where I polled the community for input on what the API should be to do this.

I just can't remember where we ended up in terms of implementing it.

Given I'm on holiday, I'm not digging in to it any deeper right now, but the plan will be for node.debug to log to the debug sidebar. I assumed I'd already implemented that, but I guess not.

3 Likes

Sorry to disturb your holiday. For when you return,

The current implementation of node.debug in a function node appears to be:

function() {
    node.debug.apply(node, arguments);
}

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.