Logging to a file on Android
Is there any way of retrieving log messages from an Android handset.
I'm building an application which uses the GPS of my HTC Hero. I can run and debug the application from eclipse but this isn't a good use case of GPS, sat at my desk.
When I fire the app up when I am walking around, I get an intermittent exception. Is there anyway I can output these exceptions to a text file on the SD card or output calls to Log.x("")
to a text file so that I can see what the exception is.
Thanks
EDIT : Solution
Here is the code I finally went with...
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
PrintWriter pw;
try {
pw = new PrintWriter(
new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
ex.printStackTrace(pw);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
I had to wrap the line
pw = new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
in a try/catch as Eclipse would not let me compile the app. It kept saying
Unhandled exception type IOException 1 quick fix Sorround with try/catch
So I did and it all works which is fine by me but it does make me wonder what Eclipse was on about...
Asked by: Emma845 | Posted: 20-01-2022
Answer 1
You could use Thread.setUncaughtExceptionHandler()
to catch the Exceptions.
Writing to SD Card is as simple as retrieving the directory for the card using Environment.getExternalStorageDirectory()
and creating a file there.
File f = new File(Environment.getExternalStorageDirectory(),filename);
You will need to give you app the correct permission to write to the SD Card by adding this to your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Answered by: Lydia936 | Posted: 21-02-2022
Answer 2
Log4j or slf4j can also be used as logging frameworks in Android together with logcat. See the project android-logging-log4j. Configuring logging to a (rotating) file(s) is very easy.
static {
final LogConfigurator logConfigurator = new LogConfigurator();
logConfigurator.setFileName(Environment.getExternalStorageDirectory() + "myapp.log");
logConfigurator.setRootLevel(Level.DEBUG);
// Set log level of a specific logger
logConfigurator.setLevel("org.apache", Level.ERROR);
logConfigurator.configure();
}
Answered by: Daryl972 | Posted: 21-02-2022
Answer 3
You could take a look at microlog4android. They have a solution ready to log to a file.
https://github.com/johanlkarlsson/microlog4android
Answered by: First Name465 | Posted: 21-02-2022Answer 4
I tried both options above (microlog4android & android-logging-log4j) and they were a struggle to try to get working. Once they compiled fine then I would get Runtime java.lang.NoClassDefFoundError.
So then I stumbled on logback-android ... also found here http://logback.qos.ch/
It has much better instructions and I got it working in a couple hours.
This appears to be the best logging solution for Android.
Answered by: Julian123 | Posted: 21-02-2022Answer 5
Use slf4-android lib created by BrightInventions. It's simple implementation of slf4j api using android java.util.logging.*.
Features:
- logging to file out of the box
- logging to any other destination by
LoggerConfiguration.configuration().addHandlerToLogger
- shake your device to send logs with screenshot via email
- really small, it tooks only ~55kB
slf4android is maintained mainly by @miensol.
Read more about slf4android on our blog:
Answered by: Roland503 | Posted: 21-02-2022Answer 6
Based on @Greg B solution and @JPM question in the comment, I made this static method in a seperate class and each time you instanciate a new thread, call it.
package com.stackoverflow.
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import android.os.Environment;
public class UnhandledExceptionHandler {
public static void setUndhandledException(Thread thread) {
if (thread.getUncaughtExceptionHandler() == null) {
Thread.currentThread().setUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread,
Throwable ex) {
PrintWriter pw;
try {
pw = new PrintWriter(new FileWriter(Environment
.getExternalStorageDirectory()
+ "/rt-networkandgps"
+ "/gpx/"
+ thread.getId() + ".log", true));
ex.printStackTrace(pw);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
}
Call it from anythread like this,
UnhandledExceptionHandler.setUndhandledException(Thread.currentThread());
Answered by: Melanie763 | Posted: 21-02-2022
Similar questions
logging - Android read error log from handset
How can I read the error log applications make? Is there any software which reads the error log from the handset and displays it?
I don't want to debug the app using eclipse, I'm looking for a handset based error log viewer.
logging - Android read error log from handset
How can I read the error log applications make? Is there any software which reads the error log from the handset and displays it?
I don't want to debug the app using eclipse, I'm looking for a handset based error log viewer.
android - Smack API giving error while logging into Tigase Server setup locally
I am currently developing android XMPP client to communicate with the Tigase server setup locally.Before starting development on Android I am writing a simple java code on PC to test connectivity with XMPP server.My XMPP domain is my pc name "mwbn43-1" and administrator username and passwords are admin and tigase respectively.
Following is the snippet of the code I am using
class Test {
public sta...
logging - Android adb logcat time filter
Does anyone know how to filter out the logcat ('adb shell logcat') so that it only shows the log statements after current date and time ? Thanks in advance.
logging - How to get Android crash logs?
I have an app that is not in the market place (signed with a debug certificate), but would like to get crash log data, whenever my application crashes. Where can I find a log of why my app crashed?
openid - Android app: logging in using google ID?
Well hello guys.
I am in progress of creating an android app that requires you to register and login.
Now I was thinking, why register and login? Isn't it possible to automaticly login with the google account that is already on your device? Just like how you can use your google account to login to stack overflow?
So my question is: is this possible, and how?
Thanks!
logging into youtube from android
I am trying to log into you tube using the following code
mService = new YouTubeService("Nothing", developer_key);
//Authentication
try
{
mService.setUserCredentials(mUsername, mPassword);
}
catch (AuthenticationException e)
{
Log.v(TAG, e.toString());
}
but I am getting:
ConnectYouTube: com.google.gdata.util.AuthenticationException: Error connecting with login URI
Android logging levels
I'm having a little difficulty configuring Android logging. Here's what my code looks like:
if (Log.isLoggable("MY_TAG", Log.VERBOSE)) {
Log.v("MY_TAG", "Here's a log message");
}
Pretty simple, right?
However, I'm having quite a bit of difficulty getting the Log.isLoggable("MY_TAG", Log.VERBOSE) to return true.
Per
Android: No logs while logging via slf4j-android
I use library which uses sfl4j-api for logging in android application. So I use slf4j-android to get logs, but I only got following messages:
I/AndroidLoggerFactory( 267): Logger name 'org.apache.mina.util.NamePreservingRunnable' exceeds maximum length of 23 characters, using 'o*.a*.m*.u*.NamePreser*' instead.
And no more logs from that library, but I see in code there are plenty of them....
java - Android fetch url, that needs logging in
I want to fetch data from the webpage, that contains some json data. The problem is, json data only returns if user is logged in on the website. If he is not, the login page is returned.
URL url = new URL(API_URL);
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream)));
StringBuilder builder = new StringBuilder();
String line = "";
wh...
logging - Android desktop log viewer
Closed. This question does not meet Stack Overflow guid...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android