1

I have an application with many modules and packages. All these use logrus for logging.

Application is running in a loop. Loop after loop. When application is has a fatal problem it prints out the log at that step. So I get the log at the error step; but no log about the in between steps that had succeeded.

Here is my requirement

In every loop the application should write lines in some log storage.

If the loop succeeds, it should clear this storage and be ready for next loop.

If there is any problem or error then it should print out all the accumulated log and end.

This way I will get to know the entire loop history when and only when there is an error. If the loop succeeds, there should not be any log printed.

Any idea how should I achieve this? Thanks for any guideline in advance.

user1940163
  • 191
  • 2
  • 7
  • 1
    What about another approach? If I understood correctly, you could have every loop to generate an ID, and you add that ID to the log messages as a field, when there's a failure, you get the ID and filter the log so you get the trace. – Eduardo Thales Jun 15 '22 at 23:57
  • A loop has many steps. If a step fails, the log is printed at that step. But we do not get to see the different var values before that step. So idea is every step will log to some buffer and at the loop end buffer will be flushed. If there is error at any step, the entire accumulated buffer should be printed. – user1940163 Jun 16 '22 at 00:20
  • 1
    I don't see why you have to clear the buffer. If the issue is that you want to track down the sequence of steps in a iteration that failed, you could use an ID value, could be the loop index, could be an UUID, and filter the logs by the ID, it's a pretty common practice. – Eduardo Thales Jun 16 '22 at 00:24
  • 1
    If you really want to go with your solution, then you don't need logging, you can have a "Report" type that's really just a slice, the loop creates one, the steps add messages to it, if it fails, you print the messages in the slice. But this is assuming that the number of messages are not huge (in the range of dozens of thousands). – Eduardo Thales Jun 16 '22 at 00:26

0 Answers0