# Best practices for console.log in contrib nodes?

**URL:** <https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997>\
**Category:** Developing Nodes\
**Created:** [31 December 2022 15:57 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997 "2022-12-31T15:57:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudest](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dudest/32/72328_2.png) [@dudest](https://discourse.nodered.org/u/dudest)\
**Post date:** [31 December 2022 15:57 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/1 "2022-12-31T15:57:09Z")

</div>

Hello,

I'd like my custom nodes to log in the seamlessly in terminal window or in the debug window. I was wondering if there are any best practices for this or a `util` that's available that will handle the timestamping and formatting.

I'm trying to avoid doint this:

```auto
this.socket.on('error', function (err) {
   console.log(err.toString());
});

```

Thanks and happy NYE 🪩 🎉 🥳

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [31 December 2022 16:13 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/2 "2022-12-31T16:13:48Z")

</div>

you can use node.log("words") to write to the log file, node.warn("words") to the log and sidebar, and node.error("words",msg) to create a error that the catch node can handle (and goes to the log) - and where msg is the incoming message that caused the error.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 December 2022 16:17 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/3 "2022-12-31T16:17:25Z")

</div>

See [here](https://nodered.org/docs/creating-nodes/node-js#handling-errors) for details of how to handle errors.

---

<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:** [31 December 2022 16:18 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/4 "2022-12-31T16:18:06Z")

</div>

The docs have a section on logging: [JavaScript file : Node-RED](https://nodered.org/docs/creating-nodes/node-js#logging-events)

In those examples, `this` is your node. To access them inside a callback function like in your example, you'll want to assign `this` to a variable so you can reference it.

You'll commonly see the following used:

```auto
const node = this

```

So you can then use `node.log(...)`

---

<div class="post-metadata">

**Author:** ![tve](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tve/32/59475_2.png) [@tve](https://discourse.nodered.org/u/tve)\
**Post date:** [31 December 2022 16:26 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/5 "2022-12-31T16:26:50Z")

</div>

> [@knolleary](#):
>
> To access them inside a callback function like in your example, you'll want to assign `this` to a variable so you can reference it.

The contemporary javascript way would be to use a closure, for example:

```auto
this.socket.on('error', (err) => {
   this.log(`socket error: ${err}`);
});

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [31 December 2022 16:27 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/6 "2022-12-31T16:27:02Z")

</div>

ah yes - need to tweak that error example to include the msg.

---

<div class="post-metadata">

**Author:** ![tve](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tve/32/59475_2.png) [@tve](https://discourse.nodered.org/u/tve)\
**Post date:** [31 December 2022 17:01 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/7 "2022-12-31T17:01:40Z")

</div>

Additional note: while `console.log` takes multiple arguments and prints them space-separated, the various `this.log`, `this.warn`, `this.error` don't: they only have a single string argument (plus the msg argument for error).

There is still a role for console.log, which is to print a data structure. JSON.stringify can work as an alternative in many cases, but it barfs if there is any circularity. There's probably some helper in Node.js, though...

---

<div class="post-metadata">

**Author:** ![dudest](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dudest/32/72328_2.png) [@dudest](https://discourse.nodered.org/u/dudest)\
**Post date:** [31 December 2022 17:39 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/8 "2022-12-31T17:39:09Z")

</div>

Thank you all. I really appreciate all the great advice 😀

---

<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:** [31 December 2022 17:51 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/9 "2022-12-31T17:51:14Z")

</div>

> [@tve](#):
>
> There is still a role for console.log

But it is discouraged - because if Node-RED is running in an environment with a custom log handler, console.logs won't be captured.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 January 2023 17:51 UTC](https://discourse.nodered.org/t/best-practices-for-console-log-in-contrib-nodes/72997/10 "2023-01-14T17:51:56Z")

</div>

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