I'm making a library that can be used to log errors and other events in Android apps, before being sent to a server via HTTPS PUT. Is this possible using the native Log class or is there a better way to do this? Support for batch-uploading the logs would be a bonus, but I am happy to implement开发者_如何学Python that myself if necessary.
Essentially what I'm looking for is a logger that can be configured to send logs to a server rather than saving them locally, and that can perform this in the background without significantly affecting the performance of the app.
Create your own logger class. Say you call the class "MyLogger
", you can then create some methods that mimick the android methods, like MyLogger.logI(name,text);
etc.
Within your logI
method you can print to the standard android logs as usual (Log.i(...);
) and you can also send the text in the log to your http server.
If you need help with the http request, you should look at the apache commons
documentation for a good implementation. All you need to do is create the http request, add the data to the HttpPut
instance, and then send the request to the server (dealing with it in however way you see fit on the server). Make sure you do it in an AsyncTask
, otherwise you may slow down the ui etc if you are not using a background thread.
精彩评论