Build a custom Logger

Hi,

I'm using node-red quite a lot, and i'm planning to save all the logs that comes out from the nodes to a database, as i'm writing this post, i've learned how to create a custom log handler (settings.js) for the logs that comes to nodered and i'm able to save them to a Postgres database table, as shown below:

// Configure the logging output
    logging: {
        // Only console logging is currently supported
        console: {
            // Level of logging to be recorded. Options are:
            // fatal - only those errors which make the application unusable should be recorded
            // error - record errors which are deemed fatal for a particular request + fatal errors
            // warn - record problems which are non fatal + errors + fatal errors
            // info - record information about the general running of the application + warn + error + fatal errors
            // debug - record information which is more verbose than info + info + warn + error + fatal errors
            // trace - record very detailed logging + debug + info + warn + error + fatal errors
            // off - turn off all logging (doesn't affect metrics or audit)
            level: "info",
            // Whether or not to include metric events in the log output
            metrics: false,
            // Whether or not to include audit events in the log output
            audit: false
        },
        // Custom logger
        psql: {
            level: 'info',
            metrics: true,
            handler: function (settings) {
                // Called when the logger is initialised                
                const Pool = require('pg').Pool
                const pool = new Pool({
                    user: 'myuser',
                    host: 'db',
                    database: 'mydb',
                    password: 'mypass',
                    port: 5432,
                })

                // Return the logging function
                return function (msg) {
                    console.log(msg);


                    pool.query('INSERT INTO node_log (data) VALUES ($1)', [msg], (error, results) => {
                        if (error) {
                            throw error
                        }
                    })
                }
            }
        }
    },

And this is the generated log:

mynodered    | { level: 40, msg: 'Stopping flows', timestamp: 1562850905500 }
mynodered    | 11 Jul 13:15:05 - [info] Stopped flows
mynodered    | { level: 40, msg: 'Stopped flows', timestamp: 1562850905503 }
mynodered    | 11 Jul 13:15:05 - [info] Starting flows
mynodered    | { level: 40, msg: 'Starting flows', timestamp: 1562850905505 }
mynodered    | 11 Jul 13:15:05 - [info] Started flows
mynodered    | { level: 40, msg: 'Started flows', timestamp: 1562850905507 }
mynodered    | { level: 99,
mynodered    |   nodeid: '77ec42e2.7dc2fc',
mynodered    |   event: 'node.inject.receive',
mynodered    |   msgid: '350414b5.4b64ac',
mynodered    |   value: undefined,
mynodered    |   timestamp: 1562850912684 }
mynodered    | { level: 99,
mynodered    |   nodeid: '77ec42e2.7dc2fc',
mynodered    |   event: 'node.inject.send',
mynodered    |   msgid: '350414b5.4b64ac',
mynodered    |   value: undefined,
mynodered    |   timestamp: 1562850912685 }
mynodered    | { level: 99,
mynodered    |   nodeid: '595ebe6d.50593',
mynodered    |   event: 'node.file.receive',
mynodered    |   msgid: '350414b5.4b64ac',
mynodered    |   value: undefined,
mynodered    |   timestamp: 1562850912685 }
mynodered    | { level: 99,
mynodered    |   nodeid: '595ebe6d.50593',
mynodered    |   event: 'node.file.send',
mynodered    |   msgid: '350414b5.4b64ac',
mynodered    |   value: undefined,
mynodered    |   timestamp: 1562850912688 }
mynodered    | { level: 99,
mynodered    |   nodeid: 'c1a389bb.147b68',
mynodered    |   event: 'node.debug.receive',
mynodered    |   msgid: '350414b5.4b64ac',
mynodered    |   value: undefined,
mynodered    |   timestamp: 1562850912689 }
mynodered    | { level: 99,
mynodered    |   event: 'runtime.memory.rss',
mynodered    |   value: 101470208,
mynodered    |   timestamp: 1562850912910 }
mynodered    | { level: 99,
mynodered    |   event: 'runtime.memory.heapTotal',
mynodered    |   value: 74620928,
mynodered    |   timestamp: 1562850912912 }
mynodered    | { level: 99,
mynodered    |   event: 'runtime.memory.heapUsed',
mynodered    |   value: 67069024,
mynodered    |   timestamp: 1562850912913 }
mynodered    | { level: 99,
mynodered    |   event: 'runtime.memory.rss',
mynodered    |   value: 101470208,
mynodered    |   timestamp: 1562850927917 }

Now, my goal is really simple: i want to identify the parent flow name of those nodes, and the name/description (or better, the icon that it's shown in the node) of the node, parse the data and make it usable for log debugging/event inspection in my web application.

Many thanks!

1 Like

Hi,

a) nice - would make a good snippet/example for the flows.nodered.org site
b) you should be able to use the admin api to get the flow and that will of course be a big json object containing all the ids, types and names (if set) - but not icons

Thanks,

So you suggest me to interface from my webapp to the nodered admin API to get all of these infos?

well something needs to get the flow info you wanted.

It isn't quite clear what software-flow i need to follow, can you explain more about your idea?