I'm Trying to Write Logs to Custom Log.txt File on Android File using this code of Mine but then this method creates file but contains nothing. Basically I want to read previous contents of the file and then append my data with the existing content.
The Code is as follows :
public static void write(String str)
{
InputStream fileInputStream = null;
FileOutputStream fileOutpurStream = null;
try
{
fileInputStream = new FileInputStream(file);
fileOutpurStream = new FileOutputStream(file);
if(file.exists())
{
int ch = 0;
int current = 0;
StringBuffer buffer = new StringBuffer();
while((ch = fileInputStream.read()) != -1)
{
buffer.append((char) ch);
current++;
}
byte data[]=new byte[(int)file.length()];
fileInputStream.read(data);
fileOutpurStream.write(data);
fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);
fileOutpurStream.flush();
}
else
{
file.createNewFile();
fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);
fileOutpurStream.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fileInputStream.close();
fileOutpurStream.flush();
fileOutpurStream.close();
fileOutpurStream = null;
fileInputStream = null;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Use slf4android lib.
It's simple implementation of slf4j api using android java.util.logging.*.
Features:
LoggerConfiguration.configuration().addHandlerToLogger
slf4android is maintained mainly by @miensol.
Read more about slf4android on our blog:
You can use the library I've written. It's very easy to use:
Add this dependency to your gradle file:
Initialize the library in the Application class:
This is how you use the library:
And this is how to retrieve the logs:
Here is the link to the github page with more information: https://github.com/danylovolokh/AndroidLogger
Hope it helps.
This may be late but hope this may help.. Try this....
here
bfwritter.newline
writes your text into the file. And add the permissionin your manifest file without fail.
This variant is much shorter
For those new to Java logging in general and Android logging
Some options for logging to txt in Android are below
logcat -f
as in this answer to log to file. Note that from Android 4.2, READ_LOGS permission doesn't have any impact and every Application (unless phone is rooted) could only read its own logs. The disadvantage here is logcat buffer is circular and has a size limit. You might not get earlier logs.Use Log4j with android-logging-log4j. What does android-logging-log4j do ? It makes Log4j easier to use in Android by giving two functions.
Simple example below. Notice that
logger
object in below example is a Log4j object returned and not an android-logging-log4j class. So android-logging-log4j is used only for configuring Log4j.Steps for using Log4j in Android.
Add both log4j-1.2.x.jar and android-logging-log4j-1.0.3.jar to the libs folder.
Add permissions only if using external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Write
Log4j
helper classIn Activity class
Example Source. Note that, log4j 2.x ( improved functionalities ) rewritten from scratch is not backward comptible with log4j 1.x. So you have to use log4j 1.2.x jar with android-logging-log4j jar. I was able to log to application internal file and later email the file with
setReadable(true, false)
Warning: I may be totally misunderstanding you, but if all you want is a log file, why sweat?
Put this in a bat file (change the path to your tools directory, and yourappname is of course your app's name):
Then in your code just do something similar to this:
To create the log, connect the USB cable and run your bat file.
Regards