# Custom logger in setting.js to log in file

**URL:** <https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742>\
**Category:** General\
**Created:** [20 August 2025 08:05 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742 "2025-08-20T08:05:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![greenDog](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@greenDog](https://discourse.nodered.org/u/greenDog)\
**Post date:** [20 August 2025 08:05 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/1 "2025-08-20T08:05:20Z")

</div>

Hello everyone,  
I would like to save the messages from the console in a new log every day so that I can receive the error message if Node-RED crashes.  
What do I need to change in the settings.js file to create a daily log file in the folder where the settings.js file is located? Saving the logs in a subfolder called “logs” would also be an option.  
The log should save the following information, which can also be seen in the console:

```auto
20 Aug 09:50:36 - [info]

Welcome to Node-RED
===================

20 Aug 09:50:36 - [info] Node-RED version: v4.0.9
20 Aug 09:50:36 - [info] Node.js version: v22.17.1
20 Aug 09:50:36 - [info] Windows_NT 10.0.26100 x64 LE
20 Aug 09:50:37 - [info] Loading palette nodes
20 Aug 09:50:37 - [info] Settings file : C:\Users\RS7340\.node-red\settings.js
20 Aug 09:50:37 - [info] Context store : 'default' [module=memory]
20 Aug 09:50:37 - [info] User directory : \Users\RS7340\.node-red
20 Aug 09:50:37 - [warn] Projects disabled : editorTheme.projects.enabled=false
20 Aug 09:50:37 - [info] Flows file : \Users\RS7340\.node-red\flows.json
20 Aug 09:50:37 - [info] Creating new flow file

```

Thanks!

---

<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:** [20 August 2025 08:25 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/2 "2025-08-20T08:25:22Z")

</div>

Hi, you need to look at the `logging` property in the settings. It is an object and you can add new sub-properties to create a new logging instance.

```javascript
{
    level: 'trace',
    metrics: false,
    audit: false,
    handler: function(settings) {
        // ...
    }
}

```

---

<div class="post-metadata">

**Author:** ![greenDog](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@greenDog](https://discourse.nodered.org/u/greenDog)\
**Post date:** [20 August 2025 08:29 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/3 "2025-08-20T08:29:34Z")

</div>

I found the logging property in the settings, but I don't know what to write in the handler so that the logging is saved in a file.

---

<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:** [20 August 2025 08:45 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/4 "2025-08-20T08:45:06Z")

</div>

I no longer use this but it is an example of the type you mention. (I have a separate log output to MQTT on my dev instance so that I can view detailed logs from uibuilder on a web page).

```js
        /** Custom logging: https://nodered.org/docs/user-guide/runtime/logging */
        tilog: {
            level: 'trace',
            metrics: false,
            audit: false,
            handler: function(settings) {
                // Called when the logger is initialised
                const fs = require('fs')
                const util = require('util')
                const Chalk = require('chalk')
                const chalk = new Chalk.Instance({level: 3})
                // Use flags 'a' to append or 'w' to create new on restart
                const log_file = fs.createWriteStream(__dirname + '/uibuilder.log', {flags : 'w'})

                const tilogLevels = {
                    10: 'FATAL', 20: 'ERROR', 30: 'WARN ', 40: 'INFO ', 50: 'DEBUG', 60: 'TRACE', 98: 'AUDIT', 99: 'MTRIC'
                }

                const tilog = function(d) { //
                    log_file.write(util.format.apply(null, arguments) + '\n')
                    //log_stdout.write(util.format(d) + '\n');
                }

                /** Return the logging function
                 * msg schema: { id, level, type, msg, timestamp}
                 * id = The node instance that produces the msg
                 * level = 10: fatal, 20: error, 30: warn, 40: Info, 50: Debug, 60: Trace
                 * timestamp = Javascript timestamp. Use `(new Date(msg.timestamp)).toIsoString()` or similar to convert
                 * type = 'flow' - not clear when/why this is produced
                 */
                return function(msg) {
                    if ( msg.level < 51 || msg.msg.includes('[uibuilder') || msg.msg.startsWith('+-') || msg.msg.startsWith('| ') || msg.msg.startsWith('>>') ) {
                        //console.log('TOLOG: ', msg)
                        if (msg.msg.includes('[uibuilder:')) msg.msg = msg.msg.replace('[uibuilder:', '[')
                        let msgColor, introColor
                        switch (msg.level) {
                            case 10: { // fatal
                                msgColor = chalk.redBright
                                introColor = msgColor.bold.inverse
                                break
                            }

                            case 20: { // error
                                msgColor = chalk.red
                                introColor = msgColor.inverse
                                break
                            }

                            case 30: { // warn
                                msgColor = chalk.rgb(255, 185, 0)
                                introColor = msgColor.inverse
                                break
                            }

                            case 40: { // info
                                msgColor = chalk.yellow
                                introColor = msgColor.inverse
                                break
                            }

                            case 50: { // debug
                                msgColor = chalk.greenBright
                                introColor = msgColor.inverse
                                break
                            }

                            case 60: { // trace
                                msgColor = chalk.cyanBright
                                introColor = msgColor.inverse
                                break
                            }

                            default: {
                                msgColor = chalk.reset
                                introColor = msgColor.inverse
                                break
                            }
                        }
                        let intro = introColor(tilogLevels[msg.level]+'|')
                        tilog( intro, msgColor(msg.msg) )
                    }
                    // else {
                    // tilog(
                    // `?? ${tilogLevels[msg.level]}:`, msg.msg
                    // )
                    // }
                }
            }
        },

```

As an alternative, you might want to look at using the `winston` logging tool.

---

<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:** [20 August 2025 09:48 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/5 "2025-08-20T09:48:26Z")

</div>

A potential alternative approach:

Run as a service (search NSSM on this forum). NSSM can redirect logs to a text file.

Alternatively, if you can, run Node-RED in linux and use the system journal.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [21 August 2025 17:32 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/6 "2025-08-21T17:32:22Z")

</div>

> [@Steve-Mcl](#):
>
> Run as a service

In your service invocation script, make sure you redirect the output to the log file with` >>` (append) and not `>` (replace).

A problem I encountered when running as a service with NSSM, is that it automatically restarts the Node-RED server process upon crash. Hence the process may be constantly crashing & restarting without me knowing about it.  
Does anyone know how to control this?

---

<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:** [21 August 2025 20:18 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/7 "2025-08-21T20:18:42Z")

</div>

Open the Windows Services tool, find the nssm Node-RED service and double-click. Then go to the Recovery tab:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14d2f2fa255afccdd45531ed69f7da4839d843f3.png)

You can control everything from there. You can even run another programme on error failure. You could use an AutoHotKey script to show a dialog box for example.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [21 August 2025 20:34 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/8 "2025-08-21T20:34:33Z")

</div>

Thanks Julian!

---

<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:** [19 November 2025 20:35 UTC](https://discourse.nodered.org/t/custom-logger-in-setting-js-to-log-in-file/98742/9 "2025-11-19T20:35:02Z")

</div>

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