Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:15:59+00:00 2026-06-17T15:15:59+00:00

As a long time log4j user, it is really enjoying to find log4j is

  • 0

As a long time log4j user, it is really enjoying to find log4j is available on Android with the help of this project https://code.google.com/p/android-logging-log4j/

For my current Android projects, I am writing logs both to logcat and a file on SD-Card, and the logging file is of great value to my project.

But some recent feedbacks from my beta testing users revealed that if the the SD-Card is removed while my application is writing logs, it will cause a Force-Close.

My current solution is to detect if external storage is mounted when log4j is first used; if no external storage is detected, I will disable log file output.

import java.io.File;

import org.apache.log4j.Level;

import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;

public class ConfigureLog4j {
    private static boolean configured = false;

    public static void configure() {
        if (configured == true) {
            return;
        }

        String status = Environment.getExternalStorageState();

        final LogConfigurator logConfigurator = new LogConfigurator();

        if (status.equals(Environment.MEDIA_MOUNTED)) {
            logConfigurator.setUseFileAppender(true);
            logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "msgant.log");
        } else {
            logConfigurator.setUseFileAppender(false);
        }
        logConfigurator.setRootLevel(Level.DEBUG);
        // Set log level of a specific logger
        logConfigurator.setLevel("org.apache", Level.ERROR);
        logConfigurator.configure();

        configured = true;
    }
}

It is far from a perfect solution, and actually, it will not solve my current issue with log4j crash, because I can not decide when users try to unmount SD-Card.

Or I should make a wrapper around log4j, and implement the storage detection logic there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:16:00+00:00Added an answer on June 17, 2026 at 3:16 pm

    you can always listen to the sdcard events and reconfigure log4j accordingly.

    <receiver android:name="com.project.events.SdCardEventListener" >
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_REMOVED" />
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
        </intent-filter>
    </receiver>
    

    this will allow you to reconfigure log4j when the card is removed (you can probably use the device internal storage till such a time a SD card is inserted again.

    The SdCardEventListener should extend BroadcastReceiver and then reconfigure the logger as necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First time poster, long time user. I CAN NOT figure out why this data
Long time reader, first time poster. Any help is greatly appreciated. I have crafted
Long time reader never posted until now. Im having some trouble with Android, im
long time reader first time user. I'm putting together my first RoR application and
Long time listener, first time caller. I'm a 6 month old dev from Code
long time reader, first time writer. I searched around on google and stack overflow,
Long time viewer, first time questioner here. Using Unit Tests in a separate project,
Long time mysql/php user, first time Drupal user. Actually, I'm not doing the Drupal
Long time reader, first time poster asks: After many weeks of google and forum
Long time user, first time asker; very new to regular expressions. I'm a designer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.