logback-android
logback-android copied to clipboard
not writting files android 11
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
Hi, thanks for replying, the reason to write outside the app specific directory is to the end user to have the log at hand, because they dont have a lot of knowledge and start digging in directories is a risk.
The steps to reproduce the issue are the following:
1.- use android 11 2.- dont use MANAGE_EXTERNAL_STORAGE permission 3.- use MANAGE_DOCUMENTS permission and MANAGE_INTERNAL_STORAGE permission 4.- configure logbak at /storage/emulated/0/documents/logs directory 5.- start the app and start logging 6.- delete de log file at /storage/emulated/0/documents/logs directory 7.- try to log again, the file doesnt write again
De: Tony Trinh @.> Enviado: sábado, 6 de noviembre de 2021 09:49 p. m. Para: tony19/logback-android @.> CC: sagamagus @.>; Author @.> Asunto: Re: [tony19/logback-android] not writting files android 11 (#228)
The description is very unclear. Can you clarify with exact reproduction steps? Or a link to a reproduction? Also I can't reproduce the "write once" problem in the emulator.
Note MANAGE_EXTERNAL_STORAGE is only needed to write files outside of the app-specific directory in external storage. Is there a particular reason you want to write to /storage/emulated/0/documents/logs instead of the app-specific directory?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/tony19/logback-android/issues/228#issuecomment-962547489, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AVIF6THD5LVMGBV62EJPKY3UKYAMBANCNFSM5C57VE7A. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
Notes on permission usage:
-
Apps that use
MANAGE_EXTERNAL_STORAGE
are only blocked from the Play store if they don't meet the requirements for that permission. -
MANAGE_DOCUMENTS
can only be requested by the platform document management app (not for third party apps). -
There's no such permission named
MANAGE_INTERNAL_STORAGE
(at least not in API 30).
It sounds like there are two issues here:
-
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 theDocuments
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 theDownloads
folder, which does not require any special permissions. -
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?
Yes, the 1.- the final user is a little cocky, if i put it ond downloads he will say "but i didnt downloaded anything" or something like that, originally before android 11 we write the log directly on root on a log directory 2.- even if the app is closed, once you delete the file it doesn't write anymore
De: Tony Trinh @.> Enviado: martes, 9 de noviembre de 2021 05:45 p. m. Para: tony19/logback-android @.> CC: sagamagus @.>; Author @.> Asunto: Re: [tony19/logback-android] not writting files android 11 (#228)
It sounds like there are two issues here:
-
You want to write logs to shared storage on Android 11. It's not clear to me whether you actually need the location to be the Documents folder. If the objective is for users to be able to pull the logs themselves from a commonly accessible directly, you could use the Downloads folder, which does not require any special permissions.
-
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, but it doesn't? Is that right?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/tony19/logback-android/issues/228#issuecomment-964645935, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AVIF6TAJS2UJQDDE42VLKD3ULGXC7ANCNFSM5C57VE7A. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
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+)