1

My requirement is to write logs to a fixed size text file. I have gone through few articles regarding this but those are looking bit complex for me since I am new to programming.

As per my knowledge what I think I can do is write log data to a queue which is fixed size and when the queue is about to overflow then dequeue the old log data and enqueue new log data. At the end dump the entire queue data to a log text file.

Is it suggestible solution or any other better way for this?

  • Depending on the file size and the frequency of dumps (what do you mean "at the end"?), your solution may be good or it may be too heavy (frequent rewrites of a huge file). If the file allows random access, I would rather write directly to the file until I reach the end of it (maximal allowed position), then reset position back to begin-of-file and continue writing. I would reserve a space for 64-bit value of the last written position (either at begin or end of file), which I would update on each write. This will let the reader know where to start reading, – felix-b Jun 17 '19 at 18:47
  • @felix-b When I said "at the end", I mean before program exits write all data from queue to a text file. Coming to the file size and frequency - program executed every one hour and the file size would be around 20 MB for each run.Basically my program fetches some status values for each ID and writes to log. These status are not required to be stored in logs until unless there is an exception and I simply can't disable this logging of status because as the business wanted to check the status randomly. – user3747488 Jun 17 '19 at 18:54
  • For 20 MB at program exit your approach will definitely work (assuming it's running on a PC/server and not on a low-spec device). – felix-b Jun 17 '19 at 19:02

0 Answers0