Reputation: 5713
I have added bunyan logging to various subsystems of my application. But when I see the log file, I find that the the log is not ordered by timestamp. I assume this is expected because of the async nature of nodejs.
I wanted to know if there is any way I can change this so that the logs appear sequentially (chronologically)?
Upvotes: 2
Views: 541
Reputation: 839
Guaranteeing sort order in a runtime logger is tricky if you're also looking for peek performance, minimal memory overhead, etc. As you suggest the async nature of node contributes to this.
Probably not the answer you'd like to hear but you'll need to post-process the file to guarantee order. On linux a simple solution for this is to just use sort on the log file (which should work if your timestamps are in ISO format and are at the front of each line):
$ sort your-bunyan-logfile
Now you might be looking to sort a live log. Not technically difficult but I'm unaware of anything that does this out of the box. You'd need to write something that keeps a number of lines in memory for at least a short duration (to facilitate those late logs) and then displays them after a short delay. You'll need to make some decisions about max lines to keep around and how long to keep them...
This all feels above and beyond what you want to get yourself into but if you built something that did this (and called it livesort
), had it read from stdin and write to stdout then you could do something like this to monitor a live logfile :
$ tail -f your-bunyan-logfile | livesort
Good luck! -Darrin
Upvotes: 2