logger
logger copied to clipboard
Save logs to the file
Thank you very much. Save the file. Can you customize the save time? Generate a file a day, thank you
@wuxinxi how are you writing the logs to file? i'm unable to find how to get this implemented
Modify the source code can be achieved
@wuxinxi any working example ? i'm sorry if i'm asking for more
Logger.addLogAdapter(new DiskLogAdapter());
Each saved file has 500kb, if you want to customize the path of their own name need to change the source code
@wuxinxi thanks .. as i have multiple devices connected to the machine... Is it possible to tell the logger to get from specific device
sure
@wuxinxi can we have multiple loggers? As in both AndroidLogAdapter() and DiskLogAdapter() ?
yes
I am using following code in my application class FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder() .tag("custom") .build(); Logger.addLogAdapter(new DiskLogAdapter(formatStrategy)); But log file is not generated in my phone. I have checked in "File Manager"
Custom file name and custom log folder
kotlin
val file = File(Environment.getExternalStorageDirectory(), "log")
if (!file.exists()) {
file.mkdir()
}
Logger.addLogAdapter(object : DiskLogAdapter(CsvFormatStrategy.newBuilder().tag("tag")
.logStrategy(DiskLogStrategy(DiskLogHandler(file.absolutePath, BuildConfig.APPLICATION_ID, 500 * 1024)))
.build()) {
override fun isLoggable(priority: Int, tag: String?): Boolean {
// return Const.IS_RELEASE;
return true;
}
})
java
public class DiskLogHandler extends Handler {
private final String folder;
private final int maxFileSize;
private final String fileName;
public DiskLogHandler(String folder, String fileName, int maxFileSize) {
this(getDefaultLooper(), folder, fileName, maxFileSize);
}
public DiskLogHandler(Looper looper, String folder, String fileName, int maxFileSize) {
super(looper);
this.folder = folder;
this.fileName = fileName;
this.maxFileSize = maxFileSize;
}
private static Looper getDefaultLooper() {
HandlerThread ht = new HandlerThread("AndroidFileLogger");
ht.start();
return ht.getLooper();
}
@SuppressWarnings("checkstyle:emptyblock")
@Override
public void handleMessage(Message msg) {
String content = (String) msg.obj;
FileWriter fileWriter = null;
File logFile = getLogFile(folder, fileName);
try {
fileWriter = new FileWriter(logFile, true);
writeLog(fileWriter, content);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
if (fileWriter != null) {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e1) { /* fail silently */ }
}
}
}
/**
* This is always called on a single background thread.
* Implementing classes must ONLY write to the fileWriter and nothing more.
* The abstract class takes care of everything else including close the stream and catching IOException
*
* @param fileWriter an instance of FileWriter already initialised to the correct file
*/
private void writeLog(FileWriter fileWriter, String content) throws IOException {
fileWriter.append(content);
}
private File getLogFile(String folderName, String fileName) {
File folder = new File(folderName);
if (!folder.exists()) {
//TODO: What if folder is not created, what happens then?
folder.mkdirs();
}
int newFileCount = 0;
File newFile;
File existingFile = null;
newFile = new File(folder, String.format("%s_%s.csv", fileName, newFileCount));
while (newFile.exists()) {
existingFile = newFile;
newFileCount++;
newFile = new File(folder, String.format("%s_%s.csv", fileName, newFileCount));
}
if (existingFile != null) {
if (existingFile.length() >= maxFileSize) {
return newFile;
}
return existingFile;
}
return newFile;
}
}
I think we can play around and make DiskLogAdapter more flexible. I'm happy to get some examples and parameters that can be more flexible.
Where is log file located? Newbie here
Where is log file located? Newbie here
/sdcard/logger/logs_0.csv
How to save the file to internal storage?
How to save the file to internal storage?
use DiskLogAdapter and CsvFormatStrategy
Where is log file located in internal storage?
Where is log file located in internal storage?
check it out CsvFormatStrategy
Yeah... thank for your support ^^