0

Since Jelly Been I am not able to tell my testers to use alogcat app to send me logs, because Android now doesn't allow apps to read logs.

Because of this I need to implement my own log-to-file, I'm thinking about something like:

MyLog.d(TAG, "some log");

And as I'm somewhat new to Java's IO performance wise (especially in android perspective), what I'm not sure about is what is the best option to implement this function:

  • MyLog.d opens file handle, writes to file, closes handle
  • MyLog.d opens a static file handle on first invocation and keeps it open during the whole app session, all writes are sent to it as required, flushes follow each write. closes it somewhere on app exit/going-to-bg

Because there are potentially big amounts of logging calls involved, I'm worried that first one might cause to many handle opens/closes and this might degrade performance. While the second one might have some other drawbacks (data losses? anything else?)

Also which is preferrable from the GC perspective? Looks like first one would generate quite some garbage too (new OutputStream on every log call)

So it seems that first option tends to end up quite expensive, while I'm not sure what 'hidden bad things' second one might have :)

Would be grateful for advice from anyone with experience in this area.

Community
  • 1
  • 1
dimsuz
  • 8,969
  • 8
  • 54
  • 88
  • May be this: [How to add a new line of text to an existing file in Java?](http://stackoverflow.com/questions/4614227/how-to-add-a-new-line-of-text-to-an-existing-file-in-java) – alex Dec 18 '13 at 18:38
  • As of Android 4.4.2 you can still read the logs **of your own app** so if you package the collection functionality within your .apk you can still do this using the normal logging. – Chris Stratton Dec 18 '13 at 18:50
  • @alex hey, I do know how to add line to a file :) I asked mainly about what's the best option to do it in a certain usage scenario. – dimsuz Dec 19 '13 at 10:39
  • @ChrisStratton any hints on how to do that? something [like this](http://stackoverflow.com/questions/12692103/read-logcat-programmatically-within-application)? I also found that android has java.util.logging available and it has a FileHandler, maybe it's best to try to use that. – dimsuz Dec 19 '13 at 10:41
  • @dimsuz I think the link answers your question: when to open, when to close file handle? though you could choose different scenario. Like what amount of logs you plan to make on this client? – alex Dec 19 '13 at 17:30
  • @alex, I've asked mainly about what implications would either strategy result in and why. – dimsuz Dec 24 '13 at 07:43

0 Answers0