# Add fields to custom logger

**URL:** <https://discourse.nodered.org/t/add-fields-to-custom-logger/58735>\
**Category:** General\
**Created:** [21 February 2022 08:45 UTC](https://discourse.nodered.org/t/add-fields-to-custom-logger/58735 "2022-02-21T08:45:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RubenVega](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@RubenVega](https://discourse.nodered.org/u/RubenVega)\
**Post date:** [21 February 2022 08:45 UTC](https://discourse.nodered.org/t/add-fields-to-custom-logger/58735/1 "2022-02-21T08:45:29Z")

</div>

Hello!

I am trying to improve the quality of our logs and investigating the possibility of custom logger.  
I have found it quite useful, but I would like to know if it is possible to add by default fields included in the msg.

I have found that by modifying the log function at line 157 of the file /@node-red/util/lib/log.js I can add more fields to this trace, however I can not retrieve fields from the running msg object.  
I would like the msg.id or other fields to appear in the final trace without the need to send the msg object to the logger.

For example, if in the code I write only  
`node.warn("hello")`

the trace that would be generated would be

```auto
{ level: 40,
  id: '485b456e.78e4cc',
  type: 'function',
  msg:"hello",
  _alias: '53712995.87dc28',
  z: '7684f2d.6579b0c',
  path: '6b036f2d.4f457/7684f2d.6579b0c',
  timestamp: 1645432467100,
//aditions
  msg_id: "XXXXX",
  msg_otherField:"XXXX"
 }

```

Could this result be achieved by modifying the /@node-red/util/lib/log.js or some other file?

---

<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:** [21 February 2022 09:01 UTC](https://discourse.nodered.org/t/add-fields-to-custom-logger/58735/2 "2022-02-21T09:01:46Z")

</div>

The calls to the node log functions are entirely independent of what messages are passing through. There is no concept of the 'running msg'.

To capture details of a message, you'd have to passing them in to the call to node.log

---

<div class="post-metadata">

**Author:** ![RubenVega](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@RubenVega](https://discourse.nodered.org/u/RubenVega)\
**Post date:** [21 February 2022 09:13 UTC](https://discourse.nodered.org/t/add-fields-to-custom-logger/58735/3 "2022-02-21T09:13:47Z")

</div>

Yes, more than the running msg, I was referring to the msg that has triggered the log event.

In the same log.js file at line 217 appears the following code:

```auto
    audit: function(msg,req) {
        msg.level = log.AUDIT;
        if (req) {
            msg.user = req.user;
            msg.path = req.path;
            msg.ip = req.ip || (req.headers && req.headers['x-forwarded-for']) || (req.connection && req.connection.remoteAddress) || undefined;
        }
        log.log(msg);
    }

```

That req property also travels inside msg right? couldn't you access to other properties of the msg to leave them traced?  
If not, how can the code retrieve that information?

Thanks!

---

<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:** [21 February 2022 09:32 UTC](https://discourse.nodered.org/t/add-fields-to-custom-logger/58735/4 "2022-02-21T09:32:49Z")

</div>

> [@RubenVega](#):
>
> That req property also travels inside msg right?

The audit log is a special case. It only gets used by the runtime to log access to the HTTP Admin API. The `req` property is passed in explicitly in those calls.

Again, this has nothing to do with messages passing through a flow. The fact the argument is called `msg` is probably a poor choice on my part.

---

<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:** [22 April 2022 09:33 UTC](https://discourse.nodered.org/t/add-fields-to-custom-logger/58735/5 "2022-04-22T09:33:42Z")

</div>

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