logger icon indicating copy to clipboard operation
logger copied to clipboard

Save logs to the file

Open wuxinxi opened this issue 8 years ago • 19 comments
trafficstars

Thank you very much. Save the file. Can you customize the save time? Generate a file a day, thank you

wuxinxi avatar Jul 27 '17 08:07 wuxinxi

@wuxinxi how are you writing the logs to file? i'm unable to find how to get this implemented

saikrishna321 avatar Aug 05 '17 15:08 saikrishna321

Modify the source code can be achieved

wuxinxi avatar Aug 11 '17 17:08 wuxinxi

@wuxinxi any working example ? i'm sorry if i'm asking for more

saikrishna321 avatar Aug 12 '17 14:08 saikrishna321

Logger.addLogAdapter(new DiskLogAdapter());

wuxinxi avatar Aug 13 '17 02:08 wuxinxi

Each saved file has 500kb, if you want to customize the path of their own name need to change the source code

wuxinxi avatar Aug 13 '17 02:08 wuxinxi

@wuxinxi thanks .. as i have multiple devices connected to the machine... Is it possible to tell the logger to get from specific device

saikrishna321 avatar Aug 13 '17 06:08 saikrishna321

sure

wuxinxi avatar Aug 13 '17 13:08 wuxinxi

@wuxinxi can we have multiple loggers? As in both AndroidLogAdapter() and DiskLogAdapter() ?

Shahroz16 avatar Sep 12 '17 09:09 Shahroz16

yes

wuxinxi avatar Sep 14 '17 01:09 wuxinxi

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"

Kimmidhingra avatar Dec 04 '17 12:12 Kimmidhingra

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;

	}
}

a-reznic avatar Mar 16 '18 09:03 a-reznic

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.

orhanobut avatar Mar 27 '18 12:03 orhanobut

Where is log file located? Newbie here

Shempaiii avatar Aug 24 '18 03:08 Shempaiii

Where is log file located? Newbie here

/sdcard/logger/logs_0.csv

bu2zhouzhu avatar Mar 27 '19 07:03 bu2zhouzhu

How to save the file to internal storage?

longld103 avatar Nov 01 '19 02:11 longld103

How to save the file to internal storage?

use DiskLogAdapter and CsvFormatStrategy

wuxinxi avatar Nov 01 '19 03:11 wuxinxi

Where is log file located in internal storage?

longld103 avatar Nov 01 '19 03:11 longld103

Where is log file located in internal storage?

check it out CsvFormatStrategy

wuxinxi avatar Nov 01 '19 03:11 wuxinxi

Yeah... thank for your support ^^

longld103 avatar Nov 01 '19 04:11 longld103