Custom logger with JavaScript post request to telegram

I’m going to give this a try but hopefully someone can let me know of any issues I should be aware of. I am going to add a custom logger into the settings.js that uses the XMLHttpRequest in JavaScript to send a POST request to my telegram bot so I can get metrics sent there. Is there anything that I should be aware of? Not sure if this will work or not. Going to use the direct example the documentation has but modify it for a POST request.

My setup is a raspberry pi, with a node-red docker.

Why not useone of the contrib telegram nodes?

Hi well becuaes I wanted the logs to just come in without me having to enter the debug nodes all the time or add nodes all the time. Also was interested in the logs that pertain going IN to a node and OUT a node to help be debug how the payloads are being sent and received.

i tried this and i didn't work

    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: true,
            // Whether or not to include audit events in the log output
            audit: false
        },
            log4nr: require("node-red-contrib-log4nr").logHandler

//          telegramLogger: {
//                  level: 'debug',
//                  metrics: true,
//                  handler: function(settings) {
//                          var http = new XMLHttpRequest();
//                          var url = 'https://api.telegram.org/bot<Token>/sendMessage';
//                          var data = 'chat_id=<user_id>&text=');
//                          http.open('POST', url, true);
//
//                          http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
//
//                          http.send(data + msg);
//                  }
//          },

The big problem you are going to see will likely be due to data volume. Telegram is not designed to take high-speed logging data. You might even get your bot banned - I'm not sure what limits are in place with Telegram but bear in mind that you are not paying for the service. There will be limits.

Instead, I would look for one of the logging services, some of which have free tiers. With these, you connect to their API and they are designed to handle high-volume throughputs. Generally you can also set up alerts which Telegram is good for.

Yea you’re right and that was a concerned I had. What service do you recommend? Also do you know of any examples on how to export the logging to a local database or text file?

Here is what I want: to not have to attach nodes for debugging and to just be able to view an events list on phone and pc to see how my flows execute. Right now I have a flow that I don’t know why it’s not executing. I can’t find anywhere on the forums on how people do this. I know I can add a bunch of nodes to send to telegram but if the debug is already being gathered by Node-red why can’t I just send that somewhere to see it.

Debug doesn't happen unless you add a debug node. You can set the debug output to log which means it appears in the Node-RED log. If you run Node-RED manually via something like PM2, then PM2 will control the logging. If you run it via systemd on Linux, then you can use journalctl to see the logs. If you run NR manually, you can redirect the console output to a file.

3rd-party logging tools will redirect your log to their web app. You'll need to search for that, there are a few.

The other thing you can do is to change the logging level, this may well produce output that gives you more clues since many node creators embed additional logs at trace level for example. See the settings.js file for details.

Node-RED logging is really comprehensive, you just need to play with the settings and debug nodes to get what you want.

Ok so pardon my ignorance but what is PM2? I have node red running in docker.

So I know that the debug doesn’t happen unless you have a debug node however what about the metric that are written in the settings.js? Doesn’t that happen for everything regardless of you putting in a debug node or not?

Nothing happens unless someone tells it to. If you turn up the logging, lets say to trace and turn on metrics and audit, you will see a lot of internal data that the guys have put into Node-RED itself. But that isn't necessarily true of contributed nodes.