Logback-android: do not write file to file

Trying to redirect log messages using logback-android so that messages can be saved to a file. However, it is not saved in the file.

This is my logback.xml file configuration, which is stored in src / main / assets in my android studio

<configuration debug="true"> <!-- Create a file appender for a log in the application data directory --> <appender name="FILE" class="ch.qos.logback.classic.android.FileAppender"> <file>/data/com.test.equa/files/log/foo.log</file> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Write INFO (and higher-level) messages to the log file --> <root level="INFO"> <appender-ref ref="FILE" /> </root> </configuration> 

This is a piece of code in which I start logging.

  @Override public void onCreate() { super.onCreate(); loadData(); Logger log = LoggerFactory.getLogger(MyActivity.class); log.error("Application Data setup in progress"); } 

Problem: I continue to see messages in logcat, but I expect that they will be saved in my Android sd card memory.

Added custom permission in the manifest for recording logs on the SD card

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

I am missing some thing in this configuration since I do not see any errors or messages in my logcat for any configuration errors. Can someone help me here.

+6
source share
3 answers

Have you tried adding configuration to AndroidManifest.xml?

Example manifest (from this link ):

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <logback> <configuration> <appender name="LOGCAT" class="ch.qos.logback.classic.android.LogcatAppender" > <tagEncoder> <pattern>%logger{0}</pattern> </tagEncoder> <encoder> <pattern>[ %thread ] %msg%n</pattern> </encoder> </appender> <root level="WARN" > <appender-ref ref="LOGCAT" /> </root> </configuration> </logback> </manifest> 
0
source

For Android 6+, you must define the permissions manually in the settings for your application. Otherwise, logback will not be allowed to write to external storage, even if you grant WRITE_EXTERNAL_STORAGE permission in manifest.xml.

I made a decision from here .

+2
source

For me, this was decided by Nii's first comment on the question.

Now my file tag is as follows:

 <file>/sdcard/Android/data/[my-package-from-AndroidManifest]/logs.log</file> 

It is strange to think that the logs.log file appeared on my internal storage at the given path (without / sdcard /).

My phone is running Android 6.0.1 in case it is relevant.

Concluding your answer, try the file path described above and check the internal storage if other answers do not work.

Update

I just wanted to open the log file on the computer, but could not find it. I can still find it with any application to access the repository (for example, a file commander) on the phone itself. But when viewing the exact same folder from a PC, this is not so. Therefore, check this with the repository access application when it is enough for you.

0
source

Source: https://habr.com/ru/post/981324/


All Articles