I am doing my own logger in android by writing to a file. Currently, I call function that opens a file and appends a string to it. However, i read somewhere that it is exccessive ( operation heavy) to open a file everytime I log to it. Is this true? What’s the alternative here? Should I open a file a leave it open for the duration of the app and just append to it? Or what do you suggest?
Thank you very much
It depends how much logging you’re doing – How many logs a second, roughly how much data, how critical the logs are, etc. If you’re only dropping something into the file a couple times a minute, than as long as it’s not happening on the main thread and slowing other stuff down, you’re probably fine. If you do need to implement something more efficient, you might want to try some simple caching.
You could keep an in-memory cache of log data and save it to the file when it reaches a certain size (every 100 lines) or when a certain amount of time passes (every 5 minutes).
If you take this approach, remember to override onPause and onDestroy and make sure to write cached log data out to file before leaving the app, in case the user doesn’t come back.