# Custom node-red logger- show line numbers

**URL:** https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924
**Category:** Developing Nodes
**Created:** [14 November 2019 05:47 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924 "2019-11-14T05:47:11Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![uma](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@uma](https://discourse.nodered.org/u/uma)
#### Post date: [14 November 2019 05:47 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/1 "2019-11-14T05:47:11Z")

</div>

Hi  
I am trying to debug an error in my custom node. I get something like this in the console:

14 Nov 16:28:44 - [error] [test-validation:d5746feb.8c06d] TypeError: Cannot read property 'undefined' of  
undefined

Though this gives me the name of the node and the node id, it does not give the line number where the error occured. I tried adding a custom logger as below in the settings.js file under logging:  
// Custom logger  
myCustomLogger: {  
level: 'error',  
metrics: false,  
handler: function(settings) {  
// Called when the logger is initialised

```
        // Return the logging function
        return function(msg) {
            console.trace('custom logger', msg.timestamp, msg.event);
        }
    }
}

```

. However thsi still does not give me the line number in the node where the error occured:  
Trace: custom logger 1573709324619 undefined  
at LogHandler.handler (C:\Users\umasudhan.node-red\settings.js:270:25)  
at LogHandler. (C:\Users\umasudhan\AppData\Roaming\npm\node\_modules\node-red\node\_modules@node-red\util\lib\log.js:67:18)  
at LogHandler.emit (events.js:189:13)  
at C:\Users\umasudhan\AppData\Roaming\npm\node\_modules\node-red\node\_modules@node-red\util\lib\log.js:156:21  
at Array.forEach ()  
at Object.log (C:\Users\umasudhan\AppData\Roaming\npm\node\_modules\node-red\node\_modules@node-red\util\lib\log.js:155:21)  
at Object.createNode (C:\Users\umasudhan\AppData\Roaming\npm\node\_modules\node-red\node\_modules@node-red\runtime\lib\nodes\flows\util.js:485:25)  
at Flow.start (C:\Users\umasudhan\AppData\Roaming\npm\node\_modules\node-red\node\_modules@node-red\runtime\lib\nodes\flows\Flow.js:176:44)  
at start (C:\Users\umasudhan\AppData\Roaming\npm\node\_modules\node-red\node\_modules@node-red\runtime\lib\nodes\flows\index.js:329:33)  
at tryCatchReject (C:\Users\umasudhan\AppData\Roaming\npm\node\_modules\node-red\node\_modules\when\lib\makePromise.js:845:30)

Could someone please help?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [14 November 2019 10:45 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/2 "2019-11-14T10:45:43Z")

</div>

Sometimes, the complex structure of Node-RED doesn't allow it to know what line created the issue. This has been discussed before in the forum so if you want the details, you'll need to go searching.

Bottom line is that it isn't always possible for Node-RED to know what line in an originating node triggered the problem.

If that error comes from a function node, you can add `node.warn` messages to look at your variables/properties at specific points in order to work out what is wrong. A mistyped variable name or something happening out of the expected order or maybe the source data isn't quite what you expected. All of these might be common reasons for what you are seeing.

If it is coming from some other node, that might be harder to track down and you might need to raise an issue with the node's author. Or dig into the source code and add some custom console output.

---

<div class="post-metadata">

### Author: ![uma](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@uma](https://discourse.nodered.org/u/uma)
#### Post date: [3 December 2019 22:10 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/3 "2019-12-03T22:10:11Z")

</div>

Thanks again Julian. I finally found a way that seemed to work for me- adding a catch node with a debug out seems to output the stack trace with line numbers.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 December 2019 10:59 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/4 "2019-12-04T10:59:42Z")

</div>

Thanks for sharing, that is really useful to know - I hadn't thought of doing that 🤔

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 February 2021 10:50 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/5 "2021-02-25T10:50:31Z")

</div>

Sorry guys, misunderstood: I thought this is about getting a line number for an error within a function node...

Actually, adding a catch node did not help me but wrapping the code in a try catch block and logging the error did. Tested with this code:

```auto
//
try{
var b=null;
//
//
var a=b.a;
//
//
}
catch(e){
    node.warn(e.stack);
}
return msg;

```

This will log a message like:

TypeError: Cannot read property 'a' of null at Function node:4f5f2b5e.37917c:7:9 at Function node:4f5f2b5e.37917c:15:3 at Script.runInContext (vm.js:131:20) at processMessage (/usr/local/lib/node\_modules/node-red/node\_modules/@node-red/nodes/core/function/10-function.js:312:29) at FunctionNode.\_inputCallback (/usr/local/lib/node\_modules/node-red/node\_modules/@node-red/nodes/core/function/10-function.js:377:21) at /usr/local/lib/node\_modules/node-red/node\_modules/@node-red/runtime/lib/nodes/Node.js:203:26 at Object.trigger (/usr/local/lib/node\_modules/node-red/node\_modules/@node-red/runtime/lib/hooks.js:113:9) at FunctionNode.Node.\_emitInput (/usr/local/lib/node\_modules/node-red/node\_modules/@node-red/runtime/lib/nodes/Node.js:195:11) at FunctionNode.Node.emit (/usr/local/lib/node\_modules/node-red/node\_modules/@node-red/runtime/lib/nodes/Node.js:179:25) at FunctionNode.Node.receive (/usr/local/lib/node\_modules/node-red/node\_modules/@node-red/runti...

As you can see, the message begins with:

TypeError: Cannot read property 'a' of null at Function node:4f5f2b5e.37917c:7:9

where 7:9 indicates the line:column but to whatever reasons the line number is indicates the next line (7) but the error here happens at line 6. Note that some said on certain forums (stackoverflow) that the e.stack property is browser dependent (I tried it in FF).

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 February 2021 11:01 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/6 "2021-02-25T11:01:44Z")

</div>

> [@r0ller](#):
>
> whatever reasons the line number is indicates the next line (7) but the error here happens at line 6. Note that some said on certain forums (stackoverflow) that the e.stack property is browser dependent (I tried it in FF).

The function node runs at server side. Browser is irrelevant here.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [25 February 2021 11:32 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/7 "2021-02-25T11:32:14Z")

</div>

> [@r0ller](#):
>
> where 7:9 indicates the line:column but to whatever reasons the line number is indicates the next line (7) but the error here happens at line 6.

That is because the code you provide in the function node is wrapped in code we provide - so the line numbers of the running code are offset.

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 February 2021 12:06 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/8 "2021-02-25T12:06:27Z")

</div>

Yepp, you're right. Forgot about that, simply took the info over as in the forum it was discussed in browser context.

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 February 2021 12:08 UTC](https://discourse.nodered.org/t/custom-node-red-logger-show-line-numbers/17924/9 "2021-02-25T12:08:49Z")

</div>

Thanks for the info! I simply played around with adding commented lines here and there to see if it's always n-1 and after a while I concluded that it's reliable 🙂
