Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store logs level in different folder. #154

Open
swagatampanda3894 opened this issue Apr 16, 2024 · 0 comments
Open

Store logs level in different folder. #154

swagatampanda3894 opened this issue Apr 16, 2024 · 0 comments

Comments

@swagatampanda3894
Copy link

Inside the phone storage, I've created a folder named "MyLogs". Within that folder, there are four subfolders. I'm trying to insert values into specific folders, not all of them.

private void initializeXLog() {
File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
// Get the downloads directory
// Create a File object representing the "GA Logs" directory within the downloads directory
File gaLogsDir = new File(downloadsDir, "LogsDemo");

    // Get the current date in the format "dd_MMM" (e.g., "11_Apr")
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd_MMM");
    String currentDate = dateFormat.format(new Date());


    Date d = new Date();
    SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formattedDate = ff.format(d);
    Log.i("Current Date and Time: ","Date1: " + formattedDate);


    // Create a File object representing the current date folder within the "MyLogs" directory
    File dateFolder = new File(gaLogsDir, currentDate);

    // Create the "MyLogs" directory if it doesn't exist
    if (!downloadsDir.exists()) {
        downloadsDir.mkdirs(); // Make directories recursively
    }

    // Create the date folder if it doesn't exist
    if (!dateFolder.exists()) {
        dateFolder.mkdirs(); // Make directories recursively
    }

    // Initialize XLog with file printer
    LogConfiguration config = new LogConfiguration.Builder()
            .tag("MyApp")
            .build();

    // Create subfolders 'General', 'Sync', 'Mission', 'Task' inside the date folder
    String[] subfolders = {"General",
            "Sync",
            "Mission",
            "Task",
            "LiveData",
            "Vehicle Alerts"};

    for (String subfolderName : subfolders) {
        File subfolder = new File(dateFolder, subfolderName);
        if (!subfolder.exists()) {
            subfolder.mkdirs(); // Make directories recursively
        }

        FilePrinter filePrinter = new FilePrinter
                .Builder(subfolder.getAbsolutePath())
                .fileNameGenerator(new ChangelessFileNameGenerator("Indent_id_"))
                .backupStrategy(new AbstractBackupStrategy() {
                    @Override
                    public int getMaxBackupIndex() {
                        return 0;
                    }

                    @Override
                    public boolean shouldBackup(File file) {
                        return file.length() > maxSize; // Backup if file size exceeds 1MB
                    }

                    @Override
                    public String getBackupFileName(String fileName, int backupIndex) {
                        return fileName + "_" + backupIndex;
                    }
                })
                .flattener(new ClassicFlattener())
                .writer(new SimpleWriter() {                           // Default: SimpleWriter
                    @Override
                    public void onNewFileCreated(File file) {
                        super.onNewFileCreated(file);
                        final String header = "\n>>>>>>>>>>>>>>>> File Header >>>>>>>>>>>>>>>>" +
                                "\nDevice Manufacturer: " + Build.MANUFACTURER +
                                "\nDevice Model       : " + Build.MODEL +
                                "\nAndroid Version    : " + Build.VERSION.RELEASE +
                                "\nAndroid SDK        : " + Build.VERSION.SDK_INT +
                                "\nApp VersionName    : " + BuildConfig.VERSION_NAME +
                                "\nApp VersionCode    : " + BuildConfig.VERSION_CODE +
                                "\n Date              :  "+ formattedDate+
                                "\n<<<<<<<<<<<<<<<< File Header <<<<<<<<<<<<<<<<\n\n";
                        appendLog(header);
                    }
                })
                .build();


        XLog.init(config, new AndroidPrinter(), filePrinter);
    }
    isXLogInitialized = true;
}

I am calling the below method in some spefic acitivity acc to folder.

public void insertLog(String subfolderName, String message,String tag) {
// Check if XLog is initialized
if (!isXLogInitialized) {
Log.e("MyApplication", "XLog is not initialized. Call initializeXLog() first.");
return;
}
File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
// Get the downloads directory
// Create a File object representing the "GA Logs" directory within the downloads directory
File gaLogsDir = new File(downloadsDir, "LogsDemo");

    // Get the current date in the format "dd_MMM" (e.g., "11_Apr")
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd_MMM");
    String currentDate = dateFormat.format(new Date());

    // Create a File object representing the current date folder within the "MyLogs" directory
    File dateFolder = new File(gaLogsDir, currentDate);

    // Create the subfolder if it doesn't exist
    File subfolder = new File(dateFolder, subfolderName);
    if (!subfolder.exists()) {
        subfolder.mkdirs(); // Make directories recursively
    }
    // Insert log message into the specified subfolder
    XLog.tag(tag).i(message);
}

when ever i putting values from GeneralAcitivity it is storing in vehicle folder.

 button.setOnClickListener(v -> {

         ((XLogSampleApplication) getApplication()).insertLog("LogsDemo/General", "Btn Clicked","GeneralActivity");

       
     });
      I want as per specific folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant