I’m investigating MongoDB as a log repository. I’ve set up a basic MongoDB server, configured Log4j, and everything seems to work.
However:
- I’m aware that MongoDB writes are asynchronous (and I don’t want to kill performance by making them synchronous)
- The timestamp in the log records appears to be accurate only to the millisecond
- My app logs more than once per millisecond
- I need to be able to see the sequence in which the events were logged
Is there a way I can get more granular timestamps?
Is there a way I can inject sequence numbers on the client side (short of having the client put them in the log message)?
MongoDB inserts are sequential since write locks are process wide. As such you are guaranteed that MongoDB will return the log entries in the order it received them. Use sort({$natural:1}) to get them back in order.
Note that although writes are asynchronous they are ordered.