Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split long messages #29

Closed
dec0dOS opened this issue Nov 5, 2021 · 10 comments
Closed

Split long messages #29

dec0dOS opened this issue Nov 5, 2021 · 10 comments

Comments

@dec0dOS
Copy link

dec0dOS commented Nov 5, 2021

Hello, thanks for the awesome project!

I have an issue with the long messages as all unhandled exceptions from the server are sent to telegram.

For example:

logger.error("1".repeat(5000));

Logger.error is used for logging messages to telegram.

This line throws the following exception:

[error] uncaughtException: Unhandled error. ('400: Bad Request: message is too long')
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('400: Bad Request: message is too long')
 at new NodeError (node:internal/errors:371:5)
 at DerivedLogger.emit (node:events:383:17)
 at DerivedLogger.transportEvent (/project-path/node_modules/winston/lib/winston/logger.js:630:12)
 at Telegram.emit (node:events:406:35)
 at /project-path/node_modules/winston-telegram/lib/winston-telegram.js:133:14
 at IncomingMessage.<anonymous> (/project-path/node_modules/winston-telegram/lib/winston-telegram.js:155:9)
 at IncomingMessage.emit (node:events:406:35)
 at endReadableNT (node:internal/streams/readable:1343:12)
 at processTicksAndRejections (node:internal/process/task_queues:83:21)

The message should be split after 4096 characters to fix unexpected logging exceptions.

For now, I'm using the following technique to cut the message:

    formatMessage: function (options) {
      let message = options.message;
      if (message.length > 4000) {
        message = message.substring(0, 4000);
      }
      return message;
    },
@ivanmarban
Copy link
Owner

Do you really need to send unhandled exceptions to telegram ?

Maybe you can configure the logger to not send them to telegram and configure another logger to take care of them ?

Something similar to this: https://github.com/winstonjs/winston#exceptions

@dec0dOS
Copy link
Author

dec0dOS commented Nov 5, 2021

@ivanmarban, thanks a lot for the quick response!

Yes, that is the case. I need to send some debugging data also. I expect that message splitting is handled on the library side but not on the main application level.

That would be awesome if the message splitting feature could be implemented in winston-telegram.

@dec0dOS
Copy link
Author

dec0dOS commented Nov 5, 2021

Logging to the console or the file works as expected, by the way. The problem is when the long message is sent to the telegram.

@ivanmarban
Copy link
Owner

I will try to implement the functionality to automatically split messages longer than 4096 characters as soon as my free time let me.

Thanks for your feedback !

@ivanmarban
Copy link
Owner

BTW any help is welcome of course.

At first sight my idea is to make a function that wraps around 'send' to add the split functionality.

@dec0dOS
Copy link
Author

dec0dOS commented Nov 5, 2021

Splitting for chunks function could be something like this:

function chunkString (str, len) {
  const size = Math.ceil(str.length/len);
  const arr = Array(size);
  let offset = 0;
  
  for (let i = 0; i < size; i++) {
    arr[i] = str.substr(offset, len);
    offset += len;
  }
  
  return arr;
}

Source: https://stackoverflow.com/a/14349616

Seems that it is one of the fastest and the simplest solutions to split the string into chunks.

@ivanmarban
Copy link
Owner

@dec0dOS could you please give a try at the code on feature/split-long-messages branch and give some feedback ?

@dec0dOS
Copy link
Author

dec0dOS commented Nov 8, 2021

@ivanmarban, it works as expected in all my use cases. Thanks a lot!

@dec0dOS
Copy link
Author

dec0dOS commented Nov 8, 2021

@ivanmarban, is it going to be merged into the main branch?

@ivanmarban
Copy link
Owner

Released new feature on v2.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants