tony19/logback-android

not writting files android 11

sagamagus opened this issue · 5 comments

Description

there is an issue involving Android 11 and the google policy that dont let upload to playstore with the permission "MANAGE_EXTERNAL_STORAGE", if you dont use that permission the logs write once, but if you have another app in background, doesnt writte nor the file, nor the logs once the files exists, and if you delete the log file, it doent write again a file with that name, you must change the name of the file.

Steps to reproduce

logback.xml:
<configuration>
<timestamp key="bySecond" datePattern="yyyyMMdd"/>
<property name="LOG_DIR" value="/storage/emulated/0/documents/logs" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${LOG_DIR}/log-${bySecond}.txt</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{40} - %msg%n</pattern>
</encoder>
</appender>

`<appender name="LOGCAT" class="ch.qos.logback.classic.android.LogcatAppender">`
    `<encoder>`
        `<pattern>%-5level - %msg%n</pattern>`
    `</encoder>`
`</appender>`

`<root level="DEBUG">`
    `<appender-ref ref="FILE" />`
    `<appender-ref ref="LOGCAT" />`
`</root>`

</configuration>

add this to manifest to write in Documents route:
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

Environment

  • logback-android version: 2.0.0
  • Android version: API 30 Android 11
  • Platform: Any

Notes on permission usage:

It sounds like there are two issues here:

  1. You want to write logs to shared storage on Android 11, and you're trying the Documents folder, which is normally not allowed in Android 11. It's not clear to me whether you actually need the file location to be the Documents folder. If the objective is for users to be able to pull the logs themselves from a commonly accessible directory, you could store the logs in the Downloads folder, which does not require any special permissions.

  2. You delete the log file while the app is running, and you want the logger to create a new log file the next time it tries to log something (all while not restarting the app), but it doesn't? Is that right?

I saw the same issue, and I see the following error:

|-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[file] - openFile(/storage/emulated/0/Android/data/com.example/files/file.log,true) failed java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.example/files/file.log: open failed: ENOENT (No such file or directory)

I started to debug the issue and added the following code to my Application onCreate method:

System.out.println("External files dir: " + getExternalFilesDir(null));

With that code logback-android magically started to create parent directories and log files.
My manifest permissions looks like this:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />

(Doesn't need any permissions on Android 4.4+)