0

I am using log4j to write logs to external storage in Android.

public static Logger getLogger(Class clazz) {
        final LogConfigurator logConfigurator = new LogConfigurator();
        String logsFolderName = Environment.getExternalStorageDirectory().toString() + File.separator + "player/logs/";
        String logFileName = "player.log." + getCurrentDateString();
        if (!new File(logsFolderName).exists())
            logsFolderName = "/tmp/";

        logConfigurator.setFileName(logsFolderName + logFileName);
        logConfigurator.setRootLevel(Level.ALL);
        logConfigurator.setLevel("org.apache", Level.ALL);
        logConfigurator.setUseFileAppender(true);
        logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");
        logConfigurator.setMaxFileSize(1024 * 1024 * 5);
        logConfigurator.setImmediateFlush(true);
        logConfigurator.configure();
        Logger log = Logger.getLogger(clazz);
        return log;
    }

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "app.com.blynq.player"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
...
dependencies {
...
compile group: 'log4j', name: 'log4j', version: '1.2.17'
}

I am getting the following exception upon running:

Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method) 
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)

I have taken permissions to read and write to external directories

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.com.blynq.player">


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">

        </activity>

        <receiver
            android:name="app.com.blynq.services.BootReceiver"
            android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity android:name=".RegisterActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

What is causing the issue ?

jay
  • 1,982
  • 2
  • 24
  • 54

1 Answers1

0

Whenever the logs folder was always resolving to logsFolderName = "/tmp/" this exception would arise, as there exists no such folder. For creating tmp files, refer to this post

Community
  • 1
  • 1
jay
  • 1,982
  • 2
  • 24
  • 54